4

I will frequently use visual mode and the "norm" command to quickly comment out several lines.

Let's say this is my text (line numbers added for clarity):

1.  #include <iostream>
2.  using namespace std;
3.   
4.  int main()
5.  {
6.     cout << "Hello world!\n";
7.     return 0;
8.  }

And my cursor is on line 6. I hit 'v', 'j', and then

:'<,'>norm 0i//

now my text is:

1.  #include <iostream>
2.  using namespace std;
3.   
4.  int main()
5.  {
6.  //   cout << "Hello world!\n";
7.  //   return 0;
8.  } 

and my cursor is on the second '/' on line 7.

But now, every new line I open starts with '//'. For example, if I hit 'o', and then enter twice, it will look like this:

1.  #include <iostream>
2.  using namespace std;
3.   
4.  int main()
5.  {
6.  //   cout << "Hello world!\n";
7.  //   return 0;
8.  //
9.  //
10. //
11. }

It will also do this with 'A<cr>' and 'O'.

What causes this? Is this a bug? Is there a way to get around it? It's not super annoying, but it does throw me off a little bit. That and I'm really curious at what causes this.

If it matters at all, I am using Ubuntu 15.04 and vim 7.4.488.

  • 1
    +1 for a nicer way to comment blocks than ma + motion + :'a,.s/^/\/\// :) – cxw Jul 23 '15 at 9:50
8

This has nothing todo with the normal command or the visual block. This is due to a setting 'formatoptions', see :help 'formatoptions'. In particular, the mentioned behaviour comes from the flags r and o. If you don't like the behaviour, you can turn it off with :set fo-=ro.

See :help fo-table for the available flags.

| improve this answer | |
  • 2
    Note that :set fo-=ro won't work if you use :set fo=or (different order), you need to use :set fo-=r fo-=o. – Martin Tournoij Jul 23 '15 at 12:33
0

It's because your block includes the start of the next line (because you pressed j).

If you use V (upper case) to start your block, then you'll just have the line you want without pressing j.

| improve this answer | |

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