开发者

Yank a region in VIM without the cursor moving to the top of the block?

开发者 https://www.devze.com 2023-01-17 15:17 出处:网络
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 bloc

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.

T开发者_如何学Chis 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.


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} 


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.


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.


: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


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

精彩评论

暂无评论...
验证码 换一张
取 消