[英]vim - Run ex command to go to line from function in normal mode
我有一个 function 设置在 InsertEnter autocmd 上运行。 在 function 我想跳转到特定的行。
所以到目前为止我尝试过的(简化的)是这样的:
function JumpToLineBeforeScroll()
echom "function runs"
exe ":10"
return ""
endfunction
autocmd InsertEnter * call JumpToLineBeforeScroll() | set cul
我使用mode()
验证了当这个 function 运行时它处于正常模式(如果错误请纠正我)。 Function 运行但跳线不起作用。 我如何让它工作?
注意:我实际上想要实现的是在插入模式下鼠标滚动,切换到正常模式,保存当前行号然后滚动。 然后,一旦我按i
再次进入插入模式( InsertEnter
),我想跳回到滚动之前所在的最后一行。 因为这需要一些条件,所以我想将其保留在 function 中,而不是在autocmd
行本身中编写整个代码。
谢谢!
所以gi
存在:)
现在,在了解gi
并了解:normal
的工作原理之后,我已经达到了这个非常适合我的用例的配置。 事实证明,这实际上并不需要autocmd
,因为:startinsert
也存在:)
" Variable to track scrolling
let didScroll = 0
" Function to track if scrolling happened
function ScrollHandler()
let g:didScroll = 1
return ""
endfunction
" Function to jump back to line before scrolling started
function JumpToLineBeforeScroll()
if g:didScroll > 0
let g:didScroll = 0
:normal gi
endif
" Needed as :normal ends with an <Esc> and so comes back to ex mode
startinsert
return ""
endfunction
" Map mouse scroll wheel to only scroll the screen
nnoremap <ScrollWheelUp> <Esc>:call ScrollHandler()<CR><C-y>
nnoremap <ScrollWheelDown> <Esc>:call ScrollHandler()<CR><C-e>
inoremap <ScrollWheelUp> <Esc>:call ScrollHandler()<CR><C-y>
inoremap <ScrollWheelDown> <Esc>:call ScrollHandler()<CR><C-e>
" Normal mode mapping for i which performs gi to jump to last insertmode line if scroll has taken place
nnoremap i <Esc>:call JumpToLineBeforeScroll()<CR>
我仍在使用didScroll
,因为我并不总是想在按下i
时执行gi
操作。 如果那是设置,那么在正常模式下的任何导航都将毫无意义,因为在输入后插入时我会回到同一行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.