How to use the VIM editor

Source: Internet
Author: User

Vim Editor

Text editor, word processor

Nano, SED

Vi:visual Interface (Visual Interface)

Vim:vi improved

Mode editor, full screen editor


One: Open file

# Vim/path/to/somefile

    • Vim +#/path/to/somefile Open/path/to/somefile and locate the file # line

      • Example 1: Open file/etc/inittab, cursor in line 18

        # Vim +18/etc/inittab

    • Vim +/path/to/somefile opens/path/to/somefile and is located at the bottom of the file

      • Example 1: Open file/etc/inittab, cursor in last line

        # Vim +/etc/inittab

    • Vim +/pattern: Open the file and navigate to the beginning of the line that was first matched to the PATTERN

      • such as: Vim +/:initdefault/etc/inittab

Edit mode/Command mode: Copy, paste, delete line ...

Input mode: input content to file

Last-line mode: File management commands

Opens the Vim editor, which is in edit mode by default.

Patterns can be converted from one to the other.

Edit-Input

    • I: In front of the character of the current cursor, converted to input mode;

    • A: After the character of the current cursor, convert to input mode;

    • O: Under the current cursor line, create a new row, convert to input mode;

Capital

    • I: Convert to input mode at the beginning of the line where the cursor is currently located;

    • A: At the end of the line where the current cursor is located, convert to input mode;

    • O: On the line of the current cursor, create a new row, convert to input mode;

input - Edit

    • Esc

Edit --and last-line mode (only accessible from edit mode)

    • : You can enter a colon

Commands that can be executed by the last-line mode:

    • 10d: Delete line tenth in the file

    • 10,20D: Delete lines tenth through 20th in the file;

    • Set Nu: Display line number

    • ! ls/etc/: Execute shell command without affecting the current file edit

Last-line mode-- edit

    • Esc,esc

Input mode to the last line mode cannot be converted directly;


Two: Close the file

    • last-line mode close file

      • : Q: Quit, do not save, if you have edited the contents of the file, so that cannot exit, will prompt you the file has changed, let you confirm whether to save

      • : Wq: Save and exit

      • :q! : Do not save exit

      • : w: Save, do not exit

      • :w! : Forcibly save read-only files (read-only files are not valid for administrator root, administrators can use w! to forcibly save files)

      • : Wq--: X

    • Exit the file in Edit/Command mode

      • ZZ: Save exit (You can save exit without switching mode)


three: Move cursor (Edit/ Command mode)

    • Character-by-word movement: You can use up or down keys, or you can:

      • H: Left

      • L: Right

      • J: Next

      • K: on

      • Number plus the move key above, you can move n characters or lines in the corresponding direction

    • Move the word "

      • W: Move backwards to the beginning of the next word

      • li>

        E: Skips backward to the ending of the current or next word

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

      • The above commands all support adding numbers, such as: 3w,4b, can be implemented to move N words

    • In-line jump/move

      • 0: Beginning, Absolute Start of

      • ^: The first non-whitespace character at the beginning of the line, relative to the beginning of the header

      • $: End of row, absolute line end

    • Inline jump

      • G: Skip to last line

      • #G: Jump down to Line # header

Last line mode jump: directly to the travel number, enter can


Four, turn screen operation

    • Ctrl+f: Turn down one screen and flip one screen to the end of the file

    • Ctrl+b: Turn up one screen and turn one screen to the file header

    • Ctrl+d: Flip Down half screen

    • Ctrl+u: Flip up half screen


V. Delete a single character

    • X: Delete a single character at the cursor location

    • #x: Remove the total # characters from the cursor and backwards

Vi. Delete command D

    • The D command can be used together with the cursor jump command to achieve a different deletion effect. Such as:

      • d$: Delete the content at the end of the line at the current cursor

      • D0: Delete the current cursor at the beginning of the content

      • DW: Word deletion

      • 3DW: Delete Three words at a time

      • #de:

      • #db:

      • DD: Deletes the current cursor in the row

      • #dd: Delete the line down from the line where the cursor is located

      • Delete line 10th through 20th (last mode: 1,8d)

    • Delete the specified row in the last row mode

      • :., +5d: Delete 5 rows from the current line to the bottom in the last row mode, altogether 6 rows are deleted

      • (. Represents the current row, $ represents the last line, + #表示想下的 # lines);

      • Current line to the penultimate line:., $-3

      • .: Indicates when the forward

      • $: Indicates the last line

      • +#: Represents the Down # line


Seven, paste command: P

    • The deleted content is not purged immediately, but is saved in the memory buffer.

    • The last deletion of the content, can be pasted into any specified place, can be pasted multiple times;

    • P:

      • If you delete or copy the entire line, paste it to the place where the cursor is located;

      • If the copied or deleted content is a non-whole line, paste it to the back of the character where the cursor is located;

    • P:

      • If the whole line is deleted or copied, paste it to the top of the cursor;

      • If the copied or deleted content is a non-whole line, paste it in front of the character where the cursor is located;

Viii. Copy command: Y

    • Usage with d command

    • YY: Copy one line

    • 3yy, y0, y$ ...


Nine, first delete the content, and then into the input mode (i.e.: Replace or modify)

    • C: Usage with d command


X. REPLACEMENT command

    • R: Replace single character

    • R: Enter the replacement mode, similar to the Insert key. ESC Exits replacement mode


Xi. Revocation of edits

    • U:undo, undo the previous edit operation, you can use continuous u, undo the first n edits

    • Note: VI or VIM only saves the last 50 edits in the memory buffer, which means that we can only undo the last 50 edits at the most recent time.

    • #u: Undo the edit operation of the most recent # times directly.

Undo undo: Undo/Redo Last undo operation: Ctrl+r


12. Repeat the previous edit operation

    • .: Points, powerful points


13. Visualization Mode

    • V: Select by character, select to perform all of the above editing operations (d delete, y copy, etc.)

    • V: Select by Rectangle block, select by row


14. Find

    • /pattern: From file header to tail lookup

    • ? PATTERN: From the current cursor line, to the file header, looking backwards

      • N: Next, jump down next

      • N: Previous, jump up next


XV, find and replace

Use the S command in the last-line mode, using the same usage as sed.

    • Syntax: ADDR1,ADDR2S@ PATTERN@STRING@gi

      • @: Delimiter

      • S: Find

      • G: Replace

      • I: Ignore character case

For example: Move the document from the current line (.) all lowercase He that starts at the bottom of the document is replaced with the uppercase he

:., $-1s/he/he/g

Full-text replacement:

: 1,$ or:%

1,$, which means that from the first line to the last line, the percent semicolon also refers to the full text.

Exercises:

Replace ftp://instructor.example.com/pub in the/etc/yum.repos.d/server.repo file with Http://172.16.0.1/yum

Key

%s/ ftp: \ /\ /instructor\ . Com\ /pub/ http : \ /\ /172.16.0.1/ GI

%s@ftp://instructor\.example\.com\/pub@/http172.16.0.1/yum @gi

16. How to open multiple files

Vim FILE1 FILE2 FILE3

: Next: Switch to the next file

:p Rev: Switch to previous file

: Last: Switch to final file

: First: Switch to file one

Vim rc.local Init

The first file is displayed by default;

Switch to next file: Next

Switch to last file: prev (note: Before you switch to the previous file, you must save the edited file, otherwise the switch does not pass)

Exit: Exit All

: QA

: Qall

17, split screen display a file

Look at the previous content to edit the following

Ctrl+w,s: Horizontal splitter window

Ctrl+w,v: Vertical splitter window

Toggle window: Ctrl+w,arrow (Upper and lower left and right arrows)

Exit:

: Q Exits a window

: QA all exits, close all windows

--------------------------------------------------------

Horizontal splitter window:

Ctrl+w, loosen, then press S

ctrl+w+ down/On the arrow, toggle the cursor to the top/bottom of the window to edit.

Vertical splitter window:

Ctrl+w, release, then press V

18, sub-window display multiple files

Window 1 shows file 1, window 2 shows file 2

horizontal split display, for example: Vim- o FILE1 FILE2

Vertical Split display, for Example:vim- o FILE1 FILE2

19. Save part of the current document as another file

Use the W command in the last row mode

: W

: W/path/to/somewhere

: Addr1,addr2w/path/to/somewhere

20. Fill in the current file with another file content (merge two files)

: R/path/to/somefile

21. Interacting with the shell

Enter in the last line mode:

:! COMMAND

#COMMAND不支持alias别名命令

22. Advanced Topics

1. Display or suppress line numbers

: Set number--Set Nu

: Set Nonu

2. Ignoring or distinguishing character casing

: Set IC

: Set ignorecase

: Set Noic

: Set Noignorecase

3. Set auto Indent (useful when writing scripts)

: Set Autoindent

: Set AI

: Set Noai

4. Set highlighting (found text highlighting)

: Set Hlsearch

: Set Nohlsearch

5. Syntax highlighting

: Syntax on

: Syntax off

23. Vim Configuration file

Global:VIM/ETC/VIMRC

Local (User level):vim ~/.VIMRC


About Vim's exercises:

The system comes with the vimtutor, do it once a day, each control in half an hour to fix, became.


This article is from the "Cold Tea" blog, please be sure to keep this source http://miaocbin.blog.51cto.com/689091/1665883

How to use the 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.