Cris Stringfellow
Cris Stringfellow

Reputation: 3808

How to jump to first uncommented out statement in vim?

I'm using /print to search for my uncommented-out print statements as I want to comment them out. I know I could use search and replace to first remove all the comments, and then apply them, but I simply want to find the next uncommented out print statement, and I can't work out how to.

E.g. I have :

    #print fooVal
        #... do stuff
        #print barF
        #... more stuff
            print gold # <-- I want to use vim to jump straight to this line 

I want to match this so I don't have to cycle through all the print statements (even the commented-out ones) just to find the one print statement that is without #.

I've tried using :s/^\s+print and /^print but vim does not like it. Also, I looked here, but I could not find the info.

Upvotes: 1

Views: 352

Answers (2)

244an
244an

Reputation: 1589

In your case @Taky's solution is the best I think, but I noticed some comments on vim's regexp. I've studied this the last days, so perhaps it's a good idea to write it down.

In vimworld the use of e.g. *, ^ and $ as regexp special characters is called "magic" (set magic is default). As default + is not a regexp character and has to be used as \+ to mean "regexp +". However with small changes it can be "magic" too, read this: vim help - search patterns (the same as in vim editor, but as HTML, and with a good search function at the top).
See also vim help - pattern

Here is a little short guide (rules are overruling down the list):
settings:
:set ignorecase to ignore case in search and replace,
:set smartcase for ignoring case as long as no upper case letter is used (ignorecase must be on for it to work).
:set magic to be able to use some characters as regexp special characters, e.g. *, without having to precede them with \. This is default (I think).

Rules in search pattern (overrules settings)
\c ignore case, \C case sensitive
Use as e.g. /\cxxx for "ignore case", /\Cxxx for case sensitive (\c is the same as flag i in replace syntax as s/xxx/yyy/i.
\m use "magic" (same as setting magic), \M no "magic".

Here is the interesting part, to use + in vim patterns:
\v described as "very magic", that is what we all are used to when using regexp I think.
\V "very nomagic", ALL is literal, only \ has a special meaning.

Now, + can be used directly as in /\v\s+print (but for you it's better to use * in that particular case, + won't find "plugin" that starts the line).
E.g. also { expressions must have \ if \v isn't used.

Tip: do these mappings in .vimrc to always use \v in search patterns:

" To get 'normal' behavior for regexps (use "\V" to avoid)
nnoremap / /\v
vnoremap / /\v

(And it's very easy to just "backspace" away the \vwhen it's unwanted).

Upvotes: 1

Taky
Taky

Reputation: 5344

In your case no spaces before print. So try to use '*' instead of '+'.

This works for me: /^\s*print

Less convenient pattern should highlight uncommented lines with print: /^[^#]*\s*print

Upvotes: 3

Related Questions