7

In vim I can use for instance 10j to go 10 lines down. And I can use . to repeat the last deletion.

Now, in bash script I have many commented lines like this:

# ...
# ...
# ...
# ...
# ...
# ...
# ...
# ...
# ...
etc...

Say, there are 52 such lines. Is there a way to combine moving 52j and repeating the deletion of the # via x and delete 52 lines at once?

statox
  • 49,782
  • 19
  • 148
  • 225
St.Antario
  • 1,117
  • 3
  • 14
  • 27

3 Answers3

11

I do approve @mMontu comment suggesting to use a comment plugin (vim-commentary is an option but NERDCommenter has my preference over vim-commentary).

But you could do it in other ways:

  • First if all your # are aligned on the same column you can block select them with ctrl+v52j and then delete your selection with x.

  • If they are not aligned you could use a macro:

    qq^xjq
    
    qq        Record a macro in the register q
    ^         Go to beginning of line
    x         Suppress the first character
    j         Go one line down
    q         Stop recording
    

    You can then repeat your macro as many time as necessary with 52@q

  • And with NERDCommenter:

    • Select your 52 lines with v52j
    • Use the NERDCommenter mapping to uncomment your lines: leadercu

For more details:

statox
  • 49,782
  • 19
  • 148
  • 225
  • I know it's already linked in the comment, but maybe a link to vim-commentary would be nice? I'd add it myself but it seems rude! – Rich Nov 21 '16 at 15:57
  • 2
    @Rich I didn't add it because it is pretty easy to find and has already been referenced often on this site but it is now corrected. Also I really don't mind user editing my answer even with minor correction (when they are meaningful) that's how community driven site should work ;-) – statox Nov 21 '16 at 16:03
  • 1
    Okay, I'll bear that in mind in the future. – Rich Nov 21 '16 at 16:06
10

I like the approaches statox suggested. Here's another one:

52:norm x

This only works if the # is the first character on each line. Otherwise, I would do

52:s/#

These two work very similarly. Essentially, what <count>: is doing, is setting up a range so that the next ex command is applied to the next <count> lines. norm x means

Press 'x' as if it was in normal mode

which would obviously remove the first character on that line.

s/# is just shorthand for :substitute/#// which will remove the first # character on each line.

DJMcMayhem
  • 17,581
  • 5
  • 53
  • 85
0

Having a space with the commenting character?

Assumptions : commented lines are continuous and commenting character (# in your case) is at beginning of all those lines.

  1. Come to the beginning of the first commented line.
  2. Press Ctrl+v switches to visual-block mode.
  3. Type 52j selects beginning of next 52 lines.
  4. Use h / l or / respectively selecting multiple columns.
  5. Press x deletes the selection.
  6. Press Esc back to normal mode.

References: What's a quick way to comment/uncomment lines in Vim?

Namaste

Deepam Gupta
  • 131
  • 6