2

I found a vim tip a while back recommending swapping visual and visual block mode (http://howivim.com/2016/damian-conway/)

For the most part this is really useful. I frequently find myself wanting to only capture a part of a block containing similar text or to delete or insert a bunch of text at the beginning of the line and have it affect every line.

The only thing I'm missing in this setup is the ability to yank all the lines in that the visual block touches. Because Y yanks the whole line in normal mode, I expected it to the same in Visual Block, is there a way to configure it to yank the whole of every line in the visual block selection?

3 Answers 3

8

Maybe this?

xnoremap Y :yank<cr>

Being an ex command, :yank will automatically copy whole lines.

1
  • +1 for simplicity. A lot better than my solution.
    – DJMcMayhem
    Commented Jun 1, 2016 at 0:41
2

Well, this isn't the perfect solution, since it requires using an extra register, but it works. Try this:

vnoremap Y <esc>:let @a=""<cr>gv:norm "AY<cr>

Explanation:

<esc>:let @a=""<cr>

Escape, and clear register 'a'.

gv

Reselect our last visual block.

:norm 

Apply the following keystrokes to every line in the visual selection:

"AY<cr>

Yank the whole line, and append it to register 'a'.

2

I'm going to add this as another answer, because it's a very different (and in my opinion better) approach than my other answer. I would do this:

xnoremap Y Vy

V enters "visual line mode" where the selection is done linewise instead of character wise, and then y is obviously yank.

1
  • What happens if the user is already in "visual line mode" (say with V), then tries to use Y mapped as Vy? :) (Basically what happens at VVy)
    – VanLaser
    Commented Jun 15, 2016 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.