Shourya Bansal
Shourya Bansal

Reputation: 403

Is it possible to have specific init.vim commands for different file types?

I am currently using (Neo)vim to work with multiple different file types. I have an overall init.vim that works with all of them, but for certain files, like MarkDown (.md) and LaTeX, I want to add a few other remaps. For example, I want to set Neovim to record a new event in the Undo Tree after each .!?, by using . However, I only want this to apply to my Markdown and LaTeX files. Similarly, I want my .md files to have the option set wrap enabled and to remap j to gj and k to gk. I know how to do this in my regular config file, but is there a way to only have that configuration apply to these specific file extensions?

Upvotes: 0

Views: 1616

Answers (1)

Matt
Matt

Reputation: 15091

  1. Your commands must be local to buffer. I.e. setlocal instead of set; nnoremap <buffer> ... instead of simple nnoremap ... etc.etc.

  2. Your commands must be run once, while that specific buffer is active. The cleanest way is to comply with ftplugin. That is, put your commands into ~/.vim/after/ftplugin/[filetype].vim. For example,

~/.vim/after/ftplugin/markdown.vim

setlocal wrap
nnoremap <buffer>j gj
nnoremap <buffer>k gk

Upvotes: 4

Related Questions