Linux Vi/vim

Source: Internet
Author: User
Tags delete key save file file permissions

Linux Vi/vim

All Unix like systems will be built into the VI document editor, and other document editors will not necessarily exist.

But at the moment we use more of the Vim editor.

Vim has the ability to edit the program, can be active in font color to identify the correctness of the grammar, convenient programming.

Related article: The history of the most complete VIM shortcut key map-entry to the advanced

What is VIM?

Vim is a text editor developed from VI. Code completion, compilation and error jumps and other convenient programming features are particularly rich, in the programmer is widely used.

To put it simply, VI is an old-fashioned word processor, but the function is already complete, but there is still room for improvement. Vim can be said to be a very useful tool for program developers.

Even Vim's official website (http://www.vim.org) itself says VIM is a program development tool rather than word processing software.

Vim keyboard diagram:

Use of Vi/vim

Basically, Vi/vim is divided into three modes, namely command mode, input mode and bottom Command mode (last line mode). The three modes of action are:

Command mode:

The user has just started Vi/vim and entered command mode.

In this state, the keystrokes are recognized by VIM as commands rather than as input characters. For example, we press I at this time and do not enter a character, I is treated as a command.

Here are a few common commands:

    • i switch to input mode to enter characters.
    • x deletes the character at which the current cursor is located.
    • : switch to baseline command mode to enter commands at the bottom of the line.

If you want to edit the text: Start vim, enter command mode, press I, switch to input mode.

Command mode has only some of the most basic commands, so you still have to rely on the bottom line command mode to enter more commands.

Input mode

Press I in command mode to enter the input mode.

In input mode, you can use the following keys:

    • character keys and shift combinations , enter characters
    • Enter, enter, line break
    • Back SPACE, backspace key, delete the previous character of the cursor
    • DEL, delete key, delete cursor after one character
    • arrow keys , moving the cursor in text
    • HOME/end, move cursor to beginning/end of line
    • pageup/pagedown, top/Bottom page
    • Insert, toggle cursor to input/Replace mode, the cursor will become a vertical bar/underline
    • ESC, exit input mode, switch to command mode
Baseline Command mode

Press: (colon) in command mode to enter the baseline command mode.

The bottom line command mode can enter commands for single or multiple characters, and there are many commands available.

In the baseline command mode, the Basic command has (the colon has been omitted):

    • Q Exit Program
    • W Save File

Press the ESC key to exit the bottom command mode at any time.

To put it simply, we can think of these three patterns as the icons below to indicate:

Vi/vim using an instance to enter general mode using Vi/vim

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

$ VI runoob.  TXT 

The direct input VI file name can enter the general mode of VI. Please note, remember to add a file name after VI, regardless of whether the file exists or not!

Press I to enter input mode (also known as edit mode) to start editing text

In the general mode, just press I, O, a and other characters can enter the input mode!

In edit mode, you can find the –insert-in the status bar in the lower left corner, which is the hint that you can enter any character.

At this point, the keyboard, in addition to the ESC button, other keys can be regarded as the general input button, so you can make any edits.

Press the ESC button back to normal mode

Well, assuming I've given him an edit in the style above, how do I get out? Yes! That's right! is to give him the button to press ESC ! Immediately you will find the –insert– in the lower left corner of the screen is missing!

In general mode, press the : WqLeave VI after storage

OK, we want to archive, save and leave the command is very simple, input : Wq can be saved away!

Ok! This allows us to successfully create a runoob.txt file.

Vi/vim Key Description

In addition to the simple example above I, ESC,: Wq, in fact, Vim has a lot of keys can be used.

Part I: General mode available cursor movement, copy and paste, search and replace, etc.
How to move the cursor
h or LEFT ARROW key (←) The cursor moves one character to the left
J or DOWN ARROW key (↓) Move the cursor down one character
K or Up ARROW key (↑) Move the cursor up one character
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 arranged together, so you can use the four buttons to move the cursor. If you want to move multiple times, such as moving down 30 lines, you can use the "30j" or "30↓" combination of keys, that is, plus the number of times you want to do (number), press the action!
[Ctrl] + [f] Screen "Down" to move one page, equivalent to [PAGE DOWN] key (common)
[Ctrl] + [b] Screen "Up" to move one page, equivalent to [PAGE UP] key (common)
[Ctrl] + [d] Screen "Down" to move half a page
[Ctrl] + [u] Screen "Up" move half page
+ Cursor moves to the next line of non-whitespace
- Cursor moves to the previous line of non-whitespace
N<space> That n means "number", for example 20. When you press the 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 character distances to the back.
0 or function key [Home] This is the number "0": Move to the front of the line prompt (common)
$ or Function key [End] Move to the last face of this line prompt (common)
H The first character of the line that the cursor moves to the top of the screen
M The first character of the line that the cursor moves to the center of the screen
L The first character of the line that the cursor moves to the bottom of the screen
G Move to the last line of this file (common)
NG N is a number. Move to the nth row of this file. For example 20G moves to line 20th of this file (mates: Set Nu)
Gg Move to the first line of this file, equivalent to 1G Ah! Common
N<enter> N is a number. Move the cursor down n rows (common)
Search replacement
/word Look under the cursor for a string called Word. For example, in the file search Vbird This string, you can enter/vbird! Common
? word Look for a string with the string name word on top of the cursor.
N This n is the English key. Represents the action of repeating the previous search. For example, if we have just executed/vbird to search for Vbird this string, then pressing N will continue to search down the next string named Vbird. If the Vbird is executed, then pressing N will continue to search for the string named Vbird!
N This N is the English key. In contrast to N, the previous search action for "reverse". For example/vbird, pressing N means "up" to search for Vbird.
Using/word with N and n is extremely helpful! Can let you repeat to find some of the keywords you search!
: n1,n2s/word1/word2/g N1 and N2 are numbers. Look for the string word1 between N1 and N2, and replace the string with Word2! For example, search for vbird between 100 and 200 rows 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 display the prompt character before the substitution to the user to confirm (confirm) whether need to replace! Common
Delete, copy and paste on
X, X In one line of words, X is the backward deletion of a character (equivalent to the [Del] key), and X is to delete a character (equivalent to [backspace], which is the backspace) (common)
Nx N is a number that continuously deletes n characters backwards. For example, I want to delete the 10 characters consecutively, "10x".
Dd Delete the entire row where the cursor is located (common)
Ndd N is a number. Delete the down n rows where the cursor is located, for example, 20DD to delete 20 rows (common)
d1g Remove all data from the first row of the cursor
Dg Delete all data from the last row of the cursor
d$ Delete the last character of the row where the cursor is located
D0 That is 0 of the number, delete the cursor at the top of the line, and the first character
Yy The row where the cursor is copied (common)
Nyy N is a number. The next n rows where the cursor is copied, for example, 20yy copies 20 rows (common)
y1g Copy all data from the row to the first row of the cursor
YG Copy all data from the row of the cursor to the last row
Y0 Copy all data from the same character as the cursor to the beginning of the line
y$ Copy all data at the end of the line with the same character as the cursor
P, p p to paste the copied data on the next line of the cursor, and p for a 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 will be affixed to the original 20 lines, i.e. 21 lines. But what if we press P? Then the original 20th Guild was pushed into 30 rows. Common
J Combines data from the row of the cursor and the next row into the same row
C Repeatedly deleting multiple data, such as deleting 10 rows down, [10CJ]
U Restores the previous action. Common
[Ctrl]+r Redo the last action. Common
This u with [ctrl]+r is a very common instruction! One is recovery, the other is redo once ~ Use these two function keys, your editor, hey! Very happy!
. Don't doubt it! This is the decimal point! To repeat the meaning of a previous action. If you want to repeat the deletion, repeat the paste, and so on, press the decimal point "." It's all right! Common
Part Two: General mode switch to edit mode available button description
Enter or replace edit mode
i, i< /td> Enter input mode (Insert mode):
I is "input from the current cursor," And I is "start typing at the first non-whitespace space where you are currently in line." ( Common)
A, a enters input mode (Insert mode):
A is "enter at the next character where the cursor is currently located", and A is "start at the last character of the line where the cursor is located." ( Common)
o, o Enter input mode (Insert mode):
This is the case of the English letter O. o Enter a new line at the next line where the cursor is currently located; o Enter a new line for the previous line where the cursor is currently located! ( Common)
R, R enters replace mode:
R replaces only the character of the cursor once; R will always replace the text of the cursor until ESC is pressed; ( common"
above these keys, the words "--insert--" or "--replace--" appear in the lower left corner of the VI screen. The action is known by the name!! In particular, we mentioned above, you want to enter characters in the file, be sure to see the lower left corner of INSERT or REPLACE to enter Oh!
[ESC] exit edit mode, back to normal mode ( Common)
Part III: Description of available buttons for general mode switch to command line mode
Instructions for storing, leaving, etc. of the command line
: W Write the edited data to the hard drive archive ( Common)
: w! If the file property is read-only, the archive is forced to be written. However, in the end can be written, or with you on the file permissions related Ah!
: Q Leave VI ( common)
: q! If you have modified the file, do not want to store, use! Do not store files for forced departures.
Note that the exclamation point (!) in VI, often has the meaning of "mandatory" ~
: Wq To leave after storage, if: wq! After forcing the store to leave ( Common)
Zz This is the capital Z! If the file does not change, then do not store away, if the file has been changed, then save and leave!
: w [filename] Save edited data to 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 row after the
: n1,n2 w [filename] Store the contents of N1 to N2 into the filename file.
:! Command Temporarily leave VI to command line mode to perform command display results! For example
『:! Ls/home "You can see in VI the file information under/home with LS output!
Changes to the VIM environment
: Set Nu Displays the line number, which, when set, displays the line number of the row in the prefix of each row
: Set Nonu In contrast to set NU, the line number is canceled!

Pay special attention to the Vi/vim, the number is very meaningful! A number usually means repeating it a few times! It is also possible to represent the meaning of going to the first few what.

For example, to delete 50 rows, use "50DD"! The number is added before the action, if I want to move down 20 rows? That's "20j" or "20↓".

Reprinted from: http://www.runoob.com/linux/linux-vim.html

Linux Vi/vim

Related Article

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: info-contact@alibabacloud.com 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.