2

If i've got the code

hello 
hellllo
hello

i can insert code before or after all of the lines by using I or A in visual block mode, respectively. How can I delete the last x characters? I'm aware that I can do it with s/o// (where o would require more specific text realistically) but isn't there visual block command?

1
  • do you want to delete just the os or o, llo, o?
    – Mass
    Jul 31, 2018 at 22:57

2 Answers 2

5

One way would be to select the lines (using any of the visual modes: block, line, or regular characterwise) and then type the command:

:norm! $xxx

Vim will pre-populate the command line with a range when you press : so the actual command you will execute is:

:'<,'>norm! $xxx

This acts like visiting each line and typing $xxx. (See :help :normal.)

Note that, because of the way the x command handles a range, :norm!$3x won't work. (However, if your virtualedit setting contains onemore or all then you can use a count with a command like :norm!$ld3h: the original command given won't work.)

2
  • Very nice, this works like a charm for deleting multiple single-line HTML comments with :norm!04x$3X
    – DystD
    Feb 21 at 19:29
  • (counting <!-- comment --> spaces, :norm!05x$4X)
    – DystD
    Feb 21 at 20:00
4

You can use x and d in visual (block, line) mode. They will delete exactly the selected text. This means you can normally not do things like "delete the last three chars of all selected line" (unless all the lines are the same length, then you can with visual block mode).

But according to How do you change the last n characters for a range of lines? there is a trick: Use :right to first right align the text, remove your chars with visual block mode and the :left align the text again.

2
  • TIL about the right and last trick, amazing. thank you
    – volk
    Aug 10, 2022 at 15:44
  • 1
    I tried and some lines weren't aligned. Reading :h right, it was because :right aligns them at column 80 by default, which will leave longer lines unaligned. That can be changed by adding the desired width after it (e.g., :right 120).
    – DystD
    Feb 21 at 19:20

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.