在 VIM 中猛拉某个区域而不将光标移动到块的顶部?

发布于 2024-09-24 23:46:53 字数 349 浏览 4 评论 0原文

是否有一种简单的方法(即无需编写脚本或复杂的键盘映射序列)来 Yank 一组行并将光标保留在执行 Yank 的位置(而不是在块的开头)?

根据 VIM 的帮助:“请注意,在按字符复制命令之后,Vim 会将光标保留在距离缓冲区开头最近的第一个复制字符上。”逐行似乎表现类似。

这对我来说有点烦人,因为我倾向于从上到下选择一个大区域,进行猛拉,然后粘贴到所选区域底部附近或下方。今天,我在 Yank 之前设置了一个标记 (mx),然后跳回来,但我怀疑可能有一个不同的 Yank 序列可以满足我的需要。

我已经在 SO 和网络上搜索过很多次了。有太多现有的“VIM 快捷方式”材料需要翻阅,但我还没有找到解决方案。

提前致谢。

Is there a simple way (i.e. without writing a script or elaborate keymap sequence) to Yank a group of lines and leave the cursor wherever the Yank was performed, as opposed to at the start of the block?

According to VIM's help: "Note that after a characterwise yank command, Vim leaves the cursor on the first yanked character that is closest to the start of the buffer." Line-wise seems to behave similarly.

This is a bit annoying for me since I tend to select a large region from top to bottom, Yank, and then paste near or below the bottom of the selected region. Today I'm setting a mark (m-x) just before Yank and then jumping back, but I suspect there may be a different Yank sequence that will do what I need.

I've searched SO and the web for this numerous times. There is so much existing "VIM shortcuts" material to wade through yet I've not found a solution to this one yet.

Thanks in advance.

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

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

发布评论

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

评论(8

仅此而已 2024-10-01 23:46:53

不太回答您的问题,但也许 '] 可以解决您的问题?

 ']  `]         To the last character of the previously changed or
                yanked text.  {not in Vi} 

Not quite answering your question, but perhaps '] would solve your problem?

 ']  `]         To the last character of the previously changed or
                yanked text.  {not in Vi} 
婴鹅 2024-10-01 23:46:53

如果您使用可视块 (v),那么在拉出该块后,您可以使用 gv 重新选择同一块(这也会将光标位置移回到猛拉之前的位置)。如果随后按 Esc 键,则无需移动光标即可取消选择该块。

同样令人感兴趣的可能是可视块模式下的 ctrl-o 命令,该命令在所选块的开头和结尾之间跳转。

If you're using visual blocks (v), then after yanking the block you can use gv to re-select the same block (which also moves your cursor position back to where it was before yanking). If you then press Esc, the block is un-selected without moving the cursor.

Also of interest might be the ctrl-o command in visual block mode, which jumps between the start and end of the selected block.

灯角 2024-10-01 23:46:53

如果在视觉模式下拉动,您还可以使用 '>`> 转到刚刚拉动的视觉选择的最后一行/字符。在这种情况下,这本质上与 ']`] 相同,这显然在 VsVim 等中不受支持。

If yanking in visual mode, you could also use '> or `> to go to the last line/character of the just yanked visual selection. In this context this is essentially the same as '] and `] which is apparently not supported e.g. in VsVim.

姐不稀罕 2024-10-01 23:46:53

:y3 将从当前当前行中拉出三整行,如果您确切知道要拉动多少行,这个命令非常方便。 :help :yank 了解详细信息。

:%y 将选择整个缓冲区而不移动光标,
ggvG$y一样,没有选择突出显示的闪烁和修改"*寄存器。

我使用这个插入模式映射:

function! SelectAll()
  %y*
endfun
imap <expr> <F3> SelectAll()

ps:如果你喜欢; 要粘贴(在 vim 之外),请使用 %y+

检查 https://stackoverflow.com /a/1620030/2247746

:y3 will yank three whole lines from current current line, If you know exactly how many line to yank, this command is very handy. :help :yank for details.

:%y will select the whole buffer without moving the cursor,
like ggvG$y, without the flash of selection highlight and modifying the "* register.

I use this insert mode map:

function! SelectAll()
  %y*
endfun
imap <expr> <F3> SelectAll()

ps: if you prefer <C-V> to paste(outside vim), use %y+

check https://stackoverflow.com/a/1620030/2247746

神经大条 2024-10-01 23:46:53

我不确定自 vmap ygv 解决方案发布以来 YankRing 是否发生了变化,但在将其添加到我的 .vimrc 后,这种情况并没有持续存在。 YankRing 实际上覆盖了它。

这是我在 .vimrc 中的解决方案。

function! YRRunAfterMaps()
  vmap y ygv<Esc>
endfunction

I'm not sure sure if YankRing has changed since the vmap ygv<Esc> solution was posted but that didn't persist for me after adding it to my .vimrc. YankRing actually overwrote it.

Here's my solution in .vimrc.

function! YRRunAfterMaps()
  vmap y ygv<Esc>
endfunction
画▽骨i 2024-10-01 23:46:53

我不知道同时发生了什么,但在 IdeaVim 中,接受的答案并不像我希望的那样工作。 (不要将光标移到猛拉上)

对我来说,诀窍就是在猛拉之前设置一个标记,然后再进行猛拉。

vmap y mxy`x

I don't know what happened in the meantime but in IdeaVim the accepted answers don't work as I'd like them to. (Don't move the cursor on yank)

For me what did the trick was just setting a mark before yank and go to that afterwards.

vmap y mxy`x
稳稳的幸福 2024-10-01 23:46:53

如果您使用 Lua,您可以定义此键绑定,这将拉动并返回到拉动之前的位置:

vim.api.nvim_set_keymap("x", "y", "ygv<Esc>", { noremap = true, silent = true })

If you are using Lua, you can define this keybinding, this will yank and go back to position before yanking:

vim.api.nvim_set_keymap("x", "y", "ygv<Esc>", { noremap = true, silent = true })
七色彩虹 2024-10-01 23:46:53

使用 ModeChanged 事件和 vim.v.operator 变量。

local api = vim.api
local autocmd = api.nvim_create_autocmd
local augroup = api.nvim_create_augroup

local g = augroup("user/keep_yank_position", { clear = true })

autocmd("ModeChanged", {
    pattern = { "n:no", "no:n" },
    group = g,
    callback = function(ev)
        if vim.v.operator == "y" then
            if ev.match == "n:no" then
                vim.b.user_yank_last_pos = vim.fn.getpos(".")
            else
                if vim.b.user_yank_last_pos then
                    vim.fn.setpos(".", vim.b.user_yank_last_pos)
                    vim.b.user_yank_last_pos = nil
                end
            end
        end
    end,
})

autocmd("ModeChanged", {
    pattern = {
        "V:n",
        "n:V",
        "v:n",
        "n:v",
    },
    group = g,
    callback = function(ev)
        local match = ev.match
        if vim.tbl_contains({ "n:V", "n:v" }, match) then
            -- vim.b.user_yank_last_pos = vim.fn.getpos(".")
            vim.b.user_yank_last_pos = vim.api.nvim_win_get_cursor(0)
        else
            -- if vim.tbl_contains({ "V:n", "v:n" }, match) then
            if vim.v.operator == "y" then
                local last_pos = vim.b.user_yank_last_pos
                if last_pos then
                    -- vim.fn.setpos(".", last_pos)
                    vim.api.nvim_win_set_cursor(0, last_pos)
                end
            end
            vim.b.user_yank_last_pos = nil
            -- end
        end
    end,
})

Use the ModeChanged event and vim.v.operator variable.

local api = vim.api
local autocmd = api.nvim_create_autocmd
local augroup = api.nvim_create_augroup

local g = augroup("user/keep_yank_position", { clear = true })

autocmd("ModeChanged", {
    pattern = { "n:no", "no:n" },
    group = g,
    callback = function(ev)
        if vim.v.operator == "y" then
            if ev.match == "n:no" then
                vim.b.user_yank_last_pos = vim.fn.getpos(".")
            else
                if vim.b.user_yank_last_pos then
                    vim.fn.setpos(".", vim.b.user_yank_last_pos)
                    vim.b.user_yank_last_pos = nil
                end
            end
        end
    end,
})

autocmd("ModeChanged", {
    pattern = {
        "V:n",
        "n:V",
        "v:n",
        "n:v",
    },
    group = g,
    callback = function(ev)
        local match = ev.match
        if vim.tbl_contains({ "n:V", "n:v" }, match) then
            -- vim.b.user_yank_last_pos = vim.fn.getpos(".")
            vim.b.user_yank_last_pos = vim.api.nvim_win_get_cursor(0)
        else
            -- if vim.tbl_contains({ "V:n", "v:n" }, match) then
            if vim.v.operator == "y" then
                local last_pos = vim.b.user_yank_last_pos
                if last_pos then
                    -- vim.fn.setpos(".", last_pos)
                    vim.api.nvim_win_set_cursor(0, last_pos)
                end
            end
            vim.b.user_yank_last_pos = nil
            -- end
        end
    end,
})

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