Chinaunix首页 | 论坛 | 博客
  • 博客访问: 592461
  • 博文数量: 207
  • 博客积分: 10128
  • 博客等级: 上将
  • 技术积分: 2440
  • 用 户 组: 普通用户
  • 注册时间: 2004-10-10 21:40
文章分类

全部博文(207)

文章存档

2009年(200)

2008年(7)

我的朋友

分类: 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 filename
Tile widows horizontally or vertically
:windo wincmd H or K
Move cursor between the files
Ctrl + W + W
Alternatively, 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/g
replaces each occurrence of word1 with word2 in the current file

You can also perform this operation on a part of the file (like a method) by selecting the specific text portion of the file ( with v in command mode) and
:s/word1/word2
Alternatively, use markers to perform the same operation as shown below:
Move to the first line of the method
ma
will mark the current line as a

Move to the end of method
mb
will mark the last line of method as b

Finally,
:'a,'bs/word1/word2/g
will 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

:sh
Use Ctrl+D to return to the same line from where you entered the shell.

Move to a specific line in the file

:line_number
For example,
:32
will take you to line number 32
Use :$ to move to the last line and
:0 to move to the first line

The same result can be achieved by entering
linenumber or $ or 0 followed by (not simultaneously) G (capital G)

Use Ctrl+G to find out the current line and total number of lines in the current file

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

u
will 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.

Plugins

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.


Allows you to do all your insert-mode completion with Tab. More information about this plugin can be found at:



Provides a source code browser for the popular languages lik C, C++, Java, PHP, Perl, Python, etc). Fore detailed information, visit:

Note that this plugin requires the utility.

Resources:

阅读(885) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~