在Vim我可以将本地配置设置为:
setlocal number
如何为特定文件类型设置本地匹配?
我用的是这个:
autocmd! BufEnter *.py,.vimrc,*.sh,*.c* :match ColorColumn /\%>80v.\+/
但是当我在同一个会话中打开另一种文件类型的文件时,这会给我匹配的结果ColorColumn
。
答案1
这对作者来说可能不再有用,但我把以下内容放入.vim/ftplugin/python.vim
:
if exists('+colorcolumn')
setlocal colorcolumn=81
else
au! BufEnter <buffer> match ColorColumn /\%81v.*/
endif
因为它在 ftplugin 中,所以它只出现在 python 文件中,并且 BufEnter 将它保存在 python 文件所在的缓冲区本地。
答案2
我用这个解决了它:
augroup longLines
autocmd! BufEnter *.py,.vimrc,*.sh,*.c* :match ColorColumn /\%>80v.\+/
augroup END
答案3
答案4
这里的大多数答案都忽略了匹配仅限于 WINDOW LOCAL!下面高度注释的代码解释了如何仅为某些文件类型添加匹配。
问题是“在 Vim 中,如何为特定文件类型设置本地匹配?”下面回答了这个问题,但针对的是尾随空格而不是颜色列的情况。
" 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
参考:help :match
和:help matchadd()