切换到另一个选项卡时从插入模式更改为正常模式?

发布于 2024-11-18 19:21:21 字数 151 浏览 1 评论 0原文

假设我有多个选项卡,分屏中有多个缓冲区。

当我在一个缓冲区中处于编辑模式并切换到另一个选项卡(ctrl-pageDown)时,我仍处于插入模式。

有没有办法在更改选项卡时自动切换到正常模式?

更好的是,返回原始缓冲区时是否可以返回插入模式?

Say I have multiple tabs with multiple buffers in split screens.

When I am in edit mode in one buffer and switch to another tab (ctrl-pageDown), I am still in insert mode.

Is there a way to automatically switch to normal mode when changing tabs ?

Even better, is it possible to return to insert mode when coming back to the original buffer ?

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

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

发布评论

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

评论(3

凉宸 2024-11-25 19:21:21

您可以尝试添加一些非常简单的内容,例如

autocmd TabEnter * stopinsert

到您的 .vimrc 中。

You could try adding something very simple like

autocmd TabEnter * stopinsert

to your .vimrc.

夜清冷一曲。 2024-11-25 19:21:21

BufLeave 中,您可以调用一个函数来检查您所处的模式并设置缓冲区变量,然后在 BufEnter 中检查它是否存在并进入该模式。

请参阅有关 mode()b:var 的帮助。

这是 .vimrc 的一些示例。我刚刚为此目的写了它,我自己也开始使用它,我认为它会很有用。

au BufLeave * call ModeSelectBufLeave()
au BufEnter * call ModeSelectBufEnter()

function! ModeSelectBufLeave()
    let b:mode_select_mode = mode()
    " A more complex addition you could make: if mode() == v, V, <C-V>, s, S, or <C-S>, store the selection and restore it in ModeSelectBufEnter
endfunction

function! ModeSelectBufEnter()
    let l:mode = mode()
    stopinsert  " First, go into normal mode
    if (l:mode == "i" || l:mode == "R" || l:mode == "Rv") && 
    \       (!exists('b:mode_select_mode') ||
    \       b:mode_select_mode == "n" ||
    \       b:mode_select_mode == "v" ||
    \       b:mode_select_mode == "V" ||
    \       b:mode_select_mode == "\<C-V>" ||
    \       b:mode_select_mode == "s" ||
    \       b:mode_select_mode == "S" ||
    \       b:mode_select_mode == "\<C-S>")
        normal l
        " Compensate for the left cursor shift in stopinsert if going from an
        " insert mode to a normal mode
    endif
    if !exists('b:mode_select_mode')
        return
    elseif b:mode_select_mode == "i"
        startinsert
    elseif b:mode_select_mode == "R"
        startreplace
    elseif b:mode_select_mode == "Rv"
        startgreplace
    endif
endfunction

In BufLeave you could call a function which would check what mode you're in and set a buffer variable and then in BufEnter check if it exists and go to that mode.

See help on mode(), b:var.

Here is some sample stuff for .vimrc. Having written it just now for this purpose, I've started using it myself and I think it'll be useful.

au BufLeave * call ModeSelectBufLeave()
au BufEnter * call ModeSelectBufEnter()

function! ModeSelectBufLeave()
    let b:mode_select_mode = mode()
    " A more complex addition you could make: if mode() == v, V, <C-V>, s, S, or <C-S>, store the selection and restore it in ModeSelectBufEnter
endfunction

function! ModeSelectBufEnter()
    let l:mode = mode()
    stopinsert  " First, go into normal mode
    if (l:mode == "i" || l:mode == "R" || l:mode == "Rv") && 
    \       (!exists('b:mode_select_mode') ||
    \       b:mode_select_mode == "n" ||
    \       b:mode_select_mode == "v" ||
    \       b:mode_select_mode == "V" ||
    \       b:mode_select_mode == "\<C-V>" ||
    \       b:mode_select_mode == "s" ||
    \       b:mode_select_mode == "S" ||
    \       b:mode_select_mode == "\<C-S>")
        normal l
        " Compensate for the left cursor shift in stopinsert if going from an
        " insert mode to a normal mode
    endif
    if !exists('b:mode_select_mode')
        return
    elseif b:mode_select_mode == "i"
        startinsert
    elseif b:mode_select_mode == "R"
        startreplace
    elseif b:mode_select_mode == "Rv"
        startgreplace
    endif
endfunction
温柔嚣张 2024-11-25 19:21:21

我的 .vimrc 中有以下内容:

nmap <C-b> :b#<CR>
imap <C-b> <ESC>:b#<CR>

这可以让我在正常或插入模式下按 Ctrl+b 切换到备用缓冲区,但让我处于正常模式。

至于你的问题,你可以这样做:

imap <C-b> <ESC>:bnext<CR>i

这会让你在插入模式下按 Ctrl+b 并切换到下一个缓冲区,让你在到达那里时进入插入模式。

如果您发现自己在相同的两个缓冲区之间来回切换,我上面的原始映射可能更有用。当然,如果您使用全部三个,则最后一个需要不同的组合键。

I have the following in my .vimrc:

nmap <C-b> :b#<CR>
imap <C-b> <ESC>:b#<CR>

This lets me hit Ctrl+b when in normal or insert mode to switch to the alternate buffer but leaving me in normal mode.

As for your question, you could do this:

imap <C-b> <ESC>:bnext<CR>i

This will let you hit Ctrl+b when in insert mode and switch to the next buffer putting you in insert mode when you get there.

If you find yourself switching back and forth between the same two buffers, my original mappings above may be more useful. Of course if you use all three, you'll need a different key combination for the last one.

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