Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim default syntax for files with no extension

Tags:

vim

How do I set up a default syntax for files that have no extension in vim?

like image 949
zly Avatar asked Apr 19 '10 10:04

zly


People also ask

How do I enable syntax in vim?

After opening login.sh file in vim editor, press ESC key and type ':syntax on' to enable syntax highlighting. The file will look like the following image if syntax highlighting is on. Press ESC key and type, “syntax off” to disable syntax highlighting.

What is vim Ftplugin?

A file type plugin (ftplugin) is a script that is run automatically when Vim detects the type of file when the file is created or opened. The type can be detected from the file name (for example, file sample. c has file type c ), or from the file contents.

How do I change the filetype in vim?

As other answers have mentioned, you can use the vim set command to set syntax. :set syntax=<type> where <type> is something like perl , html , php , etc. There is another mechanism that can be used to control syntax highlighting called filetype , or ft for short.

Where does vim put syntax files?

Install the syntax file. Save the file, then install it by copying the file to ~/. vim/syntax/cel. vim on Unix-based systems, or to $HOME/vimfiles/syntax/cel.


2 Answers

One way would be to add an autocommand to your .vimrc for files that don't have the syntax set:

au BufNewFile,BufRead * if &syntax == '' | set syntax=html | endif

Or, you could set the filetype for any file that it's not defined for:

filetype plugin on
au BufNewFile,BufRead * if &ft == '' | set ft=html | endif

Setting filetype plugin on along with the au command gives the added benefit of loading HTML plugins if you have any. This also sets the syntax to "html" as well.

like image 171
Curt Nelson Avatar answered Oct 21 '22 02:10

Curt Nelson


To pick the default syntax for files without an extension, you can create an autocommand that checks if the filename contains a ., and if not, switches to the desired syntax:

autocmd BufNewFile,BufRead * if expand('%:t') !~ '\.' | set syntax=perl | endif

This one picks perl as a default syntax, but you can simply use whichever is appropriate.

like image 26
Sebastian Paaske Tørholm Avatar answered Oct 21 '22 01:10

Sebastian Paaske Tørholm