10th Chapter VIM Program Editor

Source: Internet
Author: User

VI and VIM

Vim is an upgraded version of VI that supports all VI directives

Use of VI

VI is divided into three modes: General mode, edit mode, command line mode

General Mode

Open a file with VI directly into the general mode, this mode can use the left and right buttons to move the cursor, you can delete characters, delete the whole line, copy and paste

edit Mode

In general mode, you cannot edit the contents of a file. Enter edit mode after pressing any of the letters "I,l,o,o,a,a,r,r", and press ESC to exit edit mode

command-line mode

In the general mode, enter ":,/,?" "Any one of these three characters, you can move the cursor to the bottom row." In this mode, you can read, save, a large number of substitutions, exit VI, display line numbers and other operations

Key Instructions

How to move the cursor

Ctr+f The screen moves down one page (page down)
Ctr+b Screen up one page (page UP)
0 or home Move to the front of this line
$ or end Move to the last side of this line
G Move to the end of the file
Gg Move to the first line of the file
N[enter] Move the cursor down n rows
/word Look down
? Word Look up
N Repeat the previous lookup
N Reverse Lookup

: n1,n2s/word1/word2/g

Find word1 between lines N1 and N2 and replace with Word2
: N1,N2S/WORD1/WORD2/GC Ibid. confirm to user before replacing
: 1, $s/word1/word2/g First line to last row, find replacement
X,x In one row, X deletes a character backwards, and X deletes one character forward
Dd Delete a row of the cursor
Ndd N is a number that deletes the downward n rows (including the bank) where the cursor is located
Yy Copy the line where the cursor is located
Nyy N is a number, and the N line where the cursor is copied
P,p P is copied on the next line of the cursor, p is copied on the previous line of the cursor
U Restore previous action
Ctrl+r Redo Last Action
.

decimal point, repeating the previous action

General mode switch to edit mode available button description

i,l i is inserted from the current cursor, To begin inserting the
a,a a is inserted from the next character in the cursor, A is from the end of the line where the cursor is located. A word prompt insert
o,o o to insert on the next line of the cursor, O to insert on the previous line of the cursor
r,r Enter replace mode, R will replace the character of the cursor only once, R will be replaced forever.

General mode switch to command line mode

: W Save
: w! Force Save
: Q Leave VI
: q! Forced leave not saved
: Wq Leave after saving
: w [filename] Save As FileName
: R [FileName] When editing data, read in another file data, and the data is placed on the next line where the cursor is located
: n1,n2 w [filename]

Save N1 to N2 row of data to file filename

: Set Nu Show line Numbers
: Set Nonu Cancel line number
VIM's warning message when saving files, resuming and opening

Vim when you edit a file, a. filename.swap file is created to save the operation record.

When the vim edit file process is unexpected, the staging file does not disappear. Opening the original file at this point may appear as follows:

E325:attentionfound a swapfileBy the name". TEST2.TXT.SWP"owned By:root Dated:thu June -  +: +: -  .         fileName:/tmp/wuchao_test/test2.txt modified:yes User name:root host Name:localhost.localdomain process ID:12201While openingfile "Test2.txt"Dated:mon June -  -: -: -  .(1) Another program may editing the samefile. If this is the Case, be careful isn't to end up with different instances of the samefileWhen making changes. Quit, or continue with caution. (2) an edit session forThisfilecrashed. If This is the Case, use": Recover"Or"vim-r test2.txt"To recover the changes ( see": Help Recovery"). If you do this already, delete the swapfile ". TEST2.TXT.SWP"To avoid the this message. Swapfile ". TEST2.TXT.SWP"Already exists![O]pen Read-only, (E) dit anyway, (R) Ecover, (D) elete it, (Q) uit, (A) Bort:

When Vim is a file, if the file corresponding to the staging file is detected, the above information will be displayed, you can do the following:

[O]: Open this file as read-only

[E]: Open file in normal mode

[R]: Load the contents of the staging file, save the contents of the previous unsaved

[D]: Delete the staging file

[Q]: Exit

[A]: Exit

Vim's function

In addition to the functions of the above VI, VIM has its own instructions.

Block Selection
V Character selection
V Row selection
Ctr+v Block selection
Y Copy the selection section
D Remove the selection section
Multi-file editing
: N Edit Next file
: N Edit Previous file
: Files List the currently open files for vim

Example:

/tmp/wuchao_test/under the two files, respectively, Inittab and hosts, now to the hosts text to copy part of the content into the Inittab file, the operation is as follows:

[[email protected] wuchao_test]# vim./inittab./hosts## Ctrl-alt-delete is handled by/usr/lib/systemd/system/ctrl-alt-del.target## SYSTEMD uses ' targets  instead of Runlevels. By default, there is both main targets:## multi-user.target:analogous to runlevel 3# graphical.target:analogous to RunLevel 5# ~ ~: Files 1%a  "./inittab  "line 1 2  "./hosts" line 0press ENTER or type command to continue           

In Vim, enter ": File" To view the currently edited file, enter ": N" can be toggled within multiple files.

Multi-window function

Imagine that we need to edit a very large file, we want to control in a file, or more than one file control, if the page up or down, or constantly switch vim open files, it is too troublesome. VIM provides a multi-window function.

Vim after opening a file, you can use: SP [filename] opens a window to open another file, if you open this file, direct input: SP, you can use the ctr+w+ up and down arrows to switch windows, CTR+W+Q exit the current window,: Q can also.

10th Chapter VIM Program Editor

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.