3

I'm a vim beginner. I want to handle multiple programming languages. I find great resources for how to perfect vim for one programming language by putting ton of stuff in one vimrc file. This is annoying.

I found about filetype plugin, but it's confusing. All I need is to put a vimrc file that runs for this specific lanuage. However, filetype plugin is huge with tons of features and tons of strange file paths.

How do I set different settings/plugins for different file types?

Note: It would be nice if there's a guide on how do people go about this basic need.

3

To enable filetype detection, add the following command to your vimrc:

filetype on

Then, for each filetype you want settings for, create a Vimscript file in the following location:

~/.vim/after/ftplugin/

Name the file with the filetype you want it to apply to, so for e.g. a Python settings file, create:

~/.vim/after/ftplugin/python.vim

For more details, see :help ftplugin-overrule.

  • Thanks, this is great. However, I couldn't use vim-plug in this ***.vim file (probably it's not important). – goonyMachine Sep 30 at 23:23
  • @goonyMachine I'm a little surprised you can't use the Plug commands in this setup, but I don't know how vim-plug actually functions under-the-hood. If the filetype-specific plugin-loading is more important to you than vim-plug's management features, you could possibly switch over to Vim's packages feature. If you install your plugins under the opt subdirectory then they can be loaded only when you want them with :packadd. See :help packages for more details. – Rich Oct 2 at 21:11
  • I didn't know about this. However, I'd stick with plugins in my main .vimrc over this. Hopefully the plugin is good enough to not do any overhead. – goonyMachine Oct 2 at 22:05
  • 1
    Additional info that might help beginners: (1) Vim uses certain names for different files (python.vim, cpp.vim) to get this name use :set FileType? (2) For Latex users: vim sometimes assume .tex file as plain text file. to change this add the following to .vimrc let g:tex_flavor = "latex" – goonyMachine Oct 2 at 22:10
0

Sometimes is good to define an autocmd just for your file type. You can put it in your .vimrc.

For example,

autocmd filetype tex nmap <buffer> K <leader>lv

K is mainly for documentation. But in tex files I want it to trigger to go line(vimtex).

Basically, you can type any ex cmd instead of nmap.

  • :h autocmd is recommended – eyal karni Oct 2 at 19:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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