Vim operation notes

Source: Internet
Author: User
Tags gz file imap alphanumeric characters

V1. the default character encoding is utf8. To open a file encoded with gb2312, all Chinese characters are garbled. How can I restore the normal display?
Enter edit ++ ENC = cp936

 

V2. how to execute commands in the vim system?
Enter! CMD

 

V3. use Vim to open and edit the. * GZ File Without decompression, reading, editing, and compression.
The support for compressed files is through plugin
Vim ls-lrR.txt.gz.

V4. how does Vim support syntax highlighting?
Once and for all ~ /. Add the following statement to vimrc:
Syntax on
For a temporary method, enter the command: syntax on

V5. Vim how to quickly switch between {} blocks in the structure document, such as C language.
Enter % {}.

 

V6. how can I have Vim record my editing status so that it can quickly resume work next time?
Input: mksession file_name_your_session

 

V7. How can Vim tell you the number of bytes, number of words, and current position of the edited file?
Input g ctrl_g

 

V8. enter help to open the Help window. How can I close it quickly?
Enter the CTRL-W, and then enter the CTRL-Q,
Or enter CTRL-W Q

 

V9. I want to edit logs every day. I don't want to enter the date and time. What should I do?
Now we want to use the date format mon Sep 19 23:19:03 CST 2005, simple, defined
A shortcut key f2 to perform such a ing in VIM
: Map GG: Read! Date

 

V10. how to use Vim to change the encoding of text files?
For example, if the original file is UTF-8 encoded, you need to change it to cp936 encoding. The following command is available:
: Set fileencoding = cp936
Then save the file: W

 

V11. how do I set Vim to remember my configuration options (such as the default encoding method and file type )?
Please write your configuration command ~ /. Vimrc configuration script file

 

V12. how do I view all the include file paths of the currently edited C/C ++ source file?
Input: checkpath

 

V13. how do I save global configuration selection (available to all users )?
Enter version to view the value of the system vimrc configuration file.
Edit the system vimrc configuration file and add the command you want to run vim.

 

V14. how to quickly copy a line of text in the buffer?
Enter YY to record to the buffer, move the cursor to the position you want to paste, and press P

 

V15. how can I use macro commands to generate a numerical sequence list?
1. Create the first list entry, make sure it starts with a number.
2. QA-Start recording into buffer 'A'
3. Y-yank the entry
4. P-put a copy of the entry below the first one
5. CTRL-A-increment the number
6. Q-stop recording
7. @ A-repeat the Yank, put and Increment Times

 

V16. how to fold the text (fold )?
Automatic fold. If your file is a C source file, enter
: Set foldmethod = indent
: Set foldenable
Manually fold, select the row you want to fold, in Normal Mode
Enter the command ZF
After fold is created, move the cursor to the row where fold is located.
Enter Zo to open the corresponding fold.
ZC, disable the corresponding fold
ZM, close all fold in the file
Zr, open all fold in the file
Za, automatically turn on or off the corresponding fold

V17. how do I view the characters and characters in the current file?
Enter g CTRL-G to count all bytes in UTF-8 form.

 

V18. how can I quickly search for the selected text in visual mode?
In order not to change user habits, we still use the * and # Keys
Vnoremap * Y/ "
Vnoremap # Y? "
Note: Insert Name Register content

 

V19. howSource codeBetween different blocks/sections in?
Enter {, move forward first
Input}, move forward to the next paragraph

 

V20. how do I convert a tab in text to a space with a fixed length?
Input: Set expandtab
Input: Set tabstop = 4
Input: retab

 

In v21. vim, how do I keep the paste Clipboard data in the original format without indentation?
In paste, most of the input text will contain line breaks and some special characters. Vim will automatically indent the text and
To avoid the impact of these smart options, you can enable the paste switch.

 

V22. interesting features
Gg to the beginning of the file
GD to variable Declaration
Gg = g automatic indent, = g single line indent
K (SHIFT + k) Man
CRL +] CRL + T (ctags) at function prototype)
CTL + p auto-completion (edit status)
: X encrypted storage (password required)

Instructions for use:
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.

1. Search

/XXX (? XXX) indicates searching for strings matching XXX in the entire document,/indicates searching down ,? Indicates to 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 other queries.
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
AllProgram.

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/\/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 chain usevim.html "Row 162
2 # Rows 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
Invalid 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,
So I define them - So that it is very convenient. These configurations can be written in advance ~ /. Vimrc
($ Vim/_ vimrc in Windows), you do not need to write the first colon.

: NMAP : Nohls Deselect the highlighted string
: NMAP W command mode move cursor to different windows
: IMAP Run in input mode
: NMAP : % S = * $ = Delete unnecessary spaces at the end of all rows.
: IMAP 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 : Comp javac : Mak-D. %
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 : Comp ant : Mak
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 : CL This command is used to view all compilation errors.
: IMAP

: NMAP : CC This command is used to view the current compilation error.
: IMAP

: NMAP : CN This command is used to jump to the next error location.
: IMAP

: NMAP : CP This command is used to jump to the previous error location.
: IMAP

: NMAP : Javabrowser
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

 

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.