5

In Normal mode I use the following combination to edit multiple lines:

  1. Ctrl + v
  2. Select lines to edit
  3. Shift + i
  4. Edit line
  5. Esc

However, this method only applies changes to the first line if character deletion is not involved. Uncommenting a long block of code can thus become a small headache.

What is this method missing for multi-line deletion? Or should a different method be applied.

Luís de Sousa
  • 323
  • 3
  • 16
  • What do you mean by "character deletion"? Do you mean using backspace? Why not use Ctrl+V to select the comment characters and then use x to delete that block? Also take a look at plug-ins such as vim-commentary which should make your life much easier! – filbranden Jun 02 '20 at 19:42
  • If you want to delete the characters you selected, you can use s to substitute your selection, then type in what you need. But as for commenting, I'd advocate vim-commentary as well. – Biggybi Jun 03 '20 at 03:05
  • 1
    I think your question is a duplicate of this one. However I had never noticed the behavior you mention before but it seems to indicate that you are using the insert mode for the wrong kind of operations (i.e. anything else than inserting new characters) – statox Jun 03 '20 at 08:19
  • @statox, I don't think that is the same question. I am trying to find a method for multi-line deletion, not only uncommenting. – Luís de Sousa Jun 03 '20 at 08:22
  • @LuísdeSousa Then some of the methods in the answers of the question I linked still apply. I think the answer to your question is something along the lines of in visual block selection use I and A only to insert new characters (and use BS during your edit if you inserted a wrong character), otherwise to delete characters in visual block, select the characters you want to delete and use x or d instead. However I'm not 100% sure why the backspace deletion doesn't work on all lines so I'll let someone dig into that and post an answer :) – statox Jun 03 '20 at 08:30
  • 1
    @filbranden Your reply is close to an answer. The problem is that each I delete a character the block selection is reset and I need to select the block again. – Luís de Sousa Jun 04 '20 at 08:08
  • @LuísdeSousa You can use gv to easily select the same visual selection again, if that helps... – filbranden Jun 04 '20 at 13:59
  • 1
    @LuísdeSousa Your question is still unclear. Can you give us an example of initial text and final text so we can understand what exactly you're trying to accomplish? Please [edit] the question to include an example. Make sure you include "before" and "after". If you can, also include the sequence of steps you're using to accomplish that edit. Thanks! – filbranden Jun 04 '20 at 14:02
  • @filbranden Uff... I can actually accomplish what I wish following your very first comment. I now realise that more than one column can be selected with Ctrl + `v'. Could you please add an answer bellow? – Luís de Sousa Jun 04 '20 at 17:45
  • @LuísdeSousa Sorry for the delay! I have finally added this as an answer. Please take a look and see if that addresses your issue, let me know if it needs any adjustments, I'd be happy to revise it with your feedback. Cheers! – filbranden Jun 05 '20 at 20:17

1 Answers1

5

It seems you're using the backspace key in insert mode to delete the comments. As you found out, this will not really work at all in visual block mode.

Instead, you can simply use the x or d commands to delete a visual selection. See :help v_x.

If you want to delete multiple comment characters, you can make your visual block take up "n" columns to cover all the comment characters.

For example, if your comments are // and you have a blank space after the comments (so 3 characters total), then you can:

  1. Move to the // on the first line where you want comments removed.
  2. Use Ctrl+V to enter Visual Block mode.
  3. Move to the // on the last line where you want comments removed. You'll have a n x 1 block, with "n" lines and one column.
  4. Move to the blank right after the //, with a command such as 2l or fSpace, now you'll have a n x 3 visual block.
  5. Use x or d. This will remove the contents of the visual block, with the comments.

You might also want to consider adopting a plug-in to help inserting, removing or toggling comments. I can recommend vim-commentary which does a great job. This (and similar plug-ins) are typically also able to figure out which style of comments you need for the language you're using, so they typically require zero configuration other than having your plug-in manager install and enable them.

filbranden
  • 28,785
  • 3
  • 26
  • 71