Key command |
Description |
h or left arrow |
Go one character to the left |
j or down arrow |
Go down one line |
k or up arrow |
Go up one line |
l (lowercase L) or right arrow |
Go one character to the right |
H |
Go to the top of the screen |
L |
Go to the bottom of the screen |
G |
Go to the end of the file |
w |
Move one word to the right |
b |
Move one word to the left |
0 (zero) |
Go to the beginning of the current line |
^ |
Go to the first nonblank character on the current line |
$ |
Go to the end of the current line |
Ctrl-B |
Go back one screen |
Ctrl-F |
Go forward one screen |
i |
Insert at the current cursor position |
I (uppercase i) |
Insert at the beginning of the current line |
J (uppercase j) |
Join current line with the next one (move next line up) |
a |
Append after the current cursor position |
o (lowercase O) |
Creates a blank line after the current line |
O (uppercase o) |
Creates a blank line before the current line |
r |
Replace the character at the current cursor position |
R |
Overwrite at the current cursor position |
x |
Delete the character at the current cursor position |
X |
Delete the character immediately before (to the left) of the current cursor position |
dd |
Cut (for later pasting) the entire current line |
D |
Cut from the current cursor position to the end of the line (this command is equivalent to d$) |
yX |
Give a movement command X, copy (yank) the appropriate number of characters, words, or lines from the current cursor position |
yy or Y |
Yank (copy) the entire current line |
p |
Paste after (next line) the current cursor position |
P |
Paste before (previous line) the current cursor position |
. (period) |
Repeat the last command |
u |
Undo the last command |
U |
Undo the last command in the last line. This will work as long as the cursor is still on the line. |
n |
Find the next match in a search |
N |
Find the previous match in a search |
:n |
Next file; when multiple files are specified for editing, this commands loads the next file. |
:e file |
Load file in place of the current file. |
:r file |
Insert the contents of file after (next line) the current cursor position |
:q |
Quit without saving changes. |
:w file |
Write the current buffer to file. To append to an existing file, use :w >> file. |
:wq |
Write the contents of the current file and quit. Equivalent to x! and ZZ |
:r! command |
Execute command and insert output after (next line) the current cursor position. |
#### Vi Options ####
The following options can come in handy while running vim (we need to add them in our ~/.vimrc file).
# echo set number >> ~/.vimrc
# echo syntax on >> ~/.vimrc
# echo set tabstop=4 >> ~/.vimrc
# echo set autoindent >> ~/.vimrc
![vi Editor Options](http://www.tecmint.com/wp-content/uploads/2014/10/vi-options.png)
vi Editor Options
- set number shows line numbers when vi opens an existing or a new file.
- syntax on turns on syntax highlighting (for multiple file extensions) in order to make code and config files more readable.
- set tabstop=4 sets the tab size to 4 spaces (default value is 8).
- set autoindent carries over previous indent to the next line.
#### Search and replace ####
vi has the ability to move the cursor to a certain location (on a single line or over an entire file) based on searches. It can also perform text replacements with or without confirmation from the user.
a). Searching within a line: the f command searches a line and moves the cursor to the next occurrence of a specified character in the current line.
For example, the command fh would move the cursor to the next instance of the letter h within the current line. Note that neither the letter f nor the character you’re searching for will appear anywhere on your screen, but the character will be highlighted after you press Enter.
For example, this is what I get after pressing f4 in command mode.
![Search String in Vi](http://www.tecmint.com/wp-content/uploads/2014/10/vi-search-string.png)
Search String in Vi
b). Searching an entire file: use the / command, followed by the word or phrase to be searched for. A search may be repeated using the previous search string with the n command, or the next one (using the N command). This is the result of typing /Jane in command mode.
![Vi Search String in File](http://www.tecmint.com/wp-content/uploads/2014/10/vi-search-line.png)
Vi Search String in File
c). vi uses a command (similar to sed’s) to perform substitution operations over a range of lines or an entire file. To change the word “old” to “young” for the entire file, we must enter the following command.
:%s/old/young/g
**Notice**: The colon at the beginning of the command.
![Vi Search and Replace](http://www.tecmint.com/wp-content/uploads/2014/10/vi-search-and-replace.png)
Vi Search and Replace
The colon (:) starts the ex command, s in this case (for substitution), % is a shortcut meaning from the first line to the last line (the range can also be specified as n,m which means “from line n to line m”), old is the search pattern, while young is the replacement text, and g indicates that the substitution should be performed on every occurrence of the search string in the file.
Alternatively, a c can be added to the end of the command to ask for confirmation before performing any substitution.
:%s/old/young/gc
Before replacing the original text with the new one, vi/m will present us with the following message.
![Replace String in Vi](http://www.tecmint.com/wp-content/uploads/2014/10/vi-replace-old-with-young.png)
Replace String in Vi
- y: perform the substitution (yes)
- n: skip this occurrence and go to the next one (no)
- a: perform the substitution in this and all subsequent instances of the pattern.
- q or Esc: quit substituting.
- l (lowercase L): perform this substitution and quit (last).
- Ctrl-e, Ctrl-y: Scroll down and up, respectively, to view the context of the proposed substitution.
#### Editing Multiple Files at a Time ####
Let’s type vim file1 file2 file3 in our command prompt.
# vim file1 file2 file3
First, vim will open file1. To switch to the next file (file2), we need to use the :n command. When we want to return to the previous file, :N will do the job.
In order to switch from file1 to file3.
a). The :buffers command will show a list of the file currently being edited.
:buffers
![Edit Multiple Files](http://www.tecmint.com/wp-content/uploads/2014/10/vi-edit-multiple-files.png)
Edit Multiple Files
b). The command :buffer 3 (without the s at the end) will open file3 for editing.
In the image above, a pound sign (#) indicates that the file is currently open but in the background, while %a marks the file that is currently being edited. On the other hand, a blank space after the file number (3 in the above example) indicates that the file has not yet been opened.
#### Temporary vi buffers ####
To copy a couple of consecutive lines (let’s say 4, for example) into a temporary buffer named a (not associated with a file) and place those lines in another part of the file later in the current vi section, we need to…
1. Press the ESC key to be sure we are in vi Command mode.
2. Place the cursor on the first line of the text we wish to copy.
3. Type “a4yy to copy the current line, along with the 3 subsequent lines, into a buffer named a. We can continue editing our file – we do not need to insert the copied lines immediately.
4. When we reach the location for the copied lines, use “a before the p or P commands to insert the lines copied into the buffer named a:
- Type “ap to insert the lines copied into buffer a after the current line on which the cursor is resting.
- Type “aP to insert the lines copied into buffer a before the current line.
If we wish, we can repeat the above steps to insert the contents of buffer a in multiple places in our file. A temporary buffer, as the one in this section, is disposed when the current window is closed.
### Summary ###
As we have seen, vi/m is a powerful and versatile text editor for the CLI. Feel free to share your own tricks and comments below.
#### Reference Links ####
- [About the LFCS][1]
- [Why get a Linux Foundation Certification?][2]
- [Register for the LFCS exam][3]
--------------------------------------------------------------------------------
via: http://www.tecmint.com/vi-editor-usage/
作者:[Gabriel Cánepa][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:http://www.tecmint.com/author/gacanepa/
[1]:https://training.linuxfoundation.org/certification/LFCS
[2]:https://training.linuxfoundation.org/certification/why-certify-with-us
[3]:https://identity.linuxfoundation.org/user?destination=pid/1