4

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.

4 Answers 4

4

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.

2
  • This solution seems to hinge on exists()... without that part when I tried to apply it, it applied the match to all buffer types. I recommend @Rook's answer. Apr 18, 2020 at 11:59
  • 1
    match applies to a window not a buffer, if you switch to a none python buffer in the same window, you will have the same highlighting. Jul 30, 2020 at 12:42
1

I solved it with this:

augroup longLines
    autocmd! BufEnter *.py,.vimrc,*.sh,*.c* :match ColorColumn /\%>80v.\+/                      
augroup END
5
  • I didn't actually understand your question to the detail, but that doesn't exactly do what you asked for.
    – Rook
    Nov 13, 2011 at 12:52
  • my question is, if I open many files, how activate the match only in some files?
    – juanpablo
    Nov 13, 2011 at 15:34
  • "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
    Nov 13, 2011 at 19:15
  • many files, some *.py, some *.c, some *.txt
    – juanpablo
    Nov 13, 2011 at 20:56
  • See my answer. Is that what you wanted?
    – Rook
    Nov 13, 2011 at 23:46
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
3
  • *.sh is a bash file, a sh filetype
    – juanpablo
    Nov 14, 2011 at 1:22
  • @juanpablo - Updated the answer to include sh.
    – Rook
    Nov 14, 2011 at 1:37
  • what if I want to apply highlight to an already active buffer, i.e. not by .vimrc loading but by explicit on-the-fly command call matchadd() and still want to guarantee it affects the file's buffer only if its extension is specific? Is there a autocmd associated (like "apply immediately") or some kind of an if guard must be used?
    – bloody
    Dec 6, 2021 at 21:04
0

Most answers here ignore that match is WINDOW LOCAL ONLY! Highly commented code below to explain how to add match for a certain filetypes only.

The question asked is "In Vim how I can set a local match only for a specific filetype?" Below answers that, but for the case of trailing whitespace instead of colour column.

" Create a highlight group with the colours we wish to apply
hi _MatchTrailingWhitespace guibg=#880000 " Highlight trailing whitespace

function! AddWindowMatches()
    " match is WINDOW LOCAL ONLY, so we have to jump through some hoops to
    " make it apply to buffers only. i.e. we cant just use :setlocal match!

    " First clear all matches on the window, then we will add back the matches
    " required for each file type
    call clearmatches()

    " TRAILING WHITESPACE
    " Must escape the plus, match one or more space before the end of line
    " match trailing whitespace, except when typing at the end of a line.
    " If the filetype is python or javascript
    if index(['python', 'javascript'], &ft) >= 0
        " Can use the :match command, or the matchadd() function which returns a handle 
        " to the match, so it can easily be cleared with matchdelete(), not used here
        "match _MatchTrailingWhitespace /\s\+$/
        let w:match_trailing_space_id = matchadd('_MatchTrailingWhitespace', '\s\+$', -1)        
    endif
endfunction

augroup add_window_matches
    autocmd!
    autocmd BufWinEnter * :call myal#AddWindowMatches()
augroup END

Refer to :help :match and :help matchadd()

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .