Nov 28, 2012

Vim Commands

1) How to open file in VI Editor at a specific line

]#  vi  Sample.txt  ++105

ie., From the linux terminal, it will open the Sample.txt  at line 105

2) How can we search word that is under the cursor in VI/VIM

Put the cursor on the word
Press " * " for the next occurrence of the word

3)  a) Replace a pattern with another pattern from line "m" to line "n"

>>cat test.txt

...
49 prabhath kota
50 lakshmi muvvala
51 vamsidar kota
52 prabhath kota
53 prabhath prabhath
54 perl awk
55 prabhath prabhath
56 kota prabhath
...

:50,55s/prabhath/darling/
#It replaces line 50 to line 55, only the first occurance in the line

o/p:

...
49 prabhath kota #49th line
50 lakshmi muvvala
51 vamsidar kota
52 darling kota
53 darling kota
54 perl awk
55 darling kota      //replaced only first occurance
56 kota prabhath #56th line
...


>>cat test.txt

...
49 prabhath kota      #49th line
50 lakshmi muvvala
51 vamsidar kota
52 prabhath kota
53 prabhath prabhath    //replaced all
54 perl awk
55 prabhath prabhath   //replaced all
56 kota prabhath          #56th line
...


:50,55s/prabhath/darling/g
#It replaces line 50 to line 55, all the occurances in the line

o/p:

...
49 prabhath kota #49th line
50 lakshmi muvvala
51 vamsidar kota
52 darling kota
53 darling darling
54 perl awk
55 darling darling
56 kota prabhath #56th line
...


3 b) Replace a pattern with another pattern replacing through out the file


:%s/prabhath/prabhath12345/g


3 c) Replace a pattern with another pattern from the current line to next "n" lines


e.g.,

>> cat test.txt
test test test test
test test test test

#if the cursor is on the 2nd line ("." denotes 2nd line)
#if the cursor is on the 1st line ("." denotes 1st line)

:.,.+2s/test/testabc


o/p:

testabc test test test
testabc test test test


:.,.+2s/test/testabc/g
# "g" is for all the occurances in the current line

o/p:

testabc testabc testabc testabc
testabc testabc testabc testabc


4) To remove the last character from every line in the file. 


Use it if every line in the file ends with ^M as the result of a file transfer.
Execute it when the cursor is on the first line of the file.

:%s/.$//

Note:
$ -> denotes end of the line
^ -> denotes start of the line


5) To remove the first character from every line in the file. 


:%s/^.//

Note:
$ -> denotes end of the line
^ -> denotes start of the line



6) How to search in Vim

" Show all tabs:

/\t


" Show and Replace trailing(ending) whitespace:

:%s/\s\+$//gc


" Show spaces before a tab:

/\+\ze\t



7) Edit new file from the current viewing of the file...

Suppose you already opened test.txt, suppose you are trying to open new.txt now..

gvim test.txt

after that you want to close test.txt and open new.txt, then you can do like...

:e new.txt




8) How to access multiple files from the same window 

]# vim  test_1.txt         #Which opens test_1.txt

After editing this file, without closing or coming out of this file, do you know how to edit another file from the same window

:e test_2.txt      #Which opens test_2.txt

Now you want to go reopen the previous file test_1.txt

:e#                  #Now you have already opened test_2.txt, but this reopens test_1.txt

after this, if you try to give the command again, it reopens previous file test_2.txt

:e#                  ##Now you have already opened test_1.txt, but this reopens test_2.txt


This trick helps you a lot when you have to work switching back to back two files and this way you can easily reopen the previous file without much typing effort



9) How to Split screen

Horizontally:


vi test.txt

:split new.txt


Vertically:

vi test.txt

:vsplit new.txt

Note:
Cntrl + ww //To change the window from one to another


9) Move the cursor to beginning of line in Vim Editor

$ takes you to the end of the line


10) Move the cursor to beginning of line in Vim Editor


^ takes you to the first non-whitespace character in the line, and

0 (zero) takes you to the beginning of the line including whitespace



No comments:

Post a Comment