4

The editor Vim comes with syntax highlighting for many different programming languages.
Questions:

  1. In Emacs, language-specific settings are called "modes". In Vim, however, the term "mode" refers to command or insert mode. So what is the Vim term for programming language specific settings?
  2. Is the programming language of a document determined from its file name extension, or from its contents?
  3. How can I find out which programming language specific mode Vim is in?
  4. How can I overwrite that once and for all for a certain class of documents?
3
  • 7
    (1) Filetypes. See :h filetype. (2) Both, depending on file. (3) :set ft?. (4) Depends on what you mean by "class of documents". :h :autocmd and :h FileType might help. Commented Oct 20, 2016 at 7:12
  • By "class of documents" I meant "filetype", as I learnt from your concise answer to (1). Commented Oct 20, 2016 at 9:03
  • Could you possibly resubmit your comment as an answer, and perhaps elaborate a bit on (4)? Commented Oct 20, 2016 at 9:04

2 Answers 2

11

Posting an answer, as requested.

(1) In Emacs, language-specific settings are called "modes". In vim, however, the term "mode" refers to command vs insert mode. So what is the vim term for programming language specific settings?

The equivalent Vim term is filetype. Vim uses filetypes to apply language-specific options, indentation, syntax highlighting, key mappings, etc. This is described in detail in the help, see :h filetype.

To enable automatic filetype detection and handling, you'd normally add something like this to your vimrc:

filetype plugin indent on
syntax on

To override the settings Vim gives you this way, you need to add the overrides to a file ~/.vim/after/ftplugin/<filetype>.vim (or equivalent). See :h ftplugin for more details.

(2) Is the programming language of a document determined from its file name extension, or from its contents?

Both methods are used. Vim does most filetype detection in a file filetype.vim in it's runtime directory. To find out the exact location of this file:

:echo $VIMRUNTIME.'/filetype.vim'

Plugins can add more filetypes, and / or override detection and handling of standard ones.

(3) How can I find out which programming language specific mode vim is in?

From Vim:

set ft?

(4) How can I overwrite that once and for all for a certain class of documents?

To change the filetype of the current file:

:setf <new_filetype>

or

:setl ft=<new_filetype>

To make the change permanent: do that from a modeline (cf. :h modeline). For example:

# vim: filetype=python

You can also use autocmds to achieve the same effect:

autocmd BufRead,BufNewFile *.py setlocal filetype=python

Do read :h filetype, where all this is described in detail.

Sign up to request clarification or add additional context in comments.

Comments

3

How are they called & how to detect them

Vim uses the file filetype.vim which is used to determine the "type" of a file. So if you are editing a python file "example.py" and you used the command :set ft? it should display filetype=python. Using the file type VIM determines whether any plugins indenting rules, or syntax highlighting related to python are loaded.

How to overwrite them

You can write your own indenting rules, or syntax highlighting and other rules for the programming language by putting these settings in your vimrc. using the vim language called VIML you can see this section in Learn Vimscript the Hard Way where the author detects the Potion Filetypes. Once you detect the filetype which is done using the file extension for example python files are *.py or erlang files are *.erl the you can add your own language specific settings.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.