简体   繁体   English

如何在 Vim 中更改特定行号颜色

[英]How to change a specific line number color in Vim

I've been wanted to do this for a while, sometimes when I have a file open I want to be able to highlight certain line numbers to a different color.我一直想这样做一段时间,有时当我打开一个文件时,我希望能够将某些行号突出显示为不同的颜色。 For example, say my LineNr is blue, and my Current LineNr is red.例如,假设我的 LineNr 是蓝色的,而我的 Current LineNr 是红色的。 Say I'm on line 25, is there anyway I can change the LineNr color of lines 28-30 to green WITHOUT leaving my current line?假设我在第 25 行,无论如何我可以在不离开当前行的情况下将第 28-30 行的 LineNr 颜色更改为绿色吗?

As a quick answer, if you don't mind highlighting only by groups of at most 8 lignes at once, you can use the matchaddpos({group}, {pos}) function and create a command to apply a highlight group to a range of lines.作为一个快速的答案,如果您不介意一次仅按最多 8 个 lignes 的组突出显示,您可以使用matchaddpos({group}, {pos})函数并创建一个命令来将highlight组应用于一个范围行。

command! -range -nargs=1 -complete=highlight HiLine call matchaddpos(<f-args>, range(<line1>,<line2>))

Which you can use, for example to highlight as 'cursorline' :您可以使用它,例如突出显示为'cursorline'

:28,30HiLine CursorLine

Note that completion applies on the argument for highlight groups.请注意,完成适用于突出显示组的参数。

To remove the group(s) of previously highlighted lines you could remove thoses that contains a particular line.要删除先前突出显示的行组,您可以删除包含特定行的那些行。 I can't find a simpler way than to go through all getmatches() dicts and matchdelete({id}) the ones that contains the line on one of their 'posX' key :我找不到比遍历所有getmatches() matchdelete({id})matchdelete({id})包含其'posX'键之一上的行的那些更简单的方法:

function! s:RemoveMatchOnLine(line) abort
  for l:match in getmatches()
    let l:matchlines = values(filter(copy(l:match), 'v:key =~# ''pos\d\+'''))
    if index(l:matchlines, [a:line]) >= 0
      call matchdelete(l:match['id'])
    endif
  endfor
endfunction
command! -nargs=? LoLine call <SID>RemoveMatchOnLine(<q-args> ? <q-args> : line('.'))

Now, you can :LoLine to undo the highlighting on lines near the current line, or you can give it an argument to specify another line, so you don't have to move your cursor there : :LoLine 28 .现在,您可以:LoLine撤消当前行附近行的突出显示,或者您可以给它一个参数来指定另一行,这样您就不:LoLine 28光标移到那里:LoLine 28

Finally, you can set mappings :最后,您可以设置映射:

nnoremap <leader>hi :HiLine CursorLine<CR>
xnoremap <leader>hi :HiLine CursorLine<CR>
nnoremap <leader>hc :<c-u>execute 'LoLine ' . v:count<CR>

Typing [count]<leader>hi in normal mode will highlight count lines from the cursor.在正常模式下键入[count]<leader>hi将突出显示光标处的count行。 And [count]<leader>hc will remove the highlighting on the group of line count .[count]<leader>hc将删除 line count组上的突出显示。

Addendum附录

We could work on larger ranges using matchadd({group}, {pattern}) , using \\%xl to match line x .我们可以使用matchadd({group}, {pattern})处理更大的范围,使用\\%xl来匹配行x Replace call matchaddpos(... by替换call matchaddpos(... by

execute 'call matchadd(<f-args>, ''\%'.<line1>.'l\(.*\n\)\+\%'.(<line2>+1).'l'')'

and line 2 and 3 of the function by以及函数的第 2 行和第 3 行

let l:a = matchlist(get(l:match,'pattern',''), '^\\%\(\d\+\)l.*\\%\(\d\+\)l$')
if !empty(l:a) && l:a[1] <= a:line && a:line <= l:a[2]

But for me it breaks on large ranges, I would prefer to have the first solution which seems more robust.但对我来说,它在大范围内中断,我更喜欢第一个看起来更强大的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM