Vim 插入模式下光标线颜色变化

发布于 2024-12-07 14:19:18 字数 630 浏览 6 评论 0原文

有一个用于更改光标颜色的好片段:

if &term =~ "xterm\\|rxvt"
  " use an orange cursor in insert mode
  let &t_SI = "\<Esc>]12;orange\x7"
  " use a red cursor otherwise
  let &t_EI = "\<Esc>]12;red\x7"
  silent !echo -ne "\033]12;red\007"
  " reset cursor when vim exits
  autocmd VimLeave * silent !echo -ne "\033]112\007"
  " use \003]12;gray\007 for gnome-terminal
endif

我应该如何更改它,而不是光标,CursorLine 会更改颜色,例如从深蓝色更改为蓝色?

我的完整配置是 https://bitbucket.org/JackLeo/home-configs/src/5b8faf340f87/。 vimrc

There is good snippet for changing cursor color:

if &term =~ "xterm\\|rxvt"
  " use an orange cursor in insert mode
  let &t_SI = "\<Esc>]12;orange\x7"
  " use a red cursor otherwise
  let &t_EI = "\<Esc>]12;red\x7"
  silent !echo -ne "\033]12;red\007"
  " reset cursor when vim exits
  autocmd VimLeave * silent !echo -ne "\033]112\007"
  " use \003]12;gray\007 for gnome-terminal
endif

How should I alter this that instead of cursor, CursorLine would change color for example from dark blue to blue?

My complete config is https://bitbucket.org/JackLeo/home-configs/src/5b8faf340f87/.vimrc

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

天暗了我发光 2024-12-14 14:19:18

您是否研究过“突出显示”命令,这是控制此操作的更简单方法。

例如,要更改 CursorLine,

:hi CursorLine guifg=red guibg=blue

请参考: :helphighlight

使其在模式之间切换。

" Enable CursorLine
set cursorline

" Default Colors for CursorLine
highlight  CursorLine ctermbg=Yellow ctermfg=None

" Change Color when entering Insert Mode
autocmd InsertEnter * highlight  CursorLine ctermbg=Green ctermfg=Red

" Revert Color to default when leaving Insert Mode
autocmd InsertLeave * highlight  CursorLine ctermbg=Yellow ctermfg=None

我也许可以将 termcap 颜色与 autocmd 混合使用,但在我看来,突出显示更容易长期维护(如果偶尔使用 gVim)

Have you look in into the 'highlight' command which is a easier way to control this.

For example, to change the CursorLine,

:hi CursorLine guifg=red guibg=blue

Reference: :help highlight

To make it switch between mode.

" Enable CursorLine
set cursorline

" Default Colors for CursorLine
highlight  CursorLine ctermbg=Yellow ctermfg=None

" Change Color when entering Insert Mode
autocmd InsertEnter * highlight  CursorLine ctermbg=Green ctermfg=Red

" Revert Color to default when leaving Insert Mode
autocmd InsertLeave * highlight  CursorLine ctermbg=Yellow ctermfg=None

I may be possible to mix termcap color with autocmd, but IMO, highlight is more easy to maintain in long term (and in case if use gVim occassionally)

蓝天 2024-12-14 14:19:18

这非常简单,将以下内容放入 .vimrc< /a> 或自定义 colorscheme 文件。

set cursorline
autocmd InsertEnter * highlight CursorLine guifg=white guibg=blue ctermfg=white ctermbg=blue
autocmd InsertLeave * highlight CursorLine guifg=white guibg=darkblue ctermfg=white ctermbg=darkblue

有关更多信息,请参阅:

注意:你可以使用相同的方法来更改光标的颜色,而无需所有这些 if 语句和转义序列(这也适用于 GVim)。

This is pretty straightforward, put the following in your .vimrc or custom colorscheme file.

set cursorline
autocmd InsertEnter * highlight CursorLine guifg=white guibg=blue ctermfg=white ctermbg=blue
autocmd InsertLeave * highlight CursorLine guifg=white guibg=darkblue ctermfg=white ctermbg=darkblue

For more information see:

N.B: You can use the same method to change the colour of the cursor without all of those if-statements and escape-sequences (and this will also work in GVim).

花落人断肠 2024-12-14 14:19:18

当将 MacVim 与 'Lokaltog/vim-powerline' 一起使用时,您可以设置正常/视觉/插入颜色与电力线模式颜色相匹配。我发现这对于在不阅读电源线的情况下了解我所处的模式非常有帮助,尤其是在大屏幕上。

这是我正在使用的代码,基于 @Zarick-Lau 的答案。

在我的 colors/molokai.vim 文件中:

" Visual Mode Orange Background, Black Text
hi Visual          guifg=#000000 guibg=#FD971F

" Default Colors for CursorLine
highlight CursorLine guibg=#3E3D32
highlight Cursor guibg=#A6E22E;

" Change Color when entering Insert Mode
autocmd InsertEnter * highlight  CursorLine guibg=#323D3E
autocmd InsertEnter * highlight  Cursor guibg=#00AAFF;

" Revert Color to default when leaving Insert Mode
autocmd InsertLeave * highlight  CursorLine guibg=#3E3D32
autocmd InsertLeave * highlight  Cursor guibg=#A6E22E;

这是一个使用 molokai 原始配色方案的示例。

正常

正常模式(绿色

视觉

视觉模式(橙色)

插入

插入模式(blue)

我还发现将操作系统设置为使用相同颜色进行视觉选择也很有帮助。例如,我已将 OSX 中的突出显示颜色更改为 Orange, 蓝色,与 VIM 中相同。

当我选择文本时,它现在是橙色而不是 src="https://i.sstatic.net/HSZjR.png" alt="选择橙色突出显示">

示例

这里,文本框中使用的橙色突出显示为我现在我在操作系统中选择的所有文本都与 VIM 设置匹配。

When using MacVim with 'Lokaltog/vim-powerline' you can setup your normal/visual/insert colors to match the powerline mode color. I find this extremely helpful to know what mode I'm in without reading the powerline, especially on a large screen.

Here is the code I am using, based on @Zarick-Lau's answer.

In my colors/molokai.vim file:

" Visual Mode Orange Background, Black Text
hi Visual          guifg=#000000 guibg=#FD971F

" Default Colors for CursorLine
highlight CursorLine guibg=#3E3D32
highlight Cursor guibg=#A6E22E;

" Change Color when entering Insert Mode
autocmd InsertEnter * highlight  CursorLine guibg=#323D3E
autocmd InsertEnter * highlight  Cursor guibg=#00AAFF;

" Revert Color to default when leaving Insert Mode
autocmd InsertLeave * highlight  CursorLine guibg=#3E3D32
autocmd InsertLeave * highlight  Cursor guibg=#A6E22E;

Here is an example using the molokai original color scheme.

Normal

normal mode (green

Visual

visual mode (orange)

Insert

insert mode (blue)

I also find it's helpful to set the OS up to visually select using the same color too. For example, I've changed my highlight color to Orange in OSX, and when I select text, it is now orange instead of blue, same as in VIM.

select orange highlight

Example

Here the orange highlight being used in the text-box as I'm writing this Stack Overflow entry. Now all text I select in my OS matches the VIM setup.

selecting text in the OS

梦初启 2024-12-14 14:19:18

我选择在插入模式下切换 CursorLineNormal。首先使用 :hi Normal:hi CursorLine 获取值。然后调整以下行:

set cursorline
autocmd InsertEnter * highlight Normal ctermbg=7
autocmd InsertEnter * highlight CursorLine ctermbg=15
autocmd InsertLeave * highlight Normal ctermbg=15
autocmd InsertLeave * highlight CursorLine ctermbg=7

对于日光,看起来像这样。我喜欢“聚焦”效果。

正常模式插入模式

I chose to switch CursorLine and Normal in insert mode. First get the values with :hi Normal and :hi CursorLine. Then adjust the following lines:

set cursorline
autocmd InsertEnter * highlight Normal ctermbg=7
autocmd InsertEnter * highlight CursorLine ctermbg=15
autocmd InsertLeave * highlight Normal ctermbg=15
autocmd InsertLeave * highlight CursorLine ctermbg=7

For solarized light, this looks like this. I like the "focus" effect.

normal modeinsert mode

凉世弥音 2024-12-14 14:19:18

即使您进入或离开插入模式,当前行中也没有颜色


"set cursorline
set noshowmode

"Enable CursorLine
set nocursorline

"Default Colors for CursorLine
hi CursorLine cterm=NONE ctermbg=NONE ctermfg=NONE

"Change Color when entering Insert Mode
autocmd InsertEnter * set nocursorline

"Revert Color to default when leaving Insert Mode
autocmd InsertLeave * set nocursorline

NO COLOR in current line even if you enter or leave INSERT MODE


"set cursorline
set noshowmode

"Enable CursorLine
set nocursorline

"Default Colors for CursorLine
hi CursorLine cterm=NONE ctermbg=NONE ctermfg=NONE

"Change Color when entering Insert Mode
autocmd InsertEnter * set nocursorline

"Revert Color to default when leaving Insert Mode
autocmd InsertLeave * set nocursorline

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文