Linux Common text editor Nano/vi/vim__linux

Source: Internet
Author: User
Tags file permissions

Linux has a lot of text editors, where the system (regardless of which branch) will bring the Nano and VI of the two most basic Editor. Vim is equivalent to VI upgrade version. Here we will explain the use of these editors in turn.


You can open a file or create a new file by typing the nano filename directly on the command line.

You can see the first line of anti-White describes the version number of the Nano and the name of the file being edited. Next is the editable region, where the Nano.txt file is a new file and the content is empty, so the editing area has no text. Look at the last two lines, this is the nano can be used under a number of shortcut keys, its corresponding meaning translated as follows:

Ctrl+g: Getting online Help

Ctrl+o: Save the file, if you have permission to save the modified file

Ctrl+r: Reading data from other files, you can paste the contents of a file in this document

Ctrl+y: Show Previous page

Ctrl+k: Cut the contents of the current line

CTRL + C: ' Show where the cursor is

Ctrl+x: Exit the Nano, if you have modified the file, you will be prompted if you want to save the modified content

Ctrl+j: Adjust text formatting

Ctrl+w: Find the command, press and then jump to the last line of the reverse white position, enter the content to find a return.

CTRL + V: Show next page

Ctrl+u: Undo Cut Command, here's a test, not simply undo a line of command, when the cursor is positioned to the position of the undo row, if the position has more than one row is cut, will restore all the cut content, but many times ctrl+u will be restored, this may be a bug. This shortcut is similar to CTRL + + under Windows. The reader can try to understand this first.

Ctrl+t: I don't know what it means, it seems to be checking spelling, but I don't know how to use it.

The Nano is the starting point for Linux to learn the best tool, where we only need to know how to enter the editor, how to save the exit. Next look at Linux by the great God to push the altar of the VI editor.

2.VI Editor

VI is an old-fashioned word processor, the function is already very complete. No matter good or bad use, since we are so respected, there must be his benefits, we first learned that there will be no harm.

1) VI Model

VI is divided into three kinds of models

General mode: To VI Open or a new file can be directly into the general mode, in this mode, you can use up and down the cursor movement, you can delete characters or whole line, you can copy paste file data.

Edit mode: As the name implies, it is possible to edit the text mode, in general mode, press "I,i,o,o,a,a,r,r" These letters can enter the editing mode, after entering the editing mode in the lower left there will be insert or replace the words.

Pressing the ESC key in edit mode exits to half mode.

Command line mode: In general mode, enter ":,/,?" "Any of these three characters will move the cursor to the bottom row, in which you can find, replace, save, and leave operations like VI."

Use VI filename to enter VI General mode:

Hot Key description in general mode:

How to move the cursor
h or LEFT ARROW keys (←) The cursor moves one character to the left
J or DOWN ARROW keys (↓) The cursor moves down one character
K or Up ARROW keys (↑) The cursor moves one character up
L or right ARROW key (→) The cursor moves one character to the right
If you put your right hand on the keyboard, you will find that the hjkl are lined up, so you can use these four buttons to move the cursor. If you want to move several times, such as moving down 30 lines, you can use the combination of "30j" or "30↓", that is, add the number of times you want to do it, and then press the action.
[Ctrl] + [f] Screen "Down" to move one page, equivalent to [Page DOWN] button (commonly used)
[Ctrl] + The screen moves up one page, equivalent to the [Page up] key (commonly used)
[Ctrl] + [d] Screen "Down" move half page
[Ctrl] + Screen "Up" move half page
+ The cursor moves to the next column that is not spaces
- The cursor moves to the previous column in a spaces
N<space> That n means "number", for example 20. When you press a number and then press the SPACEBAR, the cursor moves the n characters of the line to the right. For example 20<space>, the cursor moves 20 characters away from the back.
0 or function key [home] This is the number "0": Move to this line of the first word entered prompt (commonly used)
$ or Function key [end] Move to the last character entered prompt in this line (commonly used)
H The cursor moves to the first character of the line at the top of the screen.
M The first character of the line that the cursor moves to the center of this screen
L The cursor moves to the first character of the line at the bottom of the screen
G Move to the last line of this file (commonly used)
NG N is a number. Move to the nth line of this file. For example, 20G will move to line 20th of this file (fit: Set Nu)
Gg Move to the first line of this file, equivalent to 1G. Common
N<enter> N is a number. The cursor moves down n rows (commonly used)
Search and replace
/word Look under the cursor for a string called Word. For example, to search Vbird this string in the file, enter/vbird. Common
? word Look above the cursor for a string that has a string name of Word.
N This n is an English key. Represents "Repeat the previous search action". For example, if we have just executed/vbird to search for Vbird this string, then press N and then proceed down to search for the next string named Vbird. If you are performing a vbird, then pressing N will continue searching for a string with the name Vbird.
N This N is an English key. In contrast to N, a previous search action is performed for "reverse". For example, after/vbird, pressing N means "up" search for Vbird.
Using/word with N and N is very helpful. Can let you repeat to find some of your search keywords.
: n1,n2s/word1/word2/g N1 and N2 are numbers. Look for the word1 string between the N1 and N2 lines and replace the string with Word2. For example, search for vbird between lines 100 through 200 and replace it with Vbird:
": 100,200s/vbird/vbird/g". Common
: 1, $s/word1/word2/g Look for the word1 string from the first line to the last line and replace the string with Word2. Common
: 1, $s/word1/word2/gc Look for the word1 string from the first line to the last line and replace the string with Word2. And the prompt character is displayed before the substitution to confirm whether the user (confirm) needs to be replaced. Common
Delete, copy and paste
X, X In a line of words, X deletes one character backwards (equivalent to the [Del] key), and X deletes one character forward (equivalent to [backspace] or BACKSPACE) (commonly used)
Nx N is a number, delete the n characters continuously backwards. For example, I'm going to delete 10 characters in a row, "10x".
Dd Deletes the entire column in which the cursor is located (commonly used)
Ndd N is a number. Deletes the lower n column where the cursor is located, for example, 20DD deletes 20 columns (commonly used)
d1g Remove all data from the first row of the cursor
Dg Deletes all data from the cursor to the last line
d$ Deletes the cursor at the end of the line to the last character
D0 That's 0 of the number, delete the cursor, to the first character of the line
Yy The row where the copy cursor is located (commonly used)
Nyy N is a number. Copy the lower n column where the cursor is located, for example 20yy to copy 20 columns (commonly used)
y1g All data in the first column of the copied cursor
YG Copy all data in the last column of the cursor
Y0 Copy the character that contains the cursor to all data at the beginning of the line
y$ Copy the character of the cursor to all data at the end of the line
P, p P Pastes the copied data on the next line of the cursor, and p is the row on the cursor. For example, I currently have the cursor on line 20th and have copied 10 rows of data. When P is pressed, the 10 rows of data are posted to the original 20 lines, which is the beginning of 21 lines. But what if it's pressed P. Then the original 20th line was pushed into 30 lines. Common
J Combine the columns of the cursor with the data from the next column into the same column
C Delete multiple data repeatedly, for example, delete 10 lines down, [10CJ]
U Restore the previous action. Common
[Ctrl]+r Redo the last action. Common
This u and [ctrl]+r are very common instructions. One is recovery, and the other is redo. Use these two function keys, your editor, hey. Very happy.
. Don't doubt it. This is the decimal point. It means to repeat the previous action. If you want to repeat the deletion, repeat the action, and so on, press the decimal point "." Just fine. Common

General mode to enter the editing mode of the key description:

I, I Enter insert mode:
I inserts from the current cursor, and I starts inserting at the first non-spaces line of the current row. Common
A, a Enter insert mode:
A is "insert from the next character at the current cursor", and A is "insert at the last character of the line where the cursor is located." Common
O, O Enter insert mode:
This is the case of the English letter O. o Inserts a new row at the next line at the current cursor; O inserts a new row on the previous line at the current cursor. Common
R, R Enter replacement mode (replace mode):
R replaces only one character of the cursor at a time; R replaces the text of the cursor until you press ESC;
Above these keys, in the VI screen in the lower left corner will appear "--insert--" or "--replace--" words. You know the action by name. The special note is that we mentioned above, you want to enter characters in the file, you must see the lower left corner INSERT or REPLACE to enter OH.
[ESC] Exit edit mode, back to General mode (common)

General mode switch to command line mode key Description:

: W Write edited data to a hard disk file (commonly used)
: w! If the file property is read-only, it is forced to write to it. However, whether or not to write, or with your file permissions on the file.
: Q Leave VI (commonly used)
: q! If you have modified the file, do not want to store, use! Do not store files for forced departures.
Notice ah, that exclamation point (!) in VI, often with the meaning of "coercion" ~
: Wq After storage leave, if: wq! To leave after forced storage (commonly used)
Zz This is the capital Z Oh. If the file is not changed, do not store leave, if the file has been altered, then store and leave.
: w [filename] Store edited data in another file (similar to save new file)
: R [FileName] In the edited data, read the data from another file. The file "filename" will be added to the cursor line after the
: n1,n2 w [filename] Store the contents of N1 to N2 into the filename file.
:! Command Temporarily leave the vi to instruction column mode to execute the command display results. For example
『:! Ls/home "can be in VI to see the/home under the LS output file information.
Changes to the VIM environment
: Set Nu Displays the line number, which, when set, displays the line number of the row at the prefix of each line.
: Set Nonu In contrast to set NU, the line number is canceled.
These are the forms on the bird's book, in order to facilitate inquiries, here picked up.

About VI Some of the main functions in this table can be seen, next we look at vim relative to VI for us to provide what interesting features.

In most Linux distrubutions have used vim instead of VI, such as CentOS (Ubuntu seems to have not used vim instead of VI), use the alias to see whether the system is using VIM:

The first picture is Ubuntu, the second is CentOS, you can see under the CentOS using Vim instead of VI, type VI at the command line is equivalent to type Vim.

VIM's block selection

The operations performed in VI are generally based on rows or individual characters, so what to do with an area block operation. At this time can

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.