当vim进入可视模式时运行命令 [英] Run command when vim enters visual mode

查看:81
本文介绍了当vim进入可视模式时运行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个小脚本来触发插入模式以更改行号颜色:

I use a little script to trigger insert modes in order to change the line number color:

function! CursorLineNrColorInsert(mode)
    " Insert mode: blue
    if a:mode == "i"
        highlight CursorLineNr ctermfg=4
        highlight CursorLineNr guifg=#268bd2

    " Replace mode: red
    elseif a:mode == "r"
        highlight CursorLineNr ctermfg=1
        highlight CursorLineNr guifg=#dc322f

    else
        highlight CursorLineNr ctermfg=0
        highlight CursorLineNr guifg=#073642

    endif
endfunction

autocmd InsertEnter * call CursorLineNrColorInsert(v:insertmode)
autocmd InsertLeave * highlight CursorLineNr ctermfg=0
autocmd InsertLeave * highlight CursorLineNr guifg=#073642

这很好用,当我进入任何插入模式并立即更改我的行号立即,并在普通模式下恢复为原始颜色。

That works pretty fine and changes my line number instantly when I enter any insert mode and reverts back to the original color in normal mode.

我想在可视模式下做同样的事情:

I would like to do the same for the visual mode:

function! CursorLineNrColorVisual(mode)
    " Visual mode: orange
    if mode()=~#"^[vV\<C-v>]"
        highlight CursorLineNr ctermfg=9
        highlight CursorLineNr guifg=#cb4b16

    else
        highlight CursorLineNr ctermfg=0
        highlight CursorLineNr guifg=#073642

    endif
endfunction

autocmd CursorMoved * call CursorLineNrColorVisual(mode())

不是立即,因为该函数是在 CursorMoved 上触发的。我该如何触发 CursorLineNrColorVisual()我会立即立即激活任何视觉模式吗?

Basically that works but not instantly since the function is triggered on CursorMoved. How could I fire CursorLineNrColorVisual() instantly as soon as I would activate any visual mode?

推荐答案

:help 我以以下设置结束:

" Colorize line numbers in insert and visual modes
" ------------------------------------------------
function! SetCursorLineNrColorInsert(mode)
    " Insert mode: blue
    if a:mode == "i"
        highlight CursorLineNr ctermfg=4 guifg=#268bd2

    " Replace mode: red
    elseif a:mode == "r"
        highlight CursorLineNr ctermfg=1 guifg=#dc322f

    endif
endfunction


function! SetCursorLineNrColorVisual()
    set updatetime=0

    " Visual mode: orange
    highlight CursorLineNr cterm=none ctermfg=9 guifg=#cb4b16
endfunction


function! ResetCursorLineNrColor()
    set updatetime=4000
    highlight CursorLineNr cterm=none ctermfg=0 guifg=#073642
endfunction


vnoremap <silent> <expr> <SID>SetCursorLineNrColorVisual SetCursorLineNrColorVisual()
nnoremap <silent> <script> v v<SID>SetCursorLineNrColorVisual
nnoremap <silent> <script> V V<SID>SetCursorLineNrColorVisual
nnoremap <silent> <script> <C-v> <C-v><SID>SetCursorLineNrColorVisual


augroup CursorLineNrColorSwap
    autocmd!
    autocmd InsertEnter * call SetCursorLineNrColorInsert(v:insertmode)
    autocmd InsertLeave * call ResetCursorLineNrColor()
    autocmd CursorHold * call ResetCursorLineNrColor()
augroup END

为了在退出可视模式后恢复行号的颜色必须执行以下步骤:

In order to restore the color of line numbers after leaving the visual mode I had to do the following steps:


  1. 重新映射相关的键绑定以调用输入视觉功能

  2. 进入可视模式时,该功能为 CursorHold 事件
  3. $ b $设置 updatetime = 0 b
  4. 通过 autocmd CursorHold

  5. 调用离开视觉功能而离开视觉模式时,该功能重置 updatetime = 4000 用于 CursorHold 事件

  1. Remap relevant key bindings to call an "enter-visual-function"
  2. While entering visual mode the function sets updatetime=0 for CursorHold events
  3. Call a "leave-visual-function" by autocmd CursorHold
  4. While leaving visual mode the function resets updatetime=4000 for CursorHold events

这篇关于当vim进入可视模式时运行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆