9

Below example, first line is shorter than the other lines.
I want to insert \ to all lines at the highlighted place.
enter image description here

If I press CtrlC, jjj(select all lines) , Shifti, \, only first line changed.

enter image description here

How to get below result?
enter image description here

Notice: Don't want to use :s substitute command.

1
  • :h v_b_A if you want to have them aligned after the physical line end, you might need to :set virtualedit Dec 6, 2018 at 11:44

3 Answers 3

9

It could be done with virtualedit=ALL:

  1. set virtualedit=all
  2. select visually vertical part of the text
  3. A\<ESC>
  4. return set virtualedit=block

See the screen:

enter image description here

3
  • This also works with virtualedit=block. Start visual block mode at the end of the first line. Then move down to the last line. Finally move to the right as far as you want. Then enter A\<ESC>. No need to change virtualedit setting.
    – Ralf
    Dec 6, 2018 at 19:50
  • 4
    It's interesting that at Step 3, A\<ESC> works but I\<ESC> (with capital i) doesn't. Unlike in the visual block without virtual edit. May 24, 2020 at 14:03
  • Neither A or I at the Step 3 works for me. Only thing worked so far was r "replace" command. Please refer to accepted answer in the link stackoverflow.com/questions/61468655/…
    – caltuntas
    Feb 17 at 8:03
6

The short answer is: In Visual Block mode select lines to their ends ($) and use A to append some text at the end of each line.

The longer answer:

  • gg to go the beginning of the first line.
  • CTRL-V (or the proper alternative CTRL-Q in e.g. gVim) to start blockwise visual selection.
  • jjj$ select all four lines down (jjj) and to the right until their individual ends ($).
  • A start appending some text ('\' in your case).
  • ESC

In fact this is not exactly your desired result, but functually equal and achievable with a minimum effort and time:

short line \
very long line \
very very long line \
short line \
4
  • 1
    I like this answer because it doesn’t require the fancy alignment: in my experience, alignment of line continuations is too much effort to maintain.
    – D. Ben Knoble
    Dec 6, 2018 at 19:32
  • I came here looking for this answer. Thanks!
    – mrbolichi
    Jul 18, 2022 at 8:17
  • Late to the party, but any reason this only works with visual blockwise selection (i.e <kbd>C-v</kbd>) but not visual (<kbd>v</kbd>) or visual line (<kbd>Shift v</kbd>)
    – mrbolichi
    Jul 22, 2022 at 6:53
  • Only in blockwise selection mode you can use A to append in each line instead of the current line only.
    – salchint
    Jul 23, 2022 at 15:39
2

This is a nice technique. Thanks Maxim Kim.

This description may be helpful. All codes are literal key entries except:

  • C-v for [control][v] keys combination
  • ESC for [escape] key

To insert (spaces that end with char: #) on a vertical line, with cursor anywhere on the first line:

:set ve=all         . enable full screen block cursor access
$                   . cursor to top right position for vertical line start
C-v                 . enable select (vertical line) mode
8j                  . move cursor down 8 lines (selects a vertical line)
s#ESC               . substitute # char in highlighted area, actuate by escape key
:set ve=            . restore default mode (mine is unset, could be: block)

notes: can also append a char or word using an uppercase key append: A, or use insert: I

Your Answer

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

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