Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am editing some scripts on Linux without the languages file extensions, and it seems that the editors, namely vi, nano and gedit are not applying syntax highlighting because the filenames don't use the language extensions.

Is there some parameters to be passed or some setting that can enable them to recognize the language?

Update:

After some googling I realize that bash has that ability, at least to do some parsing or check the shebang at the top determine the language. By default Ubuntu does not install the complete vim package, so after installing it, the shell files are recognized. I don't know about nano or gedit, but vi and its graphical counterpart will do.

share|improve this question
    
If the editor is extensible, you could hack something together using file(1). –  Daniel Beck Oct 3 '12 at 16:25
    
Syntax highlight works fine on my install in both nano and gedit on all language files, whether they have an extension or not. Arch64_bit/Gnome 3. I'd say it's something specific to your particular setup, unless you provide more details. –  don_crissti Oct 3 '12 at 18:28
add comment

2 Answers 2

up vote 2 down vote accepted

This question cannot be answered generally such that it applies to all text editors.

Vim recognizes files by suffix. This is driven by its default start-up scriptology, and you can hack up custom behaviors in your .vimrc.

For example, I wrote myself a C program called autotab which looks at the contents of a file and determines the shiftwidth and tabstop and expandtab settings for Vim based on how that file appears to be formatted.

When I load or reload a file in Vim, this program will analyze the contents, and then output the above Vim parameters which are interpolated into a set command and executed. For example, if the file is found to be using a two space indentation, but evidently with a mixture of spaces and eight-space hard tabs, then the program will output tabstop=8 shiftwidth=2 noexpandtab.

Exactly the same approach could be used to choose the syntax highlighting. Write a script that looks at some lines of a file, guesses the type, and then generates a :set syntax ... command.

The file utility could be used as the basis of the script. The program's "magic" database contains heuristics to detect some programming languages. Try running file on various files of yours and see the output. You may be surprised at how well it identifies the content. So the strategy might be: run file on the file, capture the output, analyze it into various cases and output the right Vim incantations. Then hook that into .vimrc.

GIT repo of autotab: http://www.kylheku.com/cgit/c-snippets/tree/autotab.c

share|improve this answer
    
Vim questions are equally on topic here –  slhck Oct 3 '12 at 16:25
add comment

First of all, the file type may be recognized in Linux/Unix systems by the "Magic Number" and this file type identifier may be checked with an editor with Hexadecimal editing functions or directly with and Hex Editor. Unfortunately, plain text files can't be recognized this way.

More information on this topic: Magic Number Definition

The possible solution for plain text file is to use an editor able to recognized which programing language is used.

May be Geany or SciTe . They support different file types but I can't say if they can recognized it if the file was created by an other editor for example...

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.