Skip to content

don’t apply current options to new opened file vim

I frequntly open many files in vim. The problem is some files have their own options. For example:

settings for make files

  set noexpandtab tabstop=4 shiftwidth=4 softtabstop=4
  set list listchars=eol:¬,tab:→⠀,trail:~,extends:>,precedes:<
  hi SpecialKey ctermfg=243
  hi NonText ctermfg=243

settings for C files

  set colorcolumn=100 tabstop=4 shiftwidth=4 softtabstop=4

So, if I open makefile first and then :tabe main.c I can see invisble character that were turned on from makefile.

I understand that I can :set nolist but imagine if I have many other options inherit from many other files. How can I tell vim not to inherit them when I :tabe anotherFile?



Well there is setlocal to set this things for a buffer only. But that does of course not work for a .vimrc. There you have two possibilities, autocmds or filetype plugins:

autocmd BufRead,BufNewFile *.{c} setlocal colorcolumnt=100

This does set the settings local to any oppened buffer of a file ending with .c

Or you can add the settings to a filetype-plugin. See :h filetype-plugin for more information on that. Basically just add the settings to a specific file. In your case most likely under .vim/ftplugin/c.vim

User contributions licensed under: CC BY-SA
2 People found this is helpful