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?

3
  • 2
    You could try block deletion or some plugin specific for comments.
    – mMontu
    Nov 21, 2016 at 14:44
  • I would go with a :s command Nov 21, 2016 at 16:37
  • 1
    When on the first "#", do ^v54jx (ctrl+v then 54jx), i.e. make a visual block on current column on all 54 lanes and delete it. Similarly, you can revert this with ^v54I#<ESC> (or even gvI#<ESC> [gv = reopen previous visual mode])
    – Rolf
    Dec 1, 2016 at 7:53

3 Answers 3

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:

3
  • 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, 2016 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, 2016 at 16:03
  • 1
    Okay, I'll bear that in mind in the future.
    – Rich
    Nov 21, 2016 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.

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 🙏

2
  • 2
    Ix in visual block mode just inserts and x at the beginning of all lines in the selection. Perhaps you meant to suggest typing x or d in visual block mode?
    – D. Ben Knoble
    May 7, 2021 at 21:03
  • Edited my answer, Thank You @D.BenKnoble May 8, 2021 at 19:46

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.