vi and vim introduction

1, vi Introduction

V I is the linux system and Unix all standard editor , similar to Notepad (Notepad) editor in the Windows system, for any version in Unix systems and linux, editor VI are identical and can therefore be introduction vi any other places can learn more about it, vi linux is the most basic text editor , after learning that it can smooth the linux world, especially in the terminal.

V i and vim are linux system editor , the difference is, vim relatively high level, can be seen as an upgraded version of vi . V I used for text editing , but vim more suitable Coding .

Vim is the point of movement of the cursor, the mode switching, delete, search and replace, copy and paste, cancellation of the command.

2, three modes of vim (focus)

Command mode , edit mode (input mode) , the last line mode (last line mode)

(1) Command mode : In this mode is not to directly edit the text, a number of shortcut keys can input operation (deleting lines, line copy, move the cursor, paste, etc.) [after opening to enter the file mode]

(2) the edit mode (input mode) : In this mode can edit the contents of the file.

(3) line mode (last line mode) : the bottom line can enter commands to operate (search, replace, save, exit, withdrawal, highlighting, etc.) to the file

3, Vim open the file mode (four kinds, it is required to master the first three)

(1) #vim file path (action: open the specified file)  

(2) + #vim digital file path (action: open the specified file, and move the cursor to a specified row )

(3) path #vim + / keyword file (action: open the specified file, and is highlighted keywords )

(. 4) #vim file path of a file the file path 3 path 2 (action: open multiple files )

Focus: make a copy of an / etc / passwd file, copied to the current home directory (do not directly modify directly under etc)

[root@localhost ~]#cp /etc/passwd  ~/

All subsequent commands are based vim / root / passwd file operations.

Exits: type: q pressing the Enter key

4, the command mode:

(1) Move the cursor

(A) move the cursor to the beginning of the line : a key shift + 6 or ^ ( T 6 letters above, do not keypad 6 )

(B) move the cursor to the end of the line : key S HIFT + 4 or $ ( R & lt letters 4 above, do not keypad 4 )

(C) move the cursor to the first line : Key: GG

And (d) move the cursor to the last line : Key: G

(E) flipping

Up scroll: Ctrl + B (before) or PgUp

Down scroll: Ctrl + F (After) or PgDn

(2) the copy operation

(A) copy cursor line: Key: YY

Paste: Where you want to paste press p key .

(B) the number of registration to the cursor behavior (including the current row), copies the specified row down

Key: Digital yy

(C) Copy visualized

Key: Ctrl + V , then press ↑ ↓ ← → arrow keys to select the area needs to be copied, press yy to copy it, and finally press the p key paste.

(3) cut, delete

(A) cutting, deleting cursor line

Key: dd (move on the next line after deletion) (idea: dd is strictly Cut command, but cut, and paste is not deleted effect)

(B) cut, delete quasi cursor behavior (including the current row), the next delete / cut line specified

Key: Digital dd (shift on the next line after deleting)

(C) cut, delete the current cursor row, but then delete the cursor does not move .

Key: D (after deleting the current row will be blank lines)

(4) undo / redo

Undo: Input "  : U " (not part of the command mode), or U  (Use the undo) (U lowercase u)

Recovery: Ctrl + R & lt     Recovery (canceled) before undo

(5) Extension: rapid movement of the cursor

(A) to quickly move the cursor to know the line.

Key: G

(B) subject to the current cursor up / down movement of the line n

Key: Digital ↑ , digital ↓ .

(C) subject to the cursor, left / right character n

Key: Digital ← , digital → .

Moves fast under (d), line mode: move to the specified line

Key: input English " : ", followed by the input line number number , press the ENTER key.

Switching between 5 mode  

 

6, line mode

Entry mode: enter the command mode, press " : " to enter

Exit way:

         (A): pressing the Esc

         (B): a continuous double-Esc

         (C): Delete the last line of all input characters

① save operation (write)
Input: " : W " Save File

Input: " : W Path " Save as

② Exit (quit)

Enter: " : q " to exit the file

③ Save and Exit

Enter: " : WQ " Save and Exit

④ Force Quit

Enter: " :! Q " represents a force quit, just do not save modification operations

⑤ search / find plug

Enter: " / keyword "

For example: Find a "sbin" keyword in the passwd file

Syntax: / sbin

In the search results to switch on / next result: N / n-   (Next)

If you need to cancel the highlight, you will need to enter: " : Nohl " [NO highlight]

⑥ replacement

: s / keyword search / new contents  replace matching content at a first cursor

: s / keyword search / new content / g  replace all qualifying content cursor

:% s / keyword search / new content to replace the entire contents of the document in first qualifying at each line  

% s / search keywords / new content / g: replace the contents of the entire document qualifying  

( % Represents full , G represents a global [Global])

⑦ display line numbers

Enter: " : the SET NU " [unmber]

If you want to cancel the display, enter: " : the SET nonu "

⑧ expansion: using vim simultaneously open multiple files, file switching line mode

Check the file name of the currently open: " : Files "

Switching mode file:

(A) If you need to specify the name of the file switch, you can enter " : Open file name already open ."

( A% [the Active, indicates the current file being opened], #: representation on an open file)

(B) may be switched on a file / files from other next command

Input: " : BN " (Back Next) is switched to the next file

Enter: " : BP " (the Back up) switch to a file

7, edit mode

Entry mode:

i : start inserting before the character under the cursor

A : Start inserted after the character at the cursor

Exit way: press the Esc key

9, practical function

(1) code coloring

Case: First, create a simple c language program

Display color: " : syntax ON " (syntax, grammar)

Close Color: " : syntax OFF "

Use (2) vim calculator

  1. Enter edit mode
  2. Press button "ctrl + R", and then enter the "=", then the cursor becomes the last row
  3. The input content to be calculated, press Enter

Extended:

①vim configuration (focus)

There are three configurations:

(A) when the file is opened in line mode input arrangement (temporary)

(B) Personal Profile ( # ~ / .vimrc , if not you can own new)

      (1) after the new well into the editing personal profile

      (2) is arranged in the configuration file, such as a display line number: #set NU

      After (3) configured vim to open the file will always display the line number

(C) global configuration file (vim comes, # / etc / vimrc )

Note: If a configuration item for the same personal profile exists, the individual configuration items prevail if the personal profile this one does not exist, the global configuration file prevail.

② abnormal exit

After editing the file and not normal to wq (save and exit), but encountered a sudden power failure or the terminal is turned off.

Solution: exchange files (temporary files created during programming) can be deleted. (If you can not find the swap file ls, we look with ls -a)

③ alias mechanism

Role: the equivalent of a custom command to create some of their own.

For example: there are cls command in Windows, under linux possible because there is no command to clear the screen without the habit. Now you can solve this problem by the alias mechanisms, can create their own cls command.

Alias alias mechanism relies on a mapping file: ~ / .bashrc

Note: If you want to create in order to take effect, the current user must log in again .

④ Exit way

" : Q " and " : WQ " and " : the X- "

(1) " : the X- " in the case file is not modified, it represents a direct exit in the case file modified representation to save and exit;

(2) If the file is not modified, but use " : WQ " conduct exit, then the file modification time will be updated; but if the file is not modified, use " : the X- " conduct exit, then the file modification time is not updated; mainly be confused finds time to modify user files.

Note: as used above is a lowercase x, X represents a capital letter for the encrypted file.

Reference: programmers dark horse liunx from entry to the master 

 

Published 57 original articles · won praise 36 · views 60000 +

Guess you like

Origin blog.csdn.net/hqy1719239337/article/details/89385344