vim同一按键不同命令

原文

function! Jump(jumpcommands)
  let l:location = getcurpos('.')
  for jumpkey in a:jumpcommands
    execute "normal! " jumpkey
    if l:location != getcurpos('.')
      break
    endif
  endfor
endfunction

function! Jumptails()
  call Jump(['$', 'L$', 'G'])
endfunction

function! Jumpheads()
  call Jump(['0', 'H', 'gg'])
endfunction

inoremap <silent> <End> <Esc>  :call Jumptails()<CR> i
nnoremap <silent> <End>        :call Jumptails()<CR>
inoremap <silent> <Pos1> <Esc> :call Jumpheads()<CR> i
nnoremap <silent> <Pos1>       :call Jumpheads()<CR>

不跟踪最后命令而只检查光标位置.
它通过存储当前列来"检查"当前位置,然后只执行所需移动之一,比较新列是否等于之前列,如果是,则执行下一个移动,等等.
Jumpheads()功能使用<Pos1>键在相反方向(向上)执行相同操作.如果vim中存在这样的命令,还可添加类似跳转段落或函数结尾/开头.
副作用是,如果在两者之间做其他事情,就会打破循环.此外,它非常简单;)

posted @ 2022-08-12 15:09  zjh6  阅读(17)  评论(0)    收藏  举报  来源