如何将colorcolumn / cc设置为光标列?

How to set colorcolumn / cc to the cursor column?

有没有简单的方法可以将 colorcolumn 设置为光标所在的列?

目前我在做:

  1. 手动记录状态栏中的列或使用 CTRL+g(例如第 12 列)
  2. 将颜色列设置到该位置(例如,:set colorcolumn=12

:help colorcolumn

是的,有一种简单的方法可以突出显示光标所在的列。

:set cursorcolumn

可以获得当前列调用函数col('.').

要将调用结果设置为一个选项,您需要构建一个表达式并 execute 它。这样:

:exe 'set cc=' . col('.')

首先调用获取当前游标列的函数;假设结果是 12。然后构造一个字符串表达式:'set cc=' . 12(其中.是VimScript中的字符串连接运算符;数值会自动转换为字符串)给出一个字符串set cc=12。最后,表达式的执行就像您在命令行中输入的一样。

结果有点长,所以如果你经常使用它,你最好为它写一个 command/function 并将 command/function 保存在你的 ~/.vimrc~/.vim/ .

function! SetCChere()
    :execute 'set colorcolumn=' . col('.')
endfunction

command! SetCChere call SetCChere()

现在执行 :SetCChere — 瞧!