"Linux acquaintance" vim editor

Source: Internet
Author: User
Tags modifier

Vim is a well-known powerful, highly customizable text editor like VI, which improves and adds many functions on the basis of VI, VIM is free software, and today we talk about the use of vim.

This article is based on the Vim editor on Centos7, and the default CENTOS7 does not have VIM installed and Vim can be installed using Yum-y install VIM.

Four modes and transformations of VIM

Command mode: Control the screen cursor movement, the deletion of text, copying and other text editing work, when using VIM to open a file, the default mode is the command mode

Insert mode: You can enter text only in insert mode

Last-line mode: Save the file or exit Vim, and also set the editing environment and some compilation work, such as listing line numbers, looking for strings, etc.

Visual mode: You can use the mouse box to select text, more humane

In particular, there are several ways in which you can do this in command mode to insert mode:

I:insert, enter a:append at the cursor location, enter o at the cursor location: Open a new line below the cursor line I: Enter a at the beginning of the line where the cursor is located: Enter o at the end of the line where the cursor is located: Open a new line above the line where the light-year is located

Exit file

In command mode:

ZZ: Save and exit

In the last line mode:

  Q: Exit  q!  : Force quit, do not save previous edits  Wq: Save and Exit  x: Save and exit   w: Save, or save to a file  w  /path/to/ Somefile

Example: We copy a file cp/etc/rc.d/init.d/functions/tmp/functions.txt, then manipulate/tmp/functions.txt this file, insert "# Hello Linux" in the second line, Save exit.

Cursor Jump

Operating in command mode

Jump between characters

H: One character to the left L: one character to the right K: up one character J: down one character

Of course, it could be the upper and lower arrows on the keyboard

Can be combined with a number command, e.g. press "2 H" to move the cursor to the left 2 characters

Jump between words

W : The first word of the next word e: The ending of the current or last word B: the beginning of the current or previous Word can be combined with a number command, for example " 2 W   ": The first word of the second word after the current word

Beginning line End Jump

^: Jump to the first non-whitespace character at the beginning of the line 0: Jump to the beginning of $: jump to the end of a row

Inline jump

#G: Jumps to the line specified by # 1g,gg: First line G: Last row

Inter-sentence jumps and inter-end jumps (not commonly used)

): Next sentence (: previous sentence}: Next paragraph {: Previous paragraph

Flip Screen

CTRL +F: Flip a screen to theend of the file Ctrl +B:turn to the file header Ctrl + D: Flip the end of the filehalf-screen ctrl+u: Turn the file header half-screen enter: Turn backward by row

Vim's editing commands

Character editing

X: Delete the current character #x: Delete the # characters starting at the cursor XP: The position of the character at the swap cursor and its following character

Replace command (replace)

R: the character R + substitution character at which the cursor is replaced

Delete command

d: Delete command, cannot use d$ alone: Delete current character to line end of line D^  : Deletes the current character to the beginning of the line (does not delete the character of the cursor) DW: Delete The word where the cursor is located to the beginning of the next word de: Delete the position of the word at the cursor to the ending db of the next word: Delete the position of the word where the cursor is located to the first DD of the word : Delete the line where the cursor is located
Can be used in combination with the number: "3 dd": Delete the line where the cursor is positioned and the following line, a total of 3 rowsNote: The deleted content is saved in the buffer

Paste command

P: The content in the buffer is pasted below the current row if it is an entire row, otherwise, paste to the back of the current cursor at the back of P: the contents of the buffer are pasted above the current row if the entire line is present, otherwise, paste to the forward at the current cursor location

Copy command

y: Copy, cannot be used alone y$: Copy cursor position to end of line y^: Copy cursor position to beginning, without cursor character ye,yw: Copy the position of the word with the cursor to the final YB: Copy the position of the word from the cursor to the beginning of the word yy: Copy a whole line

Change command

and D is the same, delete the file, the difference is that after using C, will go directly into the insert mode

C $: Delete the cursor to the end of the line and enter the insertion mode c.^: Delete the cursor position to the beginning and enter insert mode CE,CW: Delete the position of the word where the cursor is located to the tail of the word CB: Delete the position of the word where the cursor is located to the word header

Other editing operations

Use V and V to enter visualization mode in command mode

V: Selected by character V: selected by row

Once selected, you can combine D (delete), C (Delete and enter insert mode), Y (copy) to manipulate

Vim end-of-line mode

Address delimitation

N: A specific nth row, listed in:5, that is, line 5th.: Current Line $: Last line n1,n2: Specify row range, left Start row, end line N1 on right, +n2: Specify row range, left Start row, offset% right : Full text /pattern/: From the beginning of the cursor to the end of thefile the first time to match the pattern to the line /pat1/,/pat2/: Starting at the beginning of the cursor, the first line from the PAT1 match to the beginning, to the first time by Pat2 match to all the rows

Can be used in conjunction with D,Y,C, you can also use W to save the range of text to the specified file, using R/path/from/somefile to read and insert the text in the specified file into the specified location.

Find

/Pattern: finds all strings that can be matched by a pattern from the end of the current cursor to the end of the file? pattern: Finds all strings that can be matched by a pattern from the current cursor location to the file header N: View the next  N: View Previous

Find and replace

S: command for last-line mode

Format: s/What to look for/replace content/ modifier to find: What you can replace with regular expressions: You cannot use regular expressions, but you     can refer  to what you want to find  section uses the grouping symbol in the pattern, and in " replaced content " You can use the back reference to refer     to all the text that the find pattern matches, if you use &  Symbol modifier: I: Ignore case g: global substitution, meaning that a row is replaced if it matches multiple times

Sometimes we need to match "\", we can replace the delimiter with other characters, such as [email protected]@@ 或者 s###.

Vim's multi-file function

Vim can open multiple files at the same time

Vim  FILE1  FILE2.

To switch between files:

: Next  Next file: Prev  previous  file: First file:   last file

Exit All Files:

: Wqall  Save exit All files:wall  Save All Files: Qall exit all Files

Multi-Window viewing

Use the options when opening multiple files with vim:

-o: Horizontal divider window -o: Vertical divider window

Using CTRL+W to switch windows

Customizing the working characteristics of vim

Settings in the last line mode, only valid for current VIM, permanent modification must modify the configuration file:

Global:/etc/VIMRC User personal:~/.vimrv  No can create

Operations in the last-line mode:

1 , line numbers    display: Set number, abbreviated set NU    Suppress: Set Nomber, set Nonu2, brace match highlight    match: Set Showmatch, set SM    Cancel: Set NOSM3, auto indent    enabled: Set AI    disabled: Set Noai4, highlight search    enabled: Set  hlsearch    Disabled: Set Nohlsearch5, Syntax    highlighting enabled: syntaxon6  , ignoring character case    enabled: Set IC    Disabled: Set Noic

Example: In Vim, the TAB key is the position of 8 spaces and 8 spaces is a whole, if the left and right key movement will see only one move is 8 spaces, then how to modify it to 4 spaces?

You can add the following 2 lines to the/ETC/VIMRC:

Set ts=4     #设置宽度为4个空格set expandtab  #将tab转换为了space

Get vim help in last-line mode

: Help  : Help subject

If you want to practice vim, enter Vimtutor to get the VIM tutorial

[Email protected] ~]# Vimtutor

"Linux acquaintance" vim 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.