Vim-拖动到标记时防止光标移动 [英] Vim - Prevent cursor from moving when yanking to a mark

查看:61
本文介绍了Vim-拖动到标记时防止光标移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常使用标记在Vim中进行剪切/粘贴。

I typically use marks to cut/paste in Vim.

要在第4行和第12行之间插入文本,请执行以下操作:

To yank the text between lines 4 and 12, I do the following:


  1. 将光标移至第4行

  2. 键入 mx 放置一个标记为x

  3. 将光标移至第12行

  4. 键入 y'x 即可第4行和第12行之间的文本

  1. move cursor to line 4
  2. type mx to place a mark named x
  3. move cursor to line 12
  4. type y'x to yank the text between lines 4 and 12

完成此操作后,光标移至第4行。

After doing this, the cursor moves back to line #4.

有没有一种方法可以使光标停留在原位置(不移回标记)?

Is there a way to make the cursor stay where it is (without moving back to the mark)?

如果有人有更好的方法来做同样的事情,那也很棒...

If anybody has a better ways to do the same thing, that would be great, too...

预先感谢!

更新:

我使用FDinoff的答案来创建一个映射,正在使我成为一个快乐的露营者:

I used FDinoff's answer to create a mapping that is making me one happy camper:

nnoremap YY y'x<C-O>

此操作从光标移至名为x的标记,然后将光标返回至其所在位置。

This yanks from the cursor to the mark named x, then returns the cursor to where it was.

这已经节省了我的时间。再次感谢!

This has already saved me tons of time. Thanks again!

推荐答案

跳到第4行的原因是因为您使用了向后运动的yank。

The reason you jump to line 4 is because you are using yank with a backwards motion.

                            *y* *yank*
["x]y{motion}       Yank {motion} text [into register x].  When no
            characters are to be yanked (e.g., "y0" in column 1),
            this is an error when 'cpoptions' includes the 'E'
            flag.

有问题的议案是 'x ,该标记跳转到标记x。光标移到被加样部分的开头,在本例中为第4行,因为您从第12行退出。

The motion in question is 'x which is jump to mark x. The cursor is moved to the beginning of the yanked part which in this case is line 4 since you were yanking from line 12.

但是您可以做些什么。


  1. 使用 range 作为ex命令行模式拉动。范围为(发生nt行)到’x (标记x)。如果范围向后与会问你是不是你的意思。这不会移动光标。 :。,'xy

  1. Use a range for an ex command line mode yank. The range is . (current line) to 'x (mark x). If the range is backwards with will ask you is you meant the other direction. This won't move your cursor. :.,'xy

或者您可以使用 < C-o> 将使您跳回您跳出的最后一个地方。 (在注释中提到。)

Or you could use <C-o> will will jump you back to the last place you jumped from. (which was mentioned in the comments.)

或者您可以使用 '] `] 。这些命令将使您跳出最后一个选中文本的最后一个字符。

Or you could use '] or `]. These commands will jump you the last character of the the last yanked text.

这篇关于Vim-拖动到标记时防止光标移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆