Blogs Directory
Showing posts with label VIM Tips. Show all posts
Showing posts with label VIM Tips. Show all posts

Friday, October 11, 2013

Splitting a line into multiple lines and vice versa

This is the most common situation that we encounter in our work flows. I work in SOC team and there by need to do lot of text manipulations. Here i learnt that splitting a line into multiple lines and multiple lines into a single line using the method bellow.

Example 1:   (Text)

America
Africa
India
Germany

Use this command in the command mode of vim :%s/\n/ / 

The output would be like this :
 America Africa India Germany

Example 2: (Text)

America Africa India Germany

Use this command in the command mode of vim to split the line into multiple lines :%s/ /^M/g
% -> works on all the lines in the file
s -> substitute command
/ -> command divider
^M -> we get this by pressing <Ctrl>V followed by Enter.
g -> works on multiple occurrence of the pattern in the line.

Saturday, June 8, 2013

VIM : Auto complete of words when typing


VIM is a very powerful text editor so far i have ever come across because no other text editor has such an extensive commands for editing text. Here is the one which would make you love vim for all its rich text editing commands.

Think of when you are writing an RTL code where you often use same names often in the file. Vim makes it easy for you in this scenario. It can auto complete your word by matching it with "previous", "next" matching words. We need to type few letters of the word and it is already used in the file then you can use the bellow commands to insert the same word without typing the word again.

Ctrl+n : insert the next matching word.

Ctrl+p : insert the previous matching word.

In the similar way, if you are trying to type a unix path in the file then you can type in directly without the knowledge of the absolute path by using

 Ctrl+xf : list the available dirs/files in the path.




VIM : Delete all the lines containing the pattern specified



VIM has a very useful command "g" which can be used to perform operations over the lines which contains the matched pattern.

The Syntax is :

:g/pattern/d

The above command deletes all the lines containing the pattern.

If you want to delete all the lines not containing the pattern then use the command as :

:g!/pattern/d

Note that the "g!" is equivalent to "v" hence the above command can also be performed using "v" as :

:v/pattern/d

You can match multiple pattern using "or" character "|" as bellow :

:v/pattern1\|pattern2\|pattern3/d

The above command deletes all the lines that does not contain the three patterns pattern1, pattern2 and pattern3.

Monday, April 15, 2013

Interchange two columns in VIM


Sometimes we might be working on the databases where we require swapping of the columns. There may be many ways but what u found most interesting is this method.

1. First we need to provide the range over which the swapping need to be done. In this case the range is lines. If you want to swap the columns across all the lines in the file, then use % sign. % means all lines in the file.

2. Then we need to substitute the existing columns with the changed ordered column. For that we are going to use the substitute command 's'.

3. Then we need to provide the pattern to be matched. In this case the columns to be swapped need to be matched. In the same time we need to have them in the memory so that we can substitute them with the chaged order. For this we can make use of '\(' and '\)' before and after the patterns. This makes the vim to save them in the variable '1', '2'... etc upto '9'.

               \(pattern1\) \(pattern2\)
The space between the two patterns is the delimiter between the columns. It can be anything. I have used space here for the example. If you don't have a regular pattern to be matched then you can use '.*' to match the word like :
               \(.*\) \(.*\)

4. Then we need to specify the pattern to be replaced with. This is done using our saved variable. '1', '2' etc.
                   \1 \2

An example would be like this :

 :%s/\(.*\) \(.*\)/\2 \1/c

:                   command line prompt
%                 on all the lines
s                   substitute
\(.*\)             matches the first word (first column)
space -         matches the space after the name
\(.*\)            matches the second word (i.e, second column)
\2                 variable that holds the second matched word
\1                 variable that holds the first matched word
c                  substitute only on confirmation