Vim Text Editor

Source: Internet
Author: User

The VI command is the most versatile full-screen plain text editor in Unix operating systems and UNIX-like operating systems. The VI editor in Linux is called Vim, which is the enhanced version of VI (VI improved), fully compatible with the VI editor, and implements a number of enhancements.

The VIM function is very powerful and has many advantages:

    1. All UNIX like systems will have a VI text editor built in, and other text editors will not necessarily have

    2. Many of the software's editing interfaces will actively call VI (such as: Crontab,visudo commands)

    3. Vim has the ability to edit the program, can be doomed to font color to distinguish the correctness of the grammar, convenient programming;

    4. Simple program, fast editing speed


Use of VI:

1. Open the file with vim

Syntax: Vim [OPTION] ... FILE ...

Common options:

+#: After opening the file, just leave the cursor at the beginning of line #

+/pattern: Immediately after opening the file, leave the cursor at the beginning of the first line that is matched to the PATTERN

Vim + file opens file directly, the cursor is in the last line

VIM–B file binary mode open files

Vim–d file1 file2 ... Compare multiple Files

Vim-m file read-only open files

Ex file or vim–e directly into ex mode

If the file exists, the file is opened and the content is displayed

If the file does not exist, create it when you save it for the first time after editing

2, vi Open the file is divided into 3 modes:

① Command (General) mode ② edit (insert) mode ③ extended command-line mode (last-line mode)

There are three different ways to use three modes:

① Command mode:

Using VI to open the file directly into the command mode (the default mode), in this mode can be implemented to move the cursor, cut or delete text.

② edit mode:

In the command mode can cut the deletion of text, but cannot edit the contents of the file, in the command mode by pressing "i,i,o,o,a,a" any one of the keys to enter the editing mode, the lower left corner will display the insert typeface, you can edit the text content. Press ESC to return to command mode when editing is complete

③ Extended command-line mode:

In command mode, press ":,/,?" "Any of the keys to enter the extended command line mode, the cursor will move to the bottom line, to find, replace characters, save text, exit and so on.


ESC key to exit the current mode,

Esc,esc always returns to command mode


Mode conversion:

Command mode----> Edit mode

I:insert, enter at the cursor position

I: Input at the beginning of the line where the current cursor is located

A:append, enter after cursor location

A: Enter at the end of the line at the current cursor

O: Opens a new line below the line where the current cursor is located

O: Opens a new line above the line where the current cursor is located

Edit mode----> Command mode

Esc

Command mode----> Extended command-line mode

: , / , ?

Extended command-line mode----> Command mode

Esc

Locking and unlocking: ctrl+s,ctrl+q

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/85/A4/wKiom1eq0-PBTTx1AABSDGfZ1lw241.png "title=" 1.png " alt= "Wkiom1eq0-pbttx1aabsdgfz1lw241.png"/>

Command mode related operations:

Cursor Jump:

Jump between characters:

H: Left L: Right J: down K: Upper

#COMMAND: Jumps the number of characters specified by #;

Jump between words:

W: The first word of the next word

E: The ending of the current or next word

B: The first word of the current or previous word

#COMMAND: Specifies the number of words to jump by # at one time

Current page Jump:

H: Top m: Page Middle row L: Bottom of page

Beginning line End Jump:

^: jumps to the first non-whitespace character at the beginning of a line;

0: Jump to the beginning of the line

$: Jump to end of line

Move between rows:

#G, extension mode: #: Jump to line specified by #

G: Last line

1G, GG: First line

Move between sentences:

): Next sentence (: Previous

Move between paragraphs:

}: Next paragraph {: Previous paragraph

Turn screen operation:

CTRL+F: Flip a screen to the end of the file

CTRL+B: Flip a screen to the file header

Ctrl+d: Turn half screen at the end of the file

Ctrl+u: Turn half screen to file header

Character editing:

X: Delete the character at the cursor;

#x: Delete the # characters at the beginning of the cursor

XP: Swap the position of the character where the cursor is located and the character after it

~: Convert Case

Replace command (R, replace)

R: the character at which the cursor is replaced

R: Switch to replace (replace) mode

Delete command:

X,x: In one line of text x is to remove a character (equivalent to the DEL key) backwards, and X to delete one character (equivalent to the BACKSPACE key)

Nx:n is a number that continuously deletes n characters backwards.

D: Delete command, can be combined with the cursor jump character, to achieve range deletion;

d$: Delete to end of line

d^: Delete to non-empty header

D0: Delete to the beginning of the line

DD: Delete cursor in the row

DW: Delete One word backwards, delete to the beginning of the next word (delete the space after the word)

De: Remove a word backwards to delete the ending of the word that was deleted

DB: Delete a word forward

Ndd:n is the number that deletes the downward n line of the cursor,

D: Always delete from the current cursor position to the end of the line, leaving blank lines, equivalent to d$

Copy command (y, yank):

Y: Copy, behaves similar to D command

y$: Copy to end of line

Y0: Copying to the beginning of the line

y^: Copy to non-empty header

Ye: Copy to ending

YW: Copy to the beginning of the next word (copy includes space after word)

YB: Copy a word forward

YY: Copy entire row

#yy: Copy # lines

Paste command (P, paste):

P: If the buffer is an entire row, paste the current cursor below the row, or paste it at the end of the current cursor.

P: If the buffer is an entire row, the current cursor is pasted above the row, otherwise, it is pasted to the front of the current cursor position.

100iwang [ESC]: Paste "Wang" 100 times

changing commands (c, change)

C: Switch to insert mode after modification,

Combined with the cursor jump character ($,^,0,w,e,b), delete the characters in the Jump range and enter edit mode.

CC: Delete the current line and enter new content

#cc: Delete # lines and enter new content

C: Delete the current cursor to the end of the line and switch to insert mode

Area modification

<start Position><command><end position>

Command:

Y copy, d Delete, gu to uppercase, gu to lowercase

For example, the 0y$ command means:

0→ to the wardrobe first.

y→ start copying from here

$→ copy to the last character of the bank

Ye copy from the current position to the last character of this word

Undo Changes

U: Undo Recent Changes

#u: Undo many changes before

U: Undo the change of all this row after the cursor falls on this line

Press Ctrl-r to redo the final undo change

. : Repeats the previous action

N.: Repeats the previous action n times


To extend the command-line mode operation:

Press ":" To enter ex mode

Create a command prompt: On the left side of the screen at the bottom

Command:

W Write (Save) disk File

Wq Write and exit

X Write and exit

Q exit

Q! Do not save the log out, even if the changes are lost

R filename reads the contents of the file into the current file

W FileName writes the current file contents to another file

!command

R!command

Address delimitation

: Start_pos,end_pos

#: The specific # line, for example, 2 means line 2nd;

#,#: From the left # indicates the line start, to the right # indicates the end of the line

#,+#: The start of the line from the left #, plus the number of rows on the right # representation

: 2,+3 = 2 to 5 rows

.: When moving forward

$: Last line

., $-1 current line to penultimate line

%: Full text, equivalent to 1,$

/pat1/,/pat2/: Starting from the first line that is matched to the pat1 pattern, until the end of the line that was first matched to the PAT2

#,/pat/: From line # to match to pattern straight room

/pat/,$: Match to the pattern to the last

How to use: followed by an edit command

D: Delete the content within the match definition

Y: Copy the content within the definition

W File: Save a range of rows to a specified file

R file: Inserts all content from the specified file at the specified location

Find:

/pattern: Looks at the end of the file from the current cursor location

? PATTERN: Finds the file header from the current cursor location

N: Same direction as command

N: Opposite direction of command

Find and replace

S: Complete the Find and replace operation in extended mode

Format: s/What to look for/replace with content/modifiers

What to look for: Available modes

Replace with: cannot use mode, but can use \1, \2, ... You can also use the "&" reference to find the entire contents of the previous lookup


Modifier:

I: Ignore case

G: global substitution; By default, each row replaces only the first occurrence of the

GC: Global substitution, asking before each replacement

Find separators in substitutions/can be replaced with other characters, such as

[Email protected]/[email protected]/[email protected]

s#/boot#/#i


Cases:

1. Copy the/etc/profile to/tmp/directory and use the Find replacement command to remove the white space characters from the beginning of the/tmp/profile file

①cp/etc/profile/tmp

②vim/tmp/profile

③: Type the extended command-line mode to perform the%s#^[[:space:]]\+# #g

2. Copy the/etc/rc.d/init.d/functions file to the/tmp directory, and add a # number to the beginning of the line beginning with a blank character for each line of/tmp/functions with the Find replacement command

①cp/etc/rc.d/init.d/functions/tmp

②vim/tmp/functions

③:%s/^[[:space:]]/#&/g


Vim has a very powerful function, that is, the VIM register, equivalent to the Clipboard in the window, only one window, but a full of 27 vim, fully meet the user's various copy cut.

Vim Registers

There are 26 named registers and 1 unnamed registers, which often contain different content of the Clipboard, which can be shared between different sessions.

With a A, B,..., Z, with "register representation, placed between the number and the command

Example: 3 "tyy means copying 3 rows to the T register

"TP means to paste the contents of the T register

Unspecified, no named registers will be used

There are 10 digital registers, denoted by 0,1,...,9, 0 stores the most recently copied content, and 1 stores the most recently deleted content. When new text changes and deletions, 1 dumps to 2, 2 dumps to 3, and so on. Digital registers cannot be shared between different sessions

Editing a binary file

Open a file in binary mode

Vim–b Binaryfile

Extended command mode, using the XXD command to convert to a readable hexadecimal

:%!xxd

Editing a binary file

In extended command mode, use the XXD command to convert back to binary

:%!xxd–r

Save exit

Visualization mode

Allow selection of text blocks

V Character-oriented

V Line-oriented

CTRL-V Block-oriented

Visualization keys can be used in conjunction with the move key:

W,),}, arrows, etc.

Highlighted text can be deleted, copied, changed, filtered, searched/replaced, etc.

Multi-file Mode

Vim FILE1 FILE2 FILE3 ...

command in extended command line mode:

: Next Next

:p Rev A previous

: First One

: Last One

: Wall Save All

: Qall Quit all

: Wqall Save All and exit

Use more than one "window"

Multi-file Segmentation

Vim-o|-o FILE1 FILE2 ...

-O: Horizontal split

-O: Vertical split

Switch between windows: Ctrl+w, Arrow

Single File Window segmentation:

Ctrl+w,s:split, Horizontal split

Ctrl+w,v:vertical, Vertical split

Ctrl+w,q: Canceling adjacent windows

Ctrl+w,o: Cancel All windows

: Wqall exit


You can also customize your own vim for the convenience of studying and working.

Customizing the working characteristics of vim

Configuration file: Permanently valid

Global:/ETC/VIMRC

Personal: ~/.VIMRC

Extended mode: Current VIM process is valid

(1) Line number

Display: Set number, abbreviated as set Nu

Cancel display: Set Nonumber, abbreviated to set Nonu

(2) pairs of parentheses match

Match: Set Showmatch, abbreviated as set SM

Cancel: Set NOSM

(3) Auto Indent

Enable: Set AI

Disabled: Set Noai

(4) Highlight Search

Enabled: Set Hlsearch

Disabled: Set Nohlsearch

(5) syntax highlighting

Enabled: Syntax on

Disabled: Syntax off

(6) Case of ignoring characters

Enable: Set IC

Do not ignore: set Noic

(7) file format

Enabled: Fileformat=unix

Do not ignore: Fileformat=dos

(8) Set text width

: Set textwidth=65 (vimonly)

: Set wrapmargin=15

: Help Option-list

: Setor:set All

Vi/vim built-in Help

: Help

: Helptopic

Use:qto Exit Help

Vim comes with a practice tutorial: Vimtutor

Cases:

1. How do I set the tab indent to 4 characters?

Set tabstop=4, shorthand set ts=4.

Permanent entry required to write to configuration file/ETC/VIMRC or ~/.VIMRC

2. Copy the/etc/rc.d/init.d/functions file to/tmp directory, replace the/etc/sysconfig/init in the/tmp/functions file with/var/log;

①cp/etc/rc.d/init.d/functions/tmp

②vim/tmp/functions

③%[email protected]/etc/sysconfig/[email Protected]/var/[email protected]

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/85/A4/wKiom1eq1smyH8r9AAAhmj1oc9c657.png "title=" 2.png " alt= "Wkiom1eq1smyh8r9aaahmj1oc9c657.png"/>650) this.width=650; src= http://s1.51cto.com/wyfs02/M00/85/A3/ Wkiol1eq1tirugs8aaaezslwqyu682.png "title=" 3.png "alt=" Wkiol1eq1tirugs8aaaezslwqyu682.png "/>

3. Delete the # at the beginning of all lines in the/tmp/functions file that begin with # and have at least one blank character after #;

:%[email protected]\ (^#\) \ ([[: Space:]]\+\) @\[email protected]

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/85/A4/wKiom1eq1vSgkM-sAAATNx51uAE957.png "title=" 4.png " alt= "Wkiom1eq1vsgkm-saaatnx51uae957.png"/>

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/85/A3/wKioL1eq1wGiH_OXAAARxJOZmZs552.png "title=" 5.png " alt= "Wkiol1eq1wgih_oxaaarxjozmzs552.png"/>

This article is from the "I ' m Groot" blog, so be sure to keep this source http://groot.blog.51cto.com/11448219/1836559

Vim Text 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.