Vim Common Operation Summary

Source: Internet
Author: User

Vim is a text editing tool, so you don't have to remember everything when you're learning, as long as you work more efficiently with vim. This article summarizes the operation method of vim from the practical point of view, hoping that after reading this article, the reader can reach the general text editing speed.

VI is the standard Linux editor, then Vim is a well-deserved VI-enhanced version, not only compatible with all the VI commands and operations, but also added a lot of new features suitable for writing code, such as Vim will be based on the file extension and the beginning of the file to determine syntax highlighting. In some distribution VI is actually just a vim alias, such as CentOS will find: VI is aliased to ' vim ', so the use of the VI command is actually the use of vim.

First, the basic function

Use vim [filename] to enter VIM, create a file if the file does not exist, or specify a file name on save if you do not specify a filename.

1. General mode

Vim is in general mode when nothing is done after entering vim. In this mode you can do most of the operation, including delete, copy, paste and so on.

The N in the following table represents a number instead of a keystroke n;command refers to a command; The filename refers to the name of the file (exists or does not exist).

=============================== delete ================================ x equal to Del x BACKSPACE NX backwards to remove the N-character DD Delete current row ndd down delete n rows
D1G Delete the first line to the current line DG deletes the current line to the last row =============================== copy =================================
YY Copy the current line NYY copy n rows
Y1G Copy the first line to the current line YG copy the current line to the last row =============================== paste =================================
P paste below the current line p paste below the current line
============================= selected text ===============================
V by character Select V by row check [Ctrl]+v Select by rectangular area

=========================== Quick Move Cursor =============================
N+<space> the cursor to move n characters
n+<enter> the cursor moves n rows
NG moves to nth row
GG move to file start
============================= Undo & Redo ===============================
U undo
. Repeat the previous command to modify the text content
[Ctrl]+r Redo
============================== other ===================================
ZZ exits and saves (no changes are actually saved)
2. Insert mode

Pressing I,O,A in normal mode will enter insert mode (I and o,o are the only ones I use to insert new rows), and "--insert--" is displayed in the lower left corner of the screen. Press ESC in insert mode to return to normal mode. In this mode, you can enter text, all keys (except ESC) hold the key for input, such as how many times you will not undo anything but input U.

3. Command mode

In general mode, type a colon, a slash, a question mark of three, and enter command mode, which automatically returns to normal mode after the command executes (type the command and press ENTER to execute the command). In this mode, you can perform more complex operations such as text search, substitution, saving/Save As, importing, vim setting, etc.

 ============================ Search & Replace ===============================/pattern/     When search is complete, mates N and N can toggle between multiple text ? pattern? When the reverse search is complete, mates N and N can toggle between multiple text 
: S/pattern/string/g matches the pattern in the bank and replaces string
: N1,n2 s/pattren/string/g N1 to N2 Match pattern in line and replace with string
: n1,n2 S/PATTERN/STRING/GC matches pattern in N1 to N2 line and replaces it with string, asking for each substitution.
: 1,$ s/pattern/string/g Replace the first line to the last
:% s/pattern/string/g Replace in full-text scope

========================== Save/Save As/import ===========================
: W Save: w! Force save
: Q exit: q! force quit, do not save
: R filename Import file
: W F Ilename Save As
: n1,n2 w filename save N1 to N2 as file filename
: n1,n2 w! filename Saves the contents of N1 to N2 as if the specified file already exists overwrite
: Wq Save and exit
: N1,N2 CO N3: Copy the contents of the N1 line to the N2 row below the N3 line
: n1,n2 m N3: Move the contents of the N1 line to the N2 row to the N3 line
: N1,n2 D Deletes the contents of the N1 row to the N2 row
=============================== other =================================
: N Go to Nth row
:! Command turns to execute command, pointing to the completion of the return VIM
: Set nu Displays line number
: Set Nonu hidden line number

Note: 1. Tested found :! command commands cannot be command aliases, only bash built-in commands or executable file names.

2. String substitution command : n1,n2 s/pattren/ The end of the string/g in G means not only replacing the first place, but replacing it all.

Second, split-screen operation and labeling 1. tab page

Vim has a browser-like tab that makes it easy to edit multiple documents at the same time. You can also separate multiple Windows under one tab (one tab can contain multiple split screens), and each window edits a different document. The advantage of Multi-window (split-screen) is that it is convenient to control data and so on, in contrast, personal feeling label page used more.

You can use: tabnew to open a new tab and then use R to import a file in a new tab, or you can use : tabnew filename to open the specified file directly on a new tab page.

In general mode, G+T can switch between different tabs (tested to press T after G and vice versa). : tabm n can move the current tab to the specified label position, starting with n 0.

2. Split screen

vim-on filename1 filename2 Open multiple files in a split-screen way. vim-on filename1 filename2 Open multiple files in the left and right split-screen way. If you have already entered Vim, you can use : SP to open a new window (up and down), or use : VSP (left and right split screen). The SP can specify the file, such as SP filename , to open the specified file in a new window, or the newly opened window will display the same file as the old window, and the two are modified synchronously (quite fork meaning).

When you press Ctrl, double-click W to toggle between different windows, or you can switch windows by using the [ctrl]+w+ arrow key].

Three, more knowledge.

The ~/.viminfo file (auto-generated) records information about the user's operation history, so when you open the document again, you'll see that the cursor is still at the location where you left it last, and so on.

/ETC/.VIMRC is to save the configuration information of vim, such as whether the default will be syntax highlighting, line number and so on. It is not recommended to modify the configuration here, you can create a new. VIMRC in your home directory to modify the configuration.

The knowledge about Linux display Chinese is still being collated and will be shared with you later.

Reprint please specify the copy and attached to the original link address.

Vim Common Operation Summary

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.