Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

colorcolum only in certain files (e.g. *.cpp, *.h) in VIM

Tags:

vim

I would like to have a marker at column 80 in VIM, but only in file like *.cpp, *.h. but not in *.txt

For now I have this in my .vimrc

set cc=120

Cheers

Solution:

autocmd FileType cpp,c,cxx,h,hpp,python,sh  setlocal cc=120
like image 941
ezdazuzena Avatar asked Oct 19 '25 11:10

ezdazuzena


1 Answers

Vim doesn't directly use the file extension, it has an indirection called filetype, which is then used for syntax highlighting and specific settings.

Put your :set command (as :setlocal, so that it only affects the current buffer [1]) in a new file ~/.vim/after/ftplugin/cpp.vim. (You could also use :autocmd FileType cpp setlocal cc=120 directly in your .vimrc, but the separation is cleaner once you do a lot of that customization.)


[1] Note that 'colorcolumn' is window-local, not buffer-local, so the approach isn't perfect, but usually good enough. It can be perfected with additional BufWinEnter/Leave autocmds.

like image 152
Ingo Karkat Avatar answered Oct 21 '25 02:10

Ingo Karkat