3

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?

  • 2
    You could try block deletion or some plugin specific for comments. – mMontu Nov 21 '16 at 14:44
  • I would go with a :s command – Christian Brabandt Nov 21 '16 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 '16 at 7:53
8

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:

  • 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
  • 1
    @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
7

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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