Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a decent Vim regexp OR command? What is the best way to find mismatched if else's?

Tags:

regex

vim

I have some mismatching if and fi statements in a script. I would like to strip out everything but the if's else's and fi's. Just so I can see the structure. Why am working so HARD with such a powerful editor? I need a BIGFATOR operator for regexp or some epiphany that has eluded me... I don't care for pontification on regular expressions just something practical working in VIM7.2.

:g/[ ^\t]if [/print

will print out the ifs

:g/[ ^\t]fi/print

will printout the fi

What I want to do is or the conditions

:g/[ ^\t]fi BIGFATOROPERATOR [ ^\t]fi/print

I have had success doing the following... but I feel I am working TOO HARD!

:call TripMatch('[ ^\t]*if [', 'else', 'fi[ \t$]')

function! TripMatch(str1, str2, str3)

let var1 = a:str1

let var2 = a:str2

let var3 = a:str3

let max = line("$")

let n = 1

for n in range (1, max)

let currentline = getline(n)

if currentline =~? var1     echo n "1:" currentline  else     if currentline =~? var2        echo n "2:" currentline     else        if currentline =~? var3           echo n "3:" currentline        else           let foo = "do nothing"        endif     endif  endif      

endfor

endfunction

like image 302
ojblass Avatar asked Mar 25 '09 02:03

ojblass


People also ask

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.

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 does regex matching work?

A regex pattern matches a target string. The pattern is composed of a sequence of atoms. An atom is a single point within the regex pattern which it tries to match to the target string. The simplest atom is a literal, but grouping parts of the pattern to match an atom will require using ( ) as metacharacters.


1 Answers

:g/[ ^\t]if\|[ ^\t]fi/print 

BIGFATOROPERATOR is \|. It's called the alternation operator. In PCRE, it's plain |. It's escaped in Vim/ex because | is used elsewhere for general commands (or something -- FIXME).

like image 145
strager Avatar answered Sep 19 '22 06:09

strager