Share some very useful Vim commands

Source: Internet
Author: User

Share some very useful Vim commands

Specifically, I am a fan of Vim. So before some of you throw stones at me, I will first show you a series of "Little-known Vim commands ". I mean, some commands that you may not have encountered before may be useful to you. As the second disclaimer, I do not know which commands you may know and which are useful to you. Therefore, these commands are actually relatively rare, but very useful Vim commands.

-------------------------------------- Split line --------------------------------------

Build VIM into a simple and practical IDE

Vim Learning Guide

Quick learn Vi Editor

Powerful Vim Editor

Build a Vim Development Environment on CentOS 6.2

Install the highlighted Vim editing tool in CentOS 5.4

Vim tips: C language settings

Set the Vim row number in Ubuntu

Vim editor basic tutorial

-------------------------------------- Split line --------------------------------------

Save the file and exit

I learned this command recently.

  1. : X

It is equivalent to the following command:

  1. : Wq

Both Save the current file and exit.

Note: These two commands are not exactly equivalent. They are the same when the file is modified. However, if it is not modified, use: x to change the modification time of the file, and use: wq to change the modification time of the file .)

Basic Calculator

In insert mode, you can press Ctrl + r and enter =, and then enter a simple formula. Press Enter to insert the calculation result to the file. For example, try to enter:

  1. Ctrl + R' = 2 + 2' ENTER

The calculation result "4" is inserted into the file.

Find duplicate continuous words

When you type a word quickly, it is very likely that you input the same word twice consecutively, just like this. This kind of mistake may have cheated anyone, even if you read it again, it is inevitable. Fortunately, a simple regular expression can be used to prevent this error. Use the SEARCH Command (default time/) and then enter:

  1. \ (\ <\ W \ + \> \) \ _ s * \ 1

This displays all duplicate words. To achieve the best results, do not forget to run the following command:

  1. Set hlsearch

Put it in your. vimrc file to highlight all the matches.

Abbreviations

One of the most impressive tips is that you can define abbreviations in Vim, which can replace what you input with something else in real time. The syntax format is as follows:

  1. : AB [abbreviation] [Text to be replaced]

A general example is:

  1. : AB asap as soon as possible

The "asap" you entered will be replaced with "as soon as possible ".

Save the file when you forget to open the file as root

This may be a popular command in the Forum. Every time you open a file that you do not have the write permission (such as the System Configuration File) and make some modifications, Vim cannot be saved using the common ": w" command.

You do not need to re-open the file as root and then modify it. You only need to run:

  1. : W! Sudo tee %

This will be saved as root.

Real-time text Encryption

If you don't want others to understand the content on your screen, you can use a built-in option to encode the text using the following command:

  1. GgVGg?

Gg moves the cursor to the first line of the Vim buffer, V enters the visible mode, and G moves the cursor to the last line of the buffer. Therefore, ggVG overwrites the Current Buffer in visual mode. Last g? Use ROT13 to encode the entire region.

Note that it can be mapped to the most commonly used key. It also works well with letter symbols. The best way to undo it is to use the undo command: u.

Auto-completion

This is another feature that makes me feel ashamed, but I find that many people around me do not know it. Vim has the auto-completion function by default. Indeed, this function is very basic and can be enhanced through plug-ins, but it is also very helpful. The method is simple. Vim tries to predict the end of a word by entering a word. For example, if you enter "compiler" for the second time in the same file, you only enter "com" and keep it in the insert mode, press Ctrl + n to see that Vim has completed the word for you. Very simple, but also useful.

Compare the differences between the two files

Most of you probably know the vimdiff command, which can open Vim in the separation mode and compare the differences between the two files. Syntax:

  1. $ Vimdiff [file 1] [file 2]

But the same result can also be obtained through the following Vim command:

  1. : Diffthis

First open the original file in Vim. Then use the separation mode to bring the Second file:

  1. : Vsp [file 2]

Finally, enter the following in the first buffer:

  1. : Diffthis

Use Ctrl + w to switch the buffer and input it again:

  1. : Diffthis

In this way, different parts of the two files will be highlighted.

(Note: You can directly use the command windo diffthis in a buffer zone without entering: diffthis twice)

To stop the comparison, use:

  1. : Diffoff
Roll back files by Time

Vim records file changes, and you can easily roll back to a previous time. This command is quite intuitive. For example:

  1. : Earlier 1 m

The file will be rolled back to the Status 1 minute ago.

Note: You can use the following command to perform reverse conversion:

  1. : Later

For more details, please continue to read the highlights on the next page:

  • 1
  • 2
  • Next Page

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.