How do I set up a default syntax for files that have no extension 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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With