In Vim how I can set a local match only for a specific filetype?

3

In Vim I can set local configuration as:

 setlocal number

How I can set a local match only for a specific filetype?

I use this:

 autocmd! BufEnter *.py,.vimrc,*.sh,*.c* :match ColorColumn /\%>80v.\+/

But when I open a file of another filetype, in the same session, this gives me the match ColorColumn.

juanpablo

Posted 2011-11-13T04:36:54.357

Reputation: 5 216

Answers

3

This might no longer be useful to the author, but I put the following in .vim/ftplugin/python.vim:

if exists('+colorcolumn')
    setlocal colorcolumn=81
else
    au! BufEnter <buffer> match ColorColumn /\%81v.*/
endif

Since it's in ftplugin, it only occurs for python files, and BufEnter keeps it local to the buffer that the python file is in.

Edd

Posted 2011-11-13T04:36:54.357

Reputation: 131

1

I solved it with this:

augroup longLines
    autocmd! BufEnter *.py,.vimrc,*.sh,*.c* :match ColorColumn /\%>80v.\+/                      
augroup END

juanpablo

Posted 2011-11-13T04:36:54.357

Reputation: 5 216

I didn't actually understand your question to the detail, but that doesn't exactly do what you asked for. – Rook – 2011-11-13T12:52:30.167

my question is, if I open many files, how activate the match only in some files? – juanpablo – 2011-11-13T15:34:42.503

"many files" ?? Many files of a certain filetype or by extension or ... ? Filetype is activated by autocmd FileType, BufEnter by opening a buffer. Depends on what you want. Could you try to explain it a bit further? – Rook – 2011-11-13T19:15:29.210

many files, some *.py, some *.c, some *.txt – juanpablo – 2011-11-13T20:56:07.240

See my answer. Is that what you wanted? – Rook – 2011-11-13T23:46:24.197

1

This will make the highlighting active when Python or C filetypes are open (feel free to add the others in there. I didn't know what filetype *.sh extension belongs to), and make it go away for all the others.

augroup LongLines
    autocmd!
    autocmd FileType * match none
    autocmd FileType python,c,sh match ColorColumn /\%>80v.\+/
augroup END

Rook

Posted 2011-11-13T04:36:54.357

Reputation: 21 622

*.sh is a bash file, a sh filetype – juanpablo – 2011-11-14T01:22:19.583

@juanpablo - Updated the answer to include sh. – Rook – 2011-11-14T01:37:03.587