Some Vim usage methods, such as setting row numbers and enabling automatic indentation

Source: Internet
Author: User
Tags imap alphanumeric characters
The following are some of my experiences:

Let's take a look at vim. VI is a widely used text editor in Unix/Linux, which is available on most machines. VI has various variants, and different variant software is often used on different machines. among them, VIM is relatively easy to use and widely used. vim is short for VI improved, indicating better VI. I personally think it is a very good editor (in order to avoid the eye-catching of Emacs fans, it is not the best ). we recommend that you try it if you have never used it. Of course, VIM is easy to use for compiling text files, such as programming and HTML documents, but it cannot be used to compile Word documents.

About the installation of VIM, the basic use of methods and other network can find a lot, it is not here, if you are interested in Vim, then look here (Chinese documentation): http://vcd.cosoft.org.cn/pwiki/index.php

This article describes some of the useful and commonly used commands. If you are familiar with these commands, you will find it quite comfortable to edit files.

Note:
In the following example, xxx indicates entering XXX in command mode and press Enter.
In the following example: XXX indicates entering XXX in extended mode and press ENTER
The commands in parentheses indicate related commands.
The command entered in edit mode or visual mode will be noted separately.

Entering and leaving the feclinux Alliance

    To enter vim, you can directly enter Vim <File Name> at the system prompt. Vim can automatically help you load the file to be edited or open a new file. After entering vim, A Tilde sign will appear on the left of the screen. If this symbol is at the beginning of a line, this column is currently empty. To exit vim, you can enter: Q and: WQ in command mode to archive and then exit (note the colon ). To switch to the command mode, use the [ESC] key. If you do not know what mode is used, you can press [ESC] several times. the system will beep to confirm entering the command mode.

Vim input mode feclinux Alliance
How do I enter materials? Several commands can enter the input mode: feclinux Alliance
New (append) feclinux Alliance

    A adds data from the end of the cursor position, and the data after the cursor moves backward with the new data. Feclinux Alliance
    A adds data from the end of the column where the cursor is located.

Feclinux Alliance
Insert)

    I start to insert data from the front of the cursor, and the data after the cursor moves backward with the new data. Feclinux Alliance
    I. Data is inserted before the first non-blank character in the column where the cursor is located.

Feclinux Alliance
Start (open)

    O adds a column under the column where the cursor is located and enters the input mode. Feclinux Alliance
    O adds a new column to the column where the cursor is located and enters the input mode.

The text description may seem complicated, but you can understand these operations as soon as you perform the actual operations. Practice is very important, especially for computer applications. You can try and verify the results at any time. We strongly recommend that you use it instead of simply using K files to get twice the result with half the effort. (Note: This section is nonsense .)

Archive instruction feclinux Alliance
Feclinux Alliance
Archive commands start with:, which is a little different from editing commands. For example, the instruction to end editing is Q. The end of this story: feclinux Alliance

    : Q end editing (quit) feclinux Alliance
    If you do not want to archive an archive and want to discard the edited archive, Use Q! Force exit. Feclinux Alliance
    : W archive (write) feclinux Alliance
    The file name to be archived can be added later. Feclinux Alliance
    Archive commands can be combined. For example, WQ means archiving and leaving. Feclinux Alliance
    ZZ functions are the same as: WQ. Feclinux Alliance

It is also worth mentioning part of the vim archiving function. You can use: N, MW filename to store the text of row n to row m in the specified filename.

1. Search

/XXX (? XXX) indicates searching for strings matching XXX in the entire document,/indicates searching down ,? Indicates
Search up. xxx can be a regular expression, so I won't say much about the regular expression.
Generally, it is case sensitive. To be case insensitive, you must enter
: Set ignorecase
After finding it, enter n to find the next match and enter n to search for it.

* (#) When the cursor stays on a word, enter this command to search for matching words.
Next (top) a word. Similarly, enter n to find the next match and enter n inverse.
To search.

G * (G #) this command is similar to the previous command, except that it does not fully match the word at the cursor.
Is to match all strings containing the word.

GD this command looks for words that match the word where the cursor is located, and points the cursor to the non-
The place where the word appears for the first time in the Comment Segment.

% This command looks for the parentheses matching the cursor location, including () [] {}

F (f) X: This command searches for the first X character on the right (left) side of the cursor in the row where the cursor is located.
After finding:
Input; indicates to continue searching
Input, indicating Reverse Lookup

2. move the cursor quickly
In VI, moving the cursor and editing are two things. Because they are distinguished, you can easily perform optical calibration.
It is useful to move the cursor a little faster.

W (e) move the cursor to the next word.
B. move the cursor to the previous word.

0 move the cursor to the beginning of the line.
^ Move the cursor to the starting character of the line.
$ Move the cursor to the end of the row.

H move the cursor to the first line of the screen.
M moves the cursor to the middle line of the screen.
L move the cursor to the end of the screen.
Gg move the cursor to the first line of the document.
G move the cursor to the end of the document.
C-F (press Ctrl and F together) this command is page down.
C-B (that is, press Ctrl and B together, and then the same) this command is page up.

''This command is quite useful. It moves the cursor to the previous tag, for example, using Gd, *, and so on.
After finding a word, enter this command to return to the position where it was last stayed.

'. This Command makes it very easy to move the cursor to the last modified line.

'. This command is quite powerful. It moves the cursor to the last modification point.

3. Copy, delete, and paste
In VI, y indicates copy, d Indicates delete, and P indicates paste. Copy and delete commands are used to move the cursor.
In combination, you can see several examples.

YW indicates copying the content from the current cursor to the end of the word where the cursor is located.
DW indicates deleting the content from the current cursor to the end of the word where the cursor is located.
Y0 indicates copying the content from the current cursor to the beginning of the row where the cursor is located.
D0 indicates deleting the content from the current cursor to the beginning of the row where the cursor is located.
Y $ indicates copying the content from the current cursor to the end of the row where the cursor is located.
D $ deletes the content from the current cursor to the end of the row where the cursor is located.
Yfa indicates copying the content from the current cursor to the first a character after the cursor.
DFA indicates to delete the content from the current cursor to the first a character after the cursor.

Specifically:
YY indicates copying the row where the cursor is located.
Dd indicates deleting the row where the cursor is located.
D: Delete the content from the current cursor to the end of the row where the cursor is located.

The complex usage of copying, deleting, and pasting is related to registers, which can be queried by yourself.

4. Numbers and commands
In VI, the combination of numbers and commands often indicates repeated execution of this command. If it appears at the beginning of the extended mode, it indicates the line
Positioning. For example:

5fx indicates 5th x characters after the cursor is searched.

5 W (e) move the cursor to the next five words.

5yy indicates copying 5 rows below the cursor.
5dd indicates deleting 5 rows below the cursor.

Y2fa indicates copying the content from the current cursor to the second a character after the cursor.

: 12, 24y indicates copying content between 12th rows and 24th rows.
: 12, y indicates copying the content between 12th rows and the row where the cursor is located.
:, 24y indicates copying the content between the row where the cursor is located and the row 24th. Deleting is similar.

5. Enter characters quickly
In Vi, you do not need to enter every character. There are many ways to quickly enter some characters.
If you are using Linux/Unix, you must have an experience. when you enter a command on the command line, enter the first few characters and then press
The tab system will automatically complete the remaining characters. If there are multiple matches, it will be printed. This is the famous command.
(In fact, file name completion is also available in Windows). VI has many character string completion commands, which are very convenient.

C-P (c-N) in editing mode, enter a few characters and then enter this command, then VI starts searching up (down)
Enter the matching words at the beginning of the cable and fill them in again. Enter this command repeatedly to search for the matching words. This command
All files opened in this Vim program will be matched.

C-X-L in editing mode, this command is used to quickly complete the contents of the line, but only
Match in the document.

In C-X-F editing mode, this command indicates filling up the file name. For example, enter:
/Usr/local/Tom:
/Usr/local/tomcat/

Abbr is short for. This is a macro operation, which can be replaced by an abbreviation in editing mode.
For example, if system. Out. println is often input to write a Java file
It is troublesome, so we should use abbreviations to reduce the number of words. You can do this:
: Abbr SPRT system. Out. println
After entering SPRT and other non-alphanumeric characters, it will be automatically extended to system.
Out. println

6. Replace
Replacement is the strength of VI, because regular expressions can be used to match strings. Below are several examples.

: S/AA/BB/g replace AA in all strings containing aa that appear in the row where the cursor is located with bb
: S // <AA/>/BB/g replace all AA in the row where the cursor is located with BB. Replace only the word AA.
: % S/AA/BB/g replace AA in all strings containing AA in this document with bb
: S/AA/BB/g replace AA in all strings containing aa that appear from row 12 to row 23 with bb
: 12, 23 S/^/#/Add the first line from 12 to 23 characters #
: % S = * $ = Delete unnecessary spaces at the end of all rows
: G/^/S * $/d delete all blank lines that do not contain characters (spaces are not included.

7. Multi-file editing
Opening and editing many files in a Vim program is quite convenient.

: SP (: VSP) file name Vim Splits a horizontal (vertical) window and opens a new file in the window.
Starting from vim6.0, the file name can be the name of a directory. In this way, VIM will
Open the Directory and display the file list. Press enter on the file name to press
Open the file. If you enter o, open the file in a new window. Enter? You can see
To help information.

: E file name Vim will open a new file in the original window. If the old file has been edited, it will be required to save.

How does C-w Vim split several windows? Enter this command to position the cursor cyclically
To each window.

: Ls this command shows how many files have been opened in the vim program, at the bottom of the screen
The following data is displayed:
1% a "usevim.html" Row 162
2 # "xxxxxx.html" line 0

Where:
1 indicates the number of opened files, which is very useful.
% A indicates the file code, and % indicates the file being edited,
# Indicates the Last edited File
"Usevim.html" indicates the file name.
Row 162 indicates the cursor position.

: B serial number (CODE) This command opens the file with the specified serial number (CODE) in this window, where the serial number (CODE)
The LS command is used.

: Set diff this command is used to compare two files, you can use
: VSP filename
Command to open another file, and then enter this command in each file window, you can see
The effect is reached.

8. Macro replacement
Vi can not only replace text with abbr, but also define macro commands. Some commands are difficult to input,
Therefore, I have defined them on <F1>-<F12>, which makes it easy to write these configurations in advance ~ /. Vimrc
($ Vim/_ vimrc in Windows), you do not need to write the first colon.

: NMAP <F2>: nohls <CR> cancels the highlighted string
: NMAP <F9> <C-W> W command mode move cursor to different windows
: Imap <F9> <ESC> <F9> run in input mode <F9>
: NMAP <F12 >:% s =* =< Cr> deletes unnecessary spaces at the end of all rows.
: Imap <F12> <ESC> <F12> same as above

: Java: (Note: Why is Java mentioned here, because the following definition does not work for other file formats?
Will talk about how to implement this)
: NMAP <F3>: comp javac <CR>: Mak-D. % <CR>
This command uses javac to compile a Java file. It automatically positions the cursor to an error point.
In $ Vim/compiler, A javac. Vim file contains only two lines:
Setlocal makeprg = javac
Setlocal errorformat = % A % F: % L:/% m, %-Z % P ^, %-C %. % #

: NMAP <F4>: comp ant <CR>: Mak <CR>
This command uses ant to compile the Java file. It automatically positions the cursor to an error point. Generally, the installation
Vim has the compiler/ant. Vim file, so this command can be used directly.
There is a build. xml file in the current directory, and ant must be installed.

: NMAP <F5>: CL <CR> this command is used to view all compilation errors.
: Imap <F5> <ESC> <F5>

: NMAP <F6>: CC <CR> this command is used to view the current compilation error.
: Imap <F6> <ESC> <F6>

: NMAP <F7>: CN <CR> this command is used to jump to the next error location.
: Imap <F7> <ESC> <F7>

: NMAP <F8>: CP <CR> this command is used to jump to the previous error location.
: Imap <F8> <ESC> <F8>

: NMAP <F11>: javabrowser <CR>
This command is used to split a new window at the left of the window, which contains the Java Resource Tree, including
The classes, member variables, and member methods in this file are as shown in jcreator.
In this window, enter? We can see the help. Hey hey, it's very useful, but we need the support of ctags.
: Imap <F11> <ESC> <F11>

9. Tab
Tab is a tab, Which is used separately for a section because it is really useful.

<Enter this command to move a tab to the left of the row where the cursor is located.
> Enter this command to move a tab to the right of the row where the cursor is located.
5> enter this command to move a tab to the right of the 5 rows after the cursor.
:> This command moves the data from 12 rows to 14 rows to the right of a tab.
:> This command moves the data from 12 rows to 14 rows to the right of two tabs.

How can we define the tab size? Some people prefer to use 8 spaces, 4 for some, and 2 for some.
Some people want the tab to be replaced by spaces, and some people want the tab to be tab. It doesn't matter. Vim can
Help you. The following settings are usually written to the configuration file first, so that you do not need to repeat it.

: Set shiftwidth = 4 set Automatic indent 4 spaces, of course, you must set Automatic indent first.
: Set STS = 4: Set softtabstop to 4. After Entering the tab, the four cells are displayed.
: Set tabstop = 4 the actual tab is 4 spaces, instead of the default 8.
: Set expandtab after entering the tab, VIM fills the tab with appropriate spaces.

10. autocmd
This command is very powerful. You can use this command to apply different configurations to different file formats.
The copyright statement is automatically added when a file is created. These commands are generally defined in ~ /. Configuration files such as vimrc
It is very powerful, so I cannot give a very specific description, only a few examples, please refer to the help for details.

: Autocmd! Delete all previous automatic commands.
Autocmd filetype Java source ~ /. Vim/files/Java. Vim
Autocmd filetype Java source ~ /. Vim/files/jcommenter. Vim
The above two commands allow me to apply the two configuration files mentioned later when opening the Java file.
Autocmd bufnewfile *. Java 0R ~ /. Vim/files/skeletons/Java. skel
The above command allows me to automatically add the content of the Java. SKEL file when creating a Java file.
Autocmd bufnewfile *. Java normal GNP
The above command enables me to automatically run the GNP command when creating a Java file. This command is made special
For example, replace _ date _ in the new Java file with the date of today.

11. Common scripts
In vim.sf.net, you can find many scripts that often have unexpected effects.
I often use the following methods:

Jcommenter. Vim automatically adds javadoc-style annotations.
For jbrowser. Vim resources, you can use tlist to browse. C and C ++ resources.

There are also many useful ones, such as checkstyle. Vim, which can test your programming style, and Jad. Vim can directly
Decompile. class files and so on.

12. Common configurations
In ~ /. You often need some personalized configurations in the vimrc configuration file. For example, some macro definitions and some
Autocmd definition. For example:

Set suffixes =. Bak ,~,. O,. H,. info,. SWP,. aux,. BBL,. BlG,. DVI,. LOF,. Log,. Lot,. ps,. TOC
In this way, when opening a file in Vim, it will ignore the above files when you press the tab key to complete the file name.

Set nu display row number
Set AI sets automatic indent
Map y $ let y and d be the same, otherwise y's intention is the same as yy.

13. Others
There are also many interesting commands recorded here to avoid forgetting.

. Repeat the last edit command.
: G/^/exec "s/^/". strpart (line ("."). "", 0, 4) Insert row numbers at the beginning of a row
: Runtime! Syntax/2html. Vim converts TXT to HTML, and
Color Configuration
 

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.