Search for “not newline” in vim

regular expressionvim

To search for A T\n in vim I can just do

/A T\n

What would be the equivalent for not newline at the end?

/A T[^\n]

did not work.

Best Answer

/A T.

searches for A T followed by a non-newline character (. is the standard regex operator that matches any character (except newline)).

/A T\n\@!

searches for A T not-followed by a newline character (you'll see the difference if you set hls). \@! is a vim-specific regexp operator that provides with similar functionality as the (?!...) perl/PRCE regexp operator (negative look-ahead). That one would work in the case of a non-text file that ends in A T (and no newline).

You could also use the positive look-ahead:

/A T.\@=

(A T as long as it's followed by a non-newline character).

You can also do:

/A T\ze.

Same as /A T. except that the end of the matched string is after the T. \zs and \ze are again vim specific and can be used to narrow the matched string (as seen with highlight search) within the pattern. The perl/PCRE equivalent of \zs is \K (in recent versions).

Having said that, /A T[^\n] works for me (vim 7.4.52), though [...] would never match a newline anyway (you'd need \_[...] to include the newline), so . is simpler.

Related Question