Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 551
LVB Avatar asked Sep 27 '10 18:09

LVB


People also ask

How do I yank a block in vim?

You can copy a block of text by pressing Ctrl-v (or Ctrl-q if you use Ctrl-v for paste), then moving the cursor to select, and pressing y to yank. Now you can move elsewhere and press p to paste the text after the cursor (or P to paste before).

How do I yank a line in Vim?

To yank one line, position the cursor anywhere on the line and type yy . Now move the cursor to the line above where you want the yanked line to be put (copied), and type p . A copy of the yanked line will appear in a new line below the cursor.

How to move line up and down Vim?

Mappings to move lines In normal mode or in insert mode, press Alt-j to move the current line down, or press Alt-k to move the current line up.

How to select a whole line in Vim?

If you want to select the entire line in a file, press V. Now when you press k or j to go up and down, vim will select the entire line above and below your cursor. Finally, you can select text in columns by pressing ctrl+v and moving up or down the block.


2 Answers

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}  
like image 106
zigdon Avatar answered Sep 17 '22 03:09

zigdon


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.

like image 45
Greg Hewgill Avatar answered Sep 18 '22 03:09

Greg Hewgill