Linux under VI && VIM

Source: Internet
Author: User

VI under the basic divided into three modes of general mode, command line mode, editing mode

VI a file after entering the general mode in this mode you can move the cursor, copy, paste, delete file data

General mode input i,i,a,a,o,o,r,r into the editing mode, you can edit the text, press [ESC] exit edit mode into the general mode

General mode input:,/,? Enter command line mode, in command line mode can find, save, replace, exit and other operations


Three different modes
General mode

Cursor movement

Move the cursor up or down the keyboard, move the NUM line up or down num+

[Page UP]: Turn up one page

[Page DOWN]: Turn down one page

0 or home: The cursor moves to the first character of the current line

$ or end: The cursor moves to the last character of the current line

H: Cursor moves to the first line of the current page

M: Cursor moves to the middle row of the current page

L: Cursor moves to the last line of the current page

G: The Light Table moves to the last line of text

NG: Cursor moves to nth line of text

GG: Cursor moves to the first line of text

N[enter]: Move the cursor down n rows

Find replacements

/word: Look down for a word string

Word: Find a word's string up

N: Repeat the previous search job

N: Opposite to N, reverse lookup of the last character to look up

: n1,n2s/word1/word2/g: Replace word1 Word with word2 Word between N1 line to year N2 line

: 1, $s/word1/word2/g: replace word1 with Word2 on the first and last line

: 1$s/word1/word2/gc: Replace Word1 with Word2 from the first line to the last line, and let the user confirm before each word is replaced

Delete, copy, paste

X,x:x deletes the character that the current cursor refers to, and moves the cursor back to delete it. x Deletes the previous character of the cursor, which is deleted forward.

NX: Remove n characters backwards

DD: Delete When moving forward

NDD: Delete cursor down n rows

YY: The line where the cursor is copied

NYY: Copy cursor down n rows

P,p:p is pasted on the next line of the current line, and P is pasted on the top row of the current row

U: Restore previous action

[Ctrl]+r: Redo Last Action


General mode switch to edit mode

Enter insert mode

I,i:i inserted from the current cursor, I is the first non-space Word prompt the current line insert

A,a: Insert from the next character where the cursor is currently located, a from the last character of the current line

O,o:o the next line where the cursor is inserted, O the previous line where the cursor is inserted

Enter replacement mode

R,r:r only replaces the character of the cursor, and R will be replaced consistently until [ESC] is pressed


General mode switch to command line mode

: W writes the edited data to the hard drive (save)

: w! Force the edited data to be written to the hard disk (if it is read-only mode)

: Q Leave VI

: q! have been modified, but do not want to store, forced to leave

: Wq Save and exit

: wq! Force Save and exit

: W[filename] Save edited data to another file

: R[filename] When editing data, add data from another file to the line following the cursor

: n1,n2 w [filename] saves data from N1 to N2 row in the filename file

:! Command command is temporarily left out of line mode and then back

: Set NU Displays line number

: Set Nonu does not display line numbers


Vim function


Vim File Recovery function

When you edit the file is not properly cut off when you create a. filename.swp temporary file in the current directory, when you want to open this file here, you will be prompted if you want to restore the previous action. O is read-only for the file, E is directly edited, R is the redo before the restore operation, D is to think that this file is useless to delete this staging file, Q is to exit directly. After the operation, except D, you need to manually delete this file, or you will be prompted each time.


Vim is the premium version of VI, the general vim has already replaced VI. Because VIM has a color hint function, but also supports a variety of program syntax.


VIM's block selection

V: Character selection, where the cursor moves so that it is anti-white

V: Row selection, the cursor moves the line is anti-white

[CTRL]+V: Block selection, the cursor moves over the block will be anti-white

Y: Copy the anti-white place

P: Paste the previously copied

D: Remove the anti-white place


Vim Multi-file editing

Vim File1 file2

First enter the File1 to edit, enter: N will go to the next file for editing;: N will go to the previous file editor;: Files will list the file that vim opens


Vim Multi-window function

When you open a file, if you let the file display multiple windows

: SP can

If multiple windows open a different file

: SP [filename]

Up/Down window Toggle [Ctrl] +w


Vim Usage History

If you've used vim, you'll create a. viminfo file in your home directory that records the actions you've done before.


Vim environment settings

Enter set all in command-line mode to view all vim settings. The line number can be displayed through set XX settings, such as Set Nu.

This method is only valid in the current, if it is necessary to modify/ETC/VIMRC (Ubuntu is/ETC/VIM/VIMRC) to set our accustomed to the VIM operating environment, the overall vim is set here, but it is generally not recommended to modify this file, you can modify the ~./. VIMRC This file does not exist by default and can be created manually

Like what:

Vim ~/.VIMRC

Set Nu "Setting line number
Set Hlsearch "High brightness anti-white
Set autoindent "Auto Indent
Set backspace=2 "Can be deleted at any time with backspace key
Set ruler "show the bottom tips
Set Bg=dark "show different shades of background
Syntax on "syntax check, color display


Garbled solution

Terminal display garbled for a lot of reasons, Linux support language, terminal interface family (with Lang set), the source code, open terminal software such as GNOME, here is a method can be real-time, if the file is BIG5 encoded

Lang=zh_cn.big5

Then terminal, set character encoding, Chinese (BIG5)


Language encoding Conversion

You can use ICONV (you need to download Iconv and then execute commands in that directory) and then use Iconv for language conversion

Iconv--list

Iconv-f Original code-t new code Filename[-o NewFile]


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Linux under 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.