Regular expressions appear to be fast gaining favor among VIM users as they learn about the incredible programming capability that they can bring. VIM’s syntax differs slightly from Perl’s, but it’s close enough. As a result, VIM users can benefit from Perl regular expression examples. In this article, we will discuss the most commonly used regular expressions in vim with examples. Let’s get started.
Here we have a file named myfile that has all the pieces that we may want to deal with, so the first thing we will do is the global search and replace, so this is quite easy. Firstly, we will open this file with this command.
Type colon. It will get you into command line mode and type percent, all lines in the file substitute. We can do something really simple, like substituting all of the cases where you see IND with LAS, for example, so that’s the sort of easiest replacement.
This command will make the changes. You can compare the following screenshot with the first to observe that IND is replaced with LAS.
There are many other ways to use search and replace, so probably everybody knows here is if you enter your visual line mode (press shift+v).
We have selected a block of text and substituted IND with LAS; for example, if we press Q and colon, we can edit this and say that we also want to change everywhere you see DUB to LAS.
This is the kind of easiest demonstration of search and replace so that range you can do a lot of different ways the first one being the kind of the whole file with percent you can specify when you’re picking a range here, and you look at it automatically says you know what have you selected with apostrophe less than an apostrophe greater than you can also as you might specify a particular range.
Now we can say on line two to line seven, replace ENG with RST.
Leaving text aside, now we may consider all the places where there is a zero, and let’s replace it with a one.
You might notice that this only highlighted the first match, so there are many like on relative line two up there; you can see we have a lot of extra zeros; there are flags that you can issue, for example, G to do all of the matches per line.
This is very useful if you’re programming because you might have multiple instances of a variable on the same line, but you might also want to give confirmation each time what you can do with C the C flexes for confirming, so you say yes (press Y) want to replace that one and no (press N) if you don’t want to replace a particular digit. When you’re done, you know you can either get through the match setter you can press Q, and those substitutions will be done.
This is the briefest overview and maybe the most basic way of using the substitute command. There are special characters for substituting where you don’t necessarily want to specify exactly what your match is by typing it out so for example, if we say find all the places where there is a word character so this is anything that’s a letter and not a space basically so space characters with s and digits with D if you want to match a set of these, for example, you can specify how many so if you want to match four of them at a time so now you’ll see we have just groups of four we also want to say just give me if there’s one or more slash plus so this is going to be all the cases where you have one or more digits on a line.
There’s “s” and capital “S” which matched space characters which includes your spaces, your tabs, and newlines; there are the digit characters 0 through 9 or not digit characters which would be capital d lowercase D being the digit characters, and then you have your word or not word characters which is anything that’s not space, so those are your sort of most useful substitution commands and generally how to use them.
Other things are useful about regular expressions. For example, let’s say that if you want to replace all of the cases where you have American space zero with something.
This is an OK way of solving this problem, but if you use very magic, which is this / V. Let’s just assume that it’s not here, and we will do one that’s magical here.
Here is American space and closes, so there’s our match. Then we would match that one, or let’s do it this way let’s match the zeros because there are some of those. We want to replace those with the same thing American space, and here we would do backslash 1, and now it knows American space and follow that with a 3, so now we have gone from American space 0 to American space 3.
Here is another example to give you when you want to keep some bit of your match but you don’t necessarily maybe know what it is or how it’s going to be formatted or might be different from one line to the next but in this case, what you want to do is you would tell them to start on the lines that say American but don’t want to start the match until right here ZS and then say match a digit and then end the match and let’s replace with a three.
Conclusion
We have done so; this has been a brief overview of regular expressions. Topics that we have covered are how to select ranges for substitution, what the metacharacters are or rather super characters for word space and digits, how to do capture groups, how to specify the start and end of a match, and then how to select ranges using regular expressions which you can easily do in your programs.