Common uses of VIM

Source: Internet
Author: User
Tags diff

Vi/vim is the editor that is provided by default for all Unix/linux operating systems. Due to its powerful functions and efficient operation, Vi/vim has become one of the many editing tools that unix/linux users and administrators must master and use skillfully.

I am in the process of learning and using Linux, the basic is to use VIM to write programs, use more and more familiar with the more, also find the power of vim, below I make some notes, the most commonly used vim command to remember.

1.vim start-up and switchover

Vim <filename> can open the file you need to view, when the file does not exist, the system will automatically create a new file;

: Open File,or:e <filename> opens a new file in the Vim window;

: Q Exit vim,: wq:q! Shitf+zz;

When you need to temporarily switch to the shell environment from the VIM editing interface, you can use the following 3 ways, which is very convenient.

    • :! + command to execute shell commands under VIM, such as:!ls-al;
    • Using the combination of ctrl-z and FG, ctrl-z the VIM process into the background, and after executing the shell command, FG takes vim back to the foreground execution;
    • Enter in normal mode of Vi/vim: SH to enter the Linux/unix shell environment. Enter the exit command when you want to return to the Vi/vim editing environment.

Operating unit of 2.vim

When using Vim, it is necessary to understand the operating unit of the VIM command.

There are many operating units in Vi/vim, mainly including:

    • Characters (H, L): in characters, H before a character, l after one character;
    • Words (W, W, B, B, e): Lowercase is split with non-alphabetic characters, uppercase is separated by non-whitespace characters (spaces, Tab), W next word, b previous word, e cursor at the end of the word;
    • Line (j, K, 0, ^, $,: N): J next line, K on line, ^ 0 beginning, $ line tail,: N jumps to n rows;
    • Sentence ((,)): VI with punctuation? 、。! One of the following, when the punctuation is at least 2 spaces, or as the last non-whitespace character of a line appears;
    • Paragraph ({,}): Paragraph separated by empty behavior;
    • Screen (h, M, L): H moves to the first line of the screen, M moves to the middle line of the screen L moves to the last line of the screen;
    • Pages (Ctrl-f, Ctrl-b, Ctrl-u, ctrl-d): Ctrl-f Scrolls forward one page, Ctrl-b scrolls back one page, Ctrl-u Scrolls forward half page, ctrl-d scrolls back half page
    • File (g, GG,: 0,: $):g moves to the end of the file,GG moves to the beginning of the file;
    • Special:% (jump to the matching brackets), '. (Jump to the recently modified location and position the edit point), '. (Jumps to the recently modified position without positioning the edit point).

The operating range of the vim movement = number of operations * operating units. For example: The 10l command moves 10 characters to the right, and the 8w command shifts 8 words right.

Editing commands for 3.vim

Insert command:

    • I insert before the current cursor
    • A append inserted after the current cursor
    • I caps are inserted at the beginning of the line
    • A caps insertion At the end of A row
    • o Insert the line below the current cursor row
    • I insert at the top of the line where the current cursor is located

To modify a command:

    • X Delete current character, 3x delete current cursor start backward three characters;
    • DD deletes the current line, 10d deletes the 10 rows starting at the current line: one, $d delete 11 rows and all subsequent rows;
    • YY copies the current line, 2yy copies the current line and its next row;
    • P is pasted after the current cursor;
    • U, Ctrl-r, Undo and forward;
    • ~ Cursor Letter Case reversal, GUW cursor word lowercase, GUW cursor Word to uppercase, GUU cursor line all characters lowercase, guu cursor line all characters into uppercase, g~~ cursor line all character case reversal;
    • . Click to repeat the previous command, the dot number in other parts of Linux also has a lot of usage (dot number of five ways);
    • >> Right Indent,<< left indent, with the use of operands, in the editing of source code is very useful;

Multi-line Comments:

    • 1. Enter command-line mode, press CTRL + V to enter Visual block mode, then press J, or K to select multiple lines to mark the lines that need comment
    • 2. Press the capital letter I, then insert the comment, for example//
    • 3. Press the ESC key to annotate all

To cancel multiple lines of comment:

    • 1. Enter command line mode, press CTRL + V to enter Visual block mode, select the number of columns by letter L, for example//2 columns to be selected
    • 2. Select the comment symbol by the letter J, or K
    • 3. Press the D key to cancel all comments

4.vim Search and replace

    • /string searches forward for the specified string;
    • ? string starts at the cursor and looks up the character from string;
    • Because regular expressions are supported, there are some special characters in vim that need to be escaped when searching. *[]^%/?~$;
    • n Search next;
    • N reverse Search next;
    • * Search forward in the file for the word currently in the cursor;
    • # Search backwards in the file for the word currently in the cursor;
    • : M,ns/str1/str2/g the string str1 from M line to N row is replaced by string str2;

5.vim Environment settings

    • : Set view VIM environment variables
    • : Set number Displays line numbers
    • : Set no number to suppress line numbers display
    • : Set autoindent setting Auto Indent
    • : Set no autoindent cancel auto indent setting
    • : Set shiftwidth=4 Sets the indent width to 4
    • : Set ignorecase settings Ignore size
    • : Set no ignorecase de-ignore size setting
    • : Set list shows invisible characters
    • : Set nolist suppress invisible character settings
    • : Set Hlsearch Highlight search results, all results are highlighted instead of just one match
    • : Set Nohlsearch Close Highlight search display
    • : Set Wrapscan re-search, search to file header or tail, return to continue search, default on

Sometimes when Vim opens a file, the indentation of the code is found to be misplaced, which is most likely due to the difference in the number of spaces that the TAB key sets in different environments.

For a saved file, you can use the following method to replace the Space and tab:
tab is replaced with a space:
: Set ts=4
: Set Expandtab
:%retab!

Space is replaced with tab:
: Set ts=4
: Set Noexpandtab
:%retab!

6.vim Multi-screen operation

Vim's multi-screen operation is ideal for viewing large projects

    • : New horizontal Cut window, open blank window by default
    • : Split horizontal Cut window (or direct input: SP), opens the current file by default
    • : Vsplit Vertical Cutting (can also: VSP)
    • CTRL+WW move to the next window
    • ctrl+w+ (HJKL) moves the window in different directions;
    • Vim-o a.txt b.txt c.txt open a horizontal split window for each file
    • Vim-o a.txt b.txt c.txt open a vertical split window for each file

vimdiff Command: Compare the differences between two files, code merge is very convenient

    • vim-d file1 file2 or Vimdiff file1 file2, using Vim's comparison mode to open two files;
    • :d iffthis opened two files using split mode file1,file2, comparing two files , Enter the command in two separate Windows ;
    • [C jumps to the previous point of difference
    • ]c jump to a different point
    • DP applies the current document content of the difference point to another document (diff put)
    • Do copy the contents of another document of the difference point to the current document (diff get)
    • : Set Diffopt=context:3, vimdiff default is the difference between the 6 lines of text will be displayed for reference. Other lines of the same text are automatically collapsed
    • ZF creates a collapse (using numbers to create a fold that has the current line to the next number of rows, such as 3j to create a fold that includes 4 rows)
    • Zo Open fold (L can also open folding)
    • ZC Close Current collapse
    • ZM Close all folds
    • ZR Open All folds
    • ZE Delete all collapsed
    • ZD Delete the current collapse
    • za If the current turn off, if the current shutdown is open (this is very convenient)
    • ZJ to the beginning of the next fold
    • ZK to the end of the previous fold

First summed up so much, there are like function jumps and other commands, and so on, to use the time to organize it, to see once may only remember a small meeting, only the actual application will form the subconscious!

Recommended http://www.ibm.com/developerworks/cn/linux/l-cn-tip-vim/

This article is I have seen the introduction of VIM basic commands the most organized article, this article also borrowed a lot of the content, express thanks!

Finally, insert a vim command deity diagram:

Common uses of VIM

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.