Latest web development tutorials

Linux vi / vim

All Unix Like systems are built vi editor on other instruments, the editor will not necessarily exist.

But now we use more is vim editor.

vim has the ability to program editing, font color can take the initiative to identify the correct syntax to facilitate programming.

Related articles: the history of the whole Vim shortcut keys bitmap - entry to Advanced


What is vim?

Vim is developed from out of a vi text editor. Finished up the code, compile, and error jump conveniences such programming is particularly rich, it is widely used among programmers.

In simple terms, vi is the old word processor, but the function is very complete, but still have a place to progress. vim can be said to be a good application developers with the tools. Even vim's official website (http://www.vim.org) has also said that vim is a program development tool instead of word processing software.

vim keyboard map:


vi / vim usage

Basically vi / vim is divided into three modes, namely, the general pattern, edit mode and command line mode command. The role of these three modes are:

  • General pattern:
    Vi to open a file directly into the normal mode (this is the default mode). In this mode, you can use the "up and down" keys to move the cursor, you can use the "Delete character" or "delete the entire line" to handle the contents of the file, you can use "Copy, Paste" to handle your data file .
  • Edit mode:
    In normal mode you can delete, copy, paste, etc. action, but can not edit the contents of the file! To wait until you press any letter "i, I, o, O, a, A, r, R", etc. will enter edit mode. Attention! Usually in Linux, when you press these buttons in the lower left of the screen will display a message "INSERT or REPLACE", in which case it can be edited. And if you want to return to normal mode, you must press the "Esc" key to exit the edit mode.
  • Command line mode command:
    In the normal mode among the input ':? / "In any of the three buttons, you can move the cursor to the bottom row. In this mode, which can provide you with "Search data" in action, and read, save, replace a large number of characters, leave vi, display line numbers, etc. action is reached in this mode!

Simply put, we can think of these three modes under icon to represent:

vi / vim Example

Use vi / vim into the general model

If you want to use vi to create a file named test.txt, you can do this:

[root@www ~]# vi test.txt

Direct input vi filename will be able to enter the general mode of vi. Please note, I remember back vi Be sure to add the file name, regardless of whether the file exists or not!

Press i to enter edit mode and start editing text

In the normal mode among just press i, o, a and other characters can enter the edit mode!

In edit mode which you can find in the bottom left corner of the status bar will appear the words -INSERT-, that is, you can enter any character prompt.

This time, in addition to the keyboard [Esc] this key, other keys can be regarded as a general input buttons, so you can make any edits.

Press [ESC] button to return to normal mode

Well, suppose I have to follow the above style he finished editing, then how should quit? Yes! Yes! He is to press [Esc] this button! Soon you will find in the lower left - INSERT - gone!

In general mode, press: vi wq leave after storage

OK, we want to archive, and save and leave the instruction is very simple, enter ": wq" to save leave!

OK! So we successfully created a test.txt file. It is not very simple.


vi / vim Key Description

In addition to the above simple example of i, [Esc],: outside wq, in fact, there are a lot of vim keys can be used.

Part I: General mode button description available, move the cursor, copy and paste, search, replace, etc.

The method of moving the cursor
h or left arrow key (←) Move the cursor to the left one character
j or down arrow key (↓) Move the cursor down one character
k or up arrow key (↑) Move the cursor up one character
l or the right arrow key (→) Move the cursor one character to the right
If your right hand on the keyboard, you'll find hjkl be arranged together, so you can use these four buttons to move the cursor. If you want to move several times, then move down 30 lines, for example, you can use the "30j" or "30 ↓" key combination, that is coupled with the number of (digital) want to make, press the action can!
[Ctrl] + [f] Screen "down" a move equivalent to [Page Down] button (common)
[Ctrl] + [b] Screen "up" a move equivalent to [Page Up] button (common)
[Ctrl] + [d] Screen "down" to move half a page
[Ctrl] + [u] Screen "up" move half a page
+ Move the cursor to the next column in a non-space character
- Move to a non-space character on a cursor
n <space> N represents the "digital", e.g., 20. Press the number and then press the space key, the cursor moves n characters of the line to the right. For example, 20 <space> the cursor will move to 20 characters from the rear.
0 or function key [Home] This is a digital "0": the character to move to the front of the line (common)
$ Or function key [End] Move to the last character of the surface (common) of the line
H Move the cursor to the first character in the top row of the screen
M Move the cursor to the first character in the center of the screen of the line
L Move the cursor to the first character of the bottom row of the screen
G The move to the last line of the file (common)
nG n is a number. Move to the n-th row of the file. For example 20G will move to the line 20 files (can be used with: set nu)
gg Move to the first line of the file, equivalent to 1G ah! (Common)
n <Enter> n is a number. Move the cursor down n lines (common)
Search and Replace
/ Word Under the cursor to find a name for the word string. For example, to search the archive vbird string, enter / vbird can! (Common)
? Word Looking above the cursor to a word string name string.
n The n button is in English. Repeat the previous search action delegate. After example, if we just execute / vbird go down vbird search string, press n, will continue to search for the next name down as vbird string. If you are performing? Vbird, then press n upwards will continue to search for the name vbird string!
N The N English is the key. And n the contrary, the former to "reverse" conducted a search operation. For example, after / vbird, press N indicates "up" Search vbird.
Use / word with n and N is very helpful! It allows you to find duplicate some of your search keywords!
: N1, n2s / word1 / word2 / g n1 and n2 are numbers. N1 and n2 between the first row of the string to find word1 and word2 replaced the string! For example, between 100-200 vbird line search and replace is VBIRD then:
": 100,200s / vbird / VBIRD / g." (Common)
: 1, $ s / word1 / word2 / g Looking word1 string from the first row to the last row, and will replace the string is word2! (Common)
: 1, $ s / word1 / word2 / gc Looking word1 string from the first row to the last row, and will replace the string is word2! And before the character to replace the display prompts the user to confirm (confirm) the need to replace! (Common)
Delete, copy and paste
x, X Which in his words, x is backward delete a character (equivalent to [del] button), X is a forward delete character (equivalent to [backspace] That is the backspace key) (common)
nx n is a number, n consecutive delete characters backwards. For example, I want to delete 10 characters in a row, "10x."
dd Delete the cursor where the whole line (common)
ndd n is a number. Remove the cursor down n columns, for example 20dd is deleted 20 (common)
d1G Delete all the data cursor to the first line
dG Delete all data resides to the last line
d $ Delete the last character at the cursor to the line
d0 That is a number of 0, delete at cursor, one character to the front of the line
yy Copy the cursor row (common)
nyy n is a number. Copy the cursor down n columns, for example, 20yy is copied 20 (common)
y1G Copy the cursor column to the first column of all data
yG Copy the cursor column to the last one of all data
y0 Copy the cursor to the character of the first trekking all the data
y $ Copy the character cursor to the end of the trekking all the data
p, P p is the copied data at the cursor to the next line paste, P was attached to the cursor line! For example, I am currently the cursor on line 20, line 10 and the data has been copied. After you press p, 10 rows of data that will be affixed after the original 20 rows, 21 rows from the start that is posted. But if it is to press the P? So the original line 20 to be pushed into 30 rows. (Common)
J The combination of data and the column where the cursor into the next column of the same column
c A plurality of data duplication, for example, to delete 10 lines down, [10cj]
u Undo previous action. (Common)
[Ctrl] + r Redo the last action. (Common)
The u and [Ctrl] + r is very commonly used commands! One is the recovery, the other is a redo - use these two function keys, your editor, hey! Very happy it!
. Do not doubt! This is the decimal point! It means repeat an action before the mean. If you want to de-duplication, etc. duplicate paste action, press the decimal point "." Enough! (Common)

Part II: General mode to edit mode button description available

Inserted or substituted into the edit mode
i, I Enter insert mode (Insert mode):
i is "inserted at the current cursor", it was "in the current row of the first non-space character is inserted at the beginning." (Common)
a, A Enter insert mode (Insert mode):
a is "from the current cursor to the next character is inserted at the beginning", A as "a character from the last row of the cursor at the beginning of insertion." (Common)
o, O Enter insert mode (Insert mode):
This is the case of English letters o. o to "insert a new row at the current cursor to the next line at"; O is currently on line at the cursor to insert a new row! (Common)
r, R Enter the substitution pattern (Replace mode):
r cursor will replace the one character at a time; R will always replace the text cursor, press ESC until the date; (common)
Above these keys in the bottom left corner of the vi screen will appear the words "--INSERT--" or "--REPLACE--" is. The action consists of the name you know it! ! Of particular note is that we have mentioned above, and when you want to input characters, be sure to see the INSERT or REPLACE to enter at the lower left corner in the files inside Oh!
[Esc] Exit edit mode, return to normal mode (common)

Part III: General command line mode to mode button description available

Storage instruction column, leave instructions such as
: W The edited data is written to disk file (common)
: W! If the file attributes to "read only" when forced to write to the file. However, in the end can not be written, or tell you about the file permissions file ah!
: Q Leave vi (common)
: Q! If the file has been modified, you do not want to store, use! Is forced to leave without saving the file.
Note ah, the exclamation mark (!) In which vi, often have "mandatory" means -
: Wq After leaving the store, if it is: left (Common) after was forced wq store!
ZZ This is a capital Z Oh! If no file changes, do not leave the store, if the file has been changes too, after leaving the store!
: W [filename] Edited data will be saved to another file (similar to Save As)
: R [filename] The edited data, read data from another file. That is the "filename" is added to the contents of this file line behind the cursor
: N1, n2 w [filename] N1 n2 to stored content into the file filename.
:! Command Vi temporarily left to the next command line mode execution command to display the results! For example, ":! Ls / home" to look them in vi / home underneath to ls output profile information!
Change vim environment
: Set nu Display line numbers, after setting, will display the line number in the prefix of each line
: Set nonu And set nu In contrast, the line number is canceled!

Special attention, in vi / vim, the number is significant! Numbers usually represent repeated several times meaning! There may be several representatives to the first what meaning.

For example, to delete 50 rows, it is "50dd" right! Digital Plus in action before, as I want to move 20 lines down it? That is "20j" or "20 ↓" can be.