Paul - The Programmer

simple & stupid

Remove empty lines by VIM global command

The VIM global command can select the lines that match a certain pattern in the scope of whole file. And the appened commands can operate on the selected lines.

Either the following command can be used to remove the empty lines.

:g/^\(\t\|\s\)*$/d
:v/\S/d

The command ':v' is similar to the command 'grep -v' which select the inverted matched lines.

\S is the meta character for the characters which is not white space or tab.

Alter the word order in lines by VIM regular expression

Somehow, I am required to alter the order of word in lines in a text file.

For example, the input text file is like below.

hello world
hello paul
this  is
for   test

I want this file be updated was below.

world hello
paul hello
is  this
test   for

Thanks to regular expression, I do not need to update the lines one after another.

The vim regular expression is a little different than the Perl's. But most of syntax are similar.

\w  : word charaters, includes both alphabet and number characters.

\s   : space characters.

*   : match zero or more preceding character or meta character like \s, \w.

The patterns can be grouped by enclosing with \( and \). 

& :  the whole matched pattern

\0:   the whole matched pattern

\1:   the matched pattern in the first pair of \( and \)

\2:   the matched pattern in the second pair of \( and \)

So, to accomplish the above task, the substitution regular expression is :

:%s/\(\w*\)\(\s*\)\(\w*\)/\3\2\1/