cyxeL
cyxeL

Reputation: 33

vim syntax doesn't work at all

I'm a beginner of vim, so I don't really know how vim work. And here I've got a problem. I can't use syntax in vim, it only works in some documents, such as vimrc or the documents with .vim.

If I add .vim behind the document syntax run well, but another extension name then syntax doesn't work. I tried to fix it by modifying /etc/vim/vimrc:

add
syntax on
set background=dark
set hlsearch
set number

Every time I open a new file I can see the number on each line and the dark background, only syntax doesn't work .

Maybe this is too easy to you guys but I can't find any way to fix this. Can someone tell me what I did wrong?

By the way I'm using Kali Linux (x64).

Upvotes: 2

Views: 1973

Answers (1)

Fred
Fred

Reputation: 391

vim tries to automatically determine the file type. If it detects a known type, it will use syntax highlighting. If it doesn't detect the type, it does not know which syntax to use (e.g. Java/vimrc/apache-config).

You can always check which file type vim thinks the current document has by using the command :set filetype. If the variable is empty, vim does now know which type of file this is and thus cannot use syntax highlighting.

There are several possibilities to "help" vim determine the file type.

  1. The easiest is to use the correct file extension: If, for example, you open a file called test.html, vim knows that you will probably fill this file with HTML code and thus automatically sets the filetype variable to html.
  2. You can also just write the file in a manner so that vim knows what kind of file this is. If, for example, a file begins with #!/bin/bash, vim knows that this is a bash script and sets the filetype variable to bash.
  3. If all else fails, you can also tell vim the filetype explicitly. Either set it in the editor, e.g. :set filetype=bash, or use a modeline, e.g. # vim:filetype=bash.

Regardless of whether the file type was correctly identified, vim needs to know which colours to use for which words. These are specified in "vim syntax files". On Debian, these are located in /usr/share/vim/vim<your-vim-version>/syntax, e.g. /usr/share/vim/vim74/syntax. I don't know if the location is the same on Kali, but I suspect it will be similar. If there is no vim syntax file for the file type you are editing, vim cannot help you. However, you might be able to find a syntax file online or in another package.

Upvotes: 4

Related Questions