分类: LINUX
2009-04-16 22:28:31
I am sure every software developer has come across the vi editor a few times. Although, the initial learning curve can be a little steep, the flexibility and power offered by vi makes it a worthy programming tool. Moreover, you are guaranteed to find it on any unix box that you log into. Following are some of the commands that I have found to be extremely useful during my development work.
Find the match of current parenthesis, brace, or bracket
%
Working with Multiple Windows
Open another file in VI in the existing window
:sp filenameTile widows horizontally or vertically
:windo wincmd H or KMove cursor between the files
Ctrl + W + WAlternatively, you can move cursor to right or left window
Ctrl W followed -> or <-
Search forward for the next identifer under the cursor
*
Search backward for the previous identifer under the cursor
#
Search and Replace
:%s/word1/word2/greplaces each occurrence of word1 with word2 in the current file
:s/word1/word2Alternatively, use markers to perform the same operation as shown below:
mawill mark the current line as a
mbwill mark the last line of method as b
:'a,'bs/word1/word2/gwill only make the changes in the lines between the markers a and b
Execute any shell command by prepending it with a :! in command mode
:!This is particularly useful if you want to compile a program without exiting the editor. For java, use
:!javac %% denotes the name of the current file (eg. Sample.java)
Access the shell without exiting the editor
:shUse Ctrl+D to return to the same line from where you entered the shell.
Move to a specific line in the file
:line_numberFor example,
:32will take you to line number 32
Repeat the last command
Also, in Vi, typing in numbers before a command repeats that command. Therefore, 100. will repeat the last command 100 times. For instance, 5dd will delete the next 5 lines.
Indentation
Use >> and << to indent lines to the right or left respectively. Multiple lines can also be indented by selecting the portion of text with v in command mode followed by << or >>
Undo/Redo
uwill undo the last change while
U
will undo all changes in the current line
Ctrl + R performs the redo operation. You can complement this with the undo last command operation to arrive at the oldest change.
Additionally, I'd like to point out a couple of plugins that can be extremely beneficial, especially if you are familiar with IDEs like NetBeans and Eclipse.