Vim - Prevent cursor from moving when yanking to a mark

别来无恙 提交于 2019-12-04 13:00:25

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.

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.

However things you could do.

  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

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

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

Jump back with: `` or <c-o>

For more help see:

:h ``
:h CTRL-O

How about y12G?
This will yank from current position (line 4) to line 12.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!