Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get Vim to match a line not beginning with a seven-digit number?

Tags:

regex

vim

I have a file with about 1000 lines. All lines begin with a seven-digit number except for the occasional line. I need to catch these lines and actually join them with the previous line.

I've managed to be able to match any line that begins with a seven-digit number by using the following regex pattern:

^\d\{7} 

I can't seem to get it to match any line that does not match this pattern though, which is really what I'm after.

As a second question that I'll embed into this one. Is it possible to have any lines that match (or do not match to stay consistent with what I'm trying to do) to join themselves to the previous line (as opposed to the J command that brings the next line up to the current one)?

Thanks

like image 759
Jason Down Avatar asked Sep 21 '09 18:09

Jason Down


People also ask

How do I match a pattern in Vim?

In normal mode, press / to start a search, then type the pattern ( \<i\> ), then press Enter. If you have an example of the word you want to find on screen, you do not need to enter a search pattern. Simply move the cursor anywhere within the word, then press * to search for the next occurrence of that whole word.

How do I search numbers in vim?

Basic Search To go back to normal mode from any other mode, just press the Esc key. Vim allows you to quickly find text using the / (forward slash) and ? (question mark) commands. It is important to note that the search command looks for the pattern as a string, not a whole word.

Does vim support regex?

Vim has several regex modes, one of which is very magic that's very similar to traditional regex. Just put \v in the front and you won't have to escape as much.


1 Answers

^\(\d\{7}\)\@! 

This is vim's regex syntax for a negative lookahead.

If you're doing this as a mass : command, you should be able to just do

:v/^\d\{7}/-1j 
like image 172
chaos Avatar answered Sep 28 '22 08:09

chaos