vim - 运行 ex 命令以在正常模式下从函数转到行

发布于 2025-01-10 20:50:25 字数 551 浏览 3 评论 0原文

我有一个在 InsertEnter autocmd 上运行的函数设置。在函数中我想跳到特定行。

所以到目前为止我已经尝试过(简化)的是:

function JumpToLineBeforeScroll()
    echom "function runs"
    exe ":10"
    return ""
endfunction

autocmd InsertEnter * call JumpToLineBeforeScroll() | set cul

我使用 mode() 验证了该函数运行时它处于正常模式(如果错误,请纠正我)。函数运行但行跳转不起作用。我该如何让它发挥作用?

注意:我实际上想要实现的是在插入模式下鼠标滚动,切换到正常模式,保存当前行号,然后滚动。然后,一旦我按 i 再次进入插入模式 (InsertEnter),我想跳回到滚动之前的最后一行。因为这需要一些条件,所以我想将其保留在函数中,而不是在 autocmd 行本身中编写整个代码。

谢谢!

I have a function setup to run on InsertEnter autocmd. In the function I want to jump to a specific line.

So what I have tried (simplified) is this so far:

function JumpToLineBeforeScroll()
    echom "function runs"
    exe ":10"
    return ""
endfunction

autocmd InsertEnter * call JumpToLineBeforeScroll() | set cul

I verified using mode() that while this function runs it is in normal mode (pls correct me if wrong). Function runs but the line jump is not working. How do I get this to work ?

Note: What I am actually trying to achieve is on mouse scroll while in insert mode, switch to normal mode, save the current line number and then scroll. Then once I press i to enter insert mode again (InsertEnter) I want to jump back to the last line I was on before scroll. As this would need some conditionals I want to keep this in a function instead of writing the entire code in the autocmd line itself.

Thanks!

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

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

发布评论

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

评论(1

瑕疵 2025-01-17 20:50:25

所以 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 操作。如果这是设置,则正常模式下的任何导航都将毫无意义,因为当输入返回插入时我将返回到同一行。

So gi exists :)

Now after learning about gi and realising how :normal works, I've reached this config which works for my usecase perfectly. And turns out autocmd is not actually needed for this as :startinsert also exists :)

" 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>

I'm still using didScroll since I dont always want to do a gi whenever I press i. If that's the setup any navigation in normal mode would be pointless as I would be back to same line when enter back insert.

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