d11wtq
d11wtq

Reputation: 35308

Vim select the ends of multiple lines (block mode, but where the ending column varies)

Is there any way in vim that I can select the end of all these lines? (I'm only showing the end of the lines in these screenshots).

In block mode I can get them all if the bottom line is longer than the rest, but if the bottom line is shorter, the longer lines are truncated.

EDIT | I guess I can just pad out the bottom line with spaces before I select, then delete the spaces later.

enter image description here enter image description here

Upvotes: 43

Views: 26014

Answers (5)

Yuri Santos
Yuri Santos

Reputation: 178

I don't know if is a new thing. But if you press $ two times (instead one) the block goes to the end of all lines without creating extra spaces).

![enter image description here

Tested on nvim 0.7.2.

Upvotes: 0

FocusedWolf
FocusedWolf

Reputation: 1122

  1. Click somewhere (anywhere) in the first line you wish to append text to.

  2. Press Control + V.

  3. Press Down to create an arbitrary vertical block selection that spans the desired lines.

  4. Press $ to expand the visual block selection to the ends of every line selected.

  5. Press Shift + A to append text to every selected line.

  6. Type the text you want to append.

  7. Press Escape and the text will be appended across the selected lines.

Upvotes: 22

Dana Woodman
Dana Woodman

Reputation: 4522

If you're looking to select the very last character of every line, like if you want to add something after the quotes at the end of each line, you can do the following:

  1. Put your cursor over the very last character (in this example, the last quote on the first line)
  2. Enter block mode: control + V
  3. Move down to select as many lines as you want to change.
  4. Insert at the end of the line: shift + A
  5. Type what you want to add and then exit Visual mode
  6. You text should now be inserted at the end of each selected line!

Hope this is helpful to others like me searching for an answer similar, but not exactly the same, as the above.

Upvotes: 14

echristopherson
echristopherson

Reputation: 7074

Alternately, you can set the virtualedit (:h 'virtualedit') setting so that, any time you're in visual block mode, you can move the cursor around even past the ends of lines. E.g. :set virtualedit=block.

Upvotes: 17

georgebrock
georgebrock

Reputation: 30043

  1. Put your cursor on the top-left character you want to be part of the block.
  2. Enter block selection mode with ctrl+v
  3. Select to the end of the line with $ (this is the step you're missing; if you move to the end of the first line using $ then the selection will extend to the end of subsequent lines as well)
  4. Move down 3 lines with 3j

There's more information in the Vim documentation's section on visual mode which you can read online, or just type :help v_$ in Vim.

Upvotes: 75

Related Questions