Introduction to the use of vim in text editing tools

Source: Internet
Author: User
Tags modifier line editor

1. Text Editor:

Plain text editor, ASCII code (127 characters), Unicode (Globally unified encoding)


2. Text Editor Category:

Line Editor: SED

Full Screen Editor: Nano, VI, VIM


3, VIM editor mode introduction:

Edit mode: (also called command mode)

Input mode:

Last-line mode: Built-in command-line interface

4. Convert between VIM editor modes:

Edit mode--Input mode

I:insert; Enter at the cursor position

A:append: Enter at the cursor location

O: Opens a new line below the cursor location

I: Enter at the beginning of the line where the cursor is located

A: Enter at the end of the cursor line

O: Open a new row above the line where the cursor is located

Input mode--edit mode

ESC key

Edit Mode--last-line mode


Last-line mode--edit mode

ESC key


5. Vim New or Open file

Format:

vim [options] [File ...]

Vim [Options]-

Vim [options]-t tag

Vim [Options]-Q [ErrorFile]

Option

+#: When the file is opened, the cursor is directly at the beginning of line #, and if not #,+, it is at the beginning of the end of the file;

+/pattern: When the file is opened, the cursor is placed directly at the beginning of the first matching word lines the pattern match.


6. Vim quit

ZZ: In edit mode, enter two uppercase Z

Q: Do not save exit, enter Q in last line mode

Q! : Force not to save exit, last line mode

Wq: Save exit, last line mode

X: Save exit, last line mode

W/path/to/somefile: Save to the specified path file, in the last line mode


7. Related operations in edit mode (i.e. command mode)

7.1, the implementation of the cursor jump: between the characters jump, the word jumps, the beginning of the line end of jump, line/sentence between jump, between sections jump

Jump between characters:

H: Jump to one character to the left

L: Jump Right One character

J: Jump down one character

K: Jumps up one character

#[hjkl]: Jump to the corresponding direction of # characters, such as 2h, jump to the left 2 characters at a time

Jump between words:

W: Jump to the beginning of the next word;

B: Jump to the beginning of the current or previous word

E: Jumps to the ending of the current or next word;

#[wbe]: Jump to the specified # word;

Beginning line End Jump:

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

0:0; Jump to beginning of line, absolute beginning (including whitespace characters)

$: Jump to end of line, absolute end of line, including whitespace characters

Inline Jump:

#G: Jump to the specified # line

1 g: Jump to the first line

G: Jump to the last line;

GG: Jump to the first line

Jump between sentences:

): Jumps to the next sentence (closing parenthesis)

(: Jump to the previous sentence (opening parenthesis)

#[()]: Jump to the # clause

Jump between Segments:

}: Jump to next paragraph

{: Jump to previous paragraph

#[{}]: Jump to paragraph #


7.2, turn screen operation:

Ctrl+f: Turn a screen at the end of the file;

Ctrl+b: Turn one screen to the file header;

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

Ctrl+u: Turn half screen to file header

Enter: Turn backward by line


7.3. Commands in edit mode

There are the following: character editing, substitution, delete, paste, copy, change, etc.

Character Editing commands:

X: Delete the character at the cursor location

#x: Deletes the specified number of # characters at and after the cursor

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

Replace command: Replace

R: the character at which the cursor is replaced, and R followed by a single character to be replaced;

Delete command: D

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

[#]d[hjklweb]: Combine cursor jump character operation

DD: Delete the line where the cursor is located

#dd: Delete the total # line where the cursor is located and after it;

Paste command: P

P: lowercase p; If the contents of the buffer are whole lines, paste at the same line as the current cursor, or paste to the back of the current cursor if it is not an entire row;

P: uppercase p; If the contents of the buffer are whole lines, paste at the top of the current cursor, or paste to the front of the current cursor if it is not an entire line;


Copy command: Yank,y

Y: Copy, work behaves similar to "D" command

YY: Copy a whole line

#yy: Copy Total # lines

Y[^$0web]

Change command: C

Changing command c is just like removing command D, which is more than the mode switching of the D command. Switch from edit mode to input mode.

C:

#cc


Undo command:

U: Undo previous actions, default up to 50 undo

#u: Undo Previous # Operations


To revoke an earlier revocation order:

Ctrl+r: Restores the previous undo operation.


Repeat the previous edit operation:. (Point number)

.


Visualization mode: Support the upper and lower left and right key box selection.

V: Lowercase v, selected by character

V: Uppercase V; select by row;


Vim comes with a practice tutorial: Vimtutor


8, the last line mode operation:

The last-line mode is the built-in command-line interface.

He includes the following: address definition, find, find and replace operations.


8.1, address definition: can be used together with the editing command, to achieve editing operations, such as: d,y,c,w,r; Note that the edit operation is directly followed by the definition, no additional space

Address definition Format:

: Start_pos[,end_pos]//Start position [, end position]

: #: # line;

For example:: 5 for line 5th,

: #,n: Specify range, start line on left, end line on right

For example:: 9,100: From line 9th to line 100th

: #,+#: Specifies the line range, the left is the absolute number of the starting line, the right side is the relative left offset;

such as:: 3,+7: Indicates from line three to line 10th

:. : Click OK to indicate the current line.

: $: Last line

For example::., $: Represents the current line to the last row

:%: Full text

Equivalent: 1,$

:/pattern/: The first time a row is matched by a pattern, starting at the cursor location

such as::/fstab/: Indicates finding the first match to the fstab.

:/fstab/,$: Represents the row from the Fstab to the last row from where the cursor was first matched.

:/pattern1/,/pattern2/: The first time to match the pattern 1 to the first time by the pattern of two matches to the forehead line, rarely used.


The address definition is followed by an edit operation:

: 5w/path/to/somefile: Write the 5th line to a specific file

: 5r/path/from/somefile: Append the contents of other files after line fifth

: W/path/to/somefile: Saves the text in range to the specified file

: R/path/from/somefile: Reads and inserts the text from the specified file into the specified position;


8.2. Find

:/pattern: Finds all strings that can be matched to the current pattern from the location of the current cursor to the end of the file, and the difference between the/pattern/and the address definition:

:? Pattern: finds all strings that can be matched to the current pattern from the current cursor location to the file header;


Note:/pattern and? Pattern can be positioned with N and N, N is the same as command direction, N is opposite to command;


8.3. Find and Replace: s

Format:: s/What to look for/replace with/[modifier]


What to look for: You can use regular expressions

Replace with: You cannot use a regular expression, but you can refer to it.

Note: If the "What to find" section uses a grouping symbol in the pattern, use the latter reference in the replaced content, and use the & symbol to refer to all the text that the lookup pattern matches directly.

Modifier:

I: Ignore case when finding

G: Global substitution. The default is to replace the first occurrence in each row

Note: If the delimiter is conflicting with the found or replaced content, you can replace the delimiter "/" with another non-characters commonly used character, such as @, #

: [email protected] What to look for @ Replace with content @[modifier]

: s# What to look for # Replace with content #[modifier]

such as::%[email protected]\<t\ ([[: Alpher:]]\+\) \>@t\[email protected]//match the words to the beginning of T, then replace the words with T

:%[email protected]\<t[[:alpher:]]\+\) @&[email protected]//match the word to the beginning of T, followed by ER

:%[email protected]#@@//Replace the line that begins with the # sign with an empty


Exercise 1: Copy the/etc/grub2.cfg file to the/tmp directory and use the Find replacement command to delete the beginning of the line with whitespace characters in the/tmp/grub2.cfg file

[Email protected] ~]# cp/etc/grub2.cfg/tmp/

:%s/^[[:space:]]\+//


Exercise 2: Copy the/etc/rc.d/init.d/functions file into TMP and use the Find Replace command to add "#" to the beginning of each line in the/tmp/functions file that starts with a white space character

[Email protected] tmp]# cp/etc/rc.d/init.d/functions/tmp/functions

:%s/^[[:space:]]\+/#\1/g


Exercise 3: Add # to the beginning of the first three lines of the/tmp/grub2.cfg file

: 1,3s/.*/#&/


Exercise 4: Replace all enabled=0 in the/etc/yum.reposd/centos-base.repo file with enabled=1, all gpgcheck=0, and replace with gpgcheck=1

[Email protected] tmp]# Cp/etc/yum.repos.d/centos-base.repo/tmp/centos-base.repo

:%s/\ (enabled\|gpgcheck\) =0/\1=1/g


9. Vim Multi-file function

Vim file1 file2 ...//new, open multiple files, open the first one by default, switch in the newly created file, enter in the last line mode: Next (Next file), prev (previous file), first (one file), last (final file).


Exit all files, enter in the last line mode:

Wqall: Save to exit all files

Wall: Save All Files

Qall: Roll out All Files

such as: [[email protected] tmp]# vim fstab issue//Open Fstab, issue file, if not present, new.


9.1. The Multi-window function of vim file:

Multi-window display of multiple files:

Vim-o file1 file2//lowercase "o" horizontal split showing both file1 and file2

Vim-o file1 file2//Capital "O" vertical split displays both File1 and file2

Switch between file windows: Press and hold ctrl+w to release and select the text window you want to manipulate with the left and right arrows.


Multi-window display of single file:

After opening the file, press and hold Ctrl+w release, press S key to split horizontally;

After opening the file, press and hold ctrl+w release, press v key to split vertically;

Close the expanded window and exit the operation normally.


10, custom Vim's working characteristics:

The setting in the last line mode is only valid for the current VIM process;

If you want the VIM feature to be permanently valid, you need to modify the configuration file:

Global configuration file:/ETC/VIMRC

Personal profile: ~/.VIMRC//You may need to create this file yourself.

such as: VIM/ROOT/.VIMRC

Set number//displays line numbers.


Vim feature: (set in last-line mode)

Line number:

Display Line numbers: Set number shorthand for set Nu

Cancel line number: Set Nonumber shorthand is set Nonu

Parentheses match, highlighting:

Match display: set Showmatch; set SM

Cancel display: Set Noshowmatch;set NOSM

Auto indent:

Enable indentation: Set AI

Deactivate indent: Set Noai

Highlight search results:

Enable settings: Set Hlsearch

Deactivate settings: Set Nohlsearch

Syntax highlighting:

Enable settings: Syntax on

Deactivate settings: Syntax off

Ignore character case: (find modifier in Replace g)

Enable settings: Set IC

Deactivate settings: Set Noic


11. The help command in the last line mode:

Help

Help keyword



12. Set tab indent to four characters in Vim editor

Add the following code in. VIMRC to achieve 4 spaces by pressing the TAB key

Set ts=4 (Note: TS is an abbreviation for TabStop, set tab width 4 spaces)

Set Expandtab


























Introduction to the use of vim in text editing tools

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.