A summary of common usage of vim

Source: Internet
Author: User
Tags using git



You are welcome to exchange technical issues with us:
Email: [email protected]
Blog Park Address: Http://www.cnblogs.com/jiangxinnju
GitHub Address: Https://github.com/jiangxincode
Address: Https://www.zhihu.com/people/jiangxinnju


How the cursor returns to the last position in vim


CTRL + O


How does vim delete the contents of a row to a row?


Delete the contents between rows 3104 through 5403:

31045403d
VIM keep the cursor in the center of the screen
ZZ is the volume page that brings the cursor to the middle
M is to move the cursor to the middle
: set scrolloff = 10 can keep the cursor in the middle, and the size of the adjustment number can control the area
Column edit operation of vim
Delete column: position the cursor where you want to operate. CTRL + V enters "visible block" mode, and selects how many rows this column operates on. D deleted.
Insert column: for example, we insert "()" before each row: the cursor is positioned to the place to be operated. CTRL + V enters "visible block" mode, and selects how many rows this column operates on. Shift + I (I) enter what you want to insert. Press ESC twice, and the inserted content will appear in the selected area of each row.
Using VIM to query function usage
When the cursor is at the function name: press K to enter the function description (equivalent to man), or use NK to set the man level
VIM cross file replication
Open a file, copy several lines under the file to another file (such as test.txt), which will overwrite the content in the target
10,100w!test.txt
Copy several lines under the file to another file, but do not overwrite the original content, that is, append
10,100w!>>test.txt
Copy lines to buffer in one file
"Anyy
Paste in another file
"AP
Solve the problem of paste format in VIM
Sometimes copying and pasting code from the editor to VIM will lead to a complete confusion of code formats. The reason is that VIM turns on smart reduction or Autoalignment mode. To maintain the code format, you can stop the above two modes before pasting. The command is:
:set nosmartindent
:set noautoindent
It's a real hassle to do so much for a paste. However, there is a simpler way to start paste mode with the command, that is:
Set paste
Because paste mode is mutually exclusive with the above modes of smartent and autoindent, and smartent is indispensable, use one of the following two commands to turn off paste mode after paste.
Set nopaste
Set paste!
In addition, you can quickly switch by binding a custom shortcut key. For example, add the following configuration to. Vimrc
Mode 1:
set pastetoggle=<F4>
Mode 2:
:map <F8> :set paste
:map <F9> :set nopaste
Note: mode 1 can be used in both reading and editing modes to switch on and off paste mode; mode 2 can be used in reading mode. Pressing the corresponding shortcut key is equivalent to executing the command defined later.
Solve the problem that the backspace key cannot be deleted in insert mode
VIM in insert mode
:set backspace=indent,eol,start
Or:
set nocompatible
VIM replacement
: 0, $s / ^ / ා / GC "with a ා - sign at the beginning of the line
: 6,10s / ^ / # / GC "add a # - sign at the beginning of lines 6-10
:% s / ^ * / / g "remove space at beginning of line
:% s / * $/ / g "remove space at end of line
:% s / ^ \ n / / g "delete empty line
: g / ^ s * $/ D "delete empty lines
VIM partial sort
If we want to sort by column 4 data, we can do this in VIM:
1,12!sort -r -n -k4.1,5
-R descending sort
-N sort by number size
-k. It means sorting according to that field. 4.1 means the beginning of the first character in column 4, and 5 means the end of the fifth field
-T followed by a separator, the default is space
In VIM, if you want to sort alphabetically 20 lines below the current line
. +20! Sort
Open and display multiple files at the same time
When VIM is not started: input in the terminal
vim file1 file2 ... filen
VIM started, enter
Open file
Show multiple files at the same time:
Split
VSplit
For files opened in multiple panes with (V) split, this method only switches between different files in the current pane.
CTRL + 6 - next file
: BN - next file
: BP - previous file
How to switch between panes
CTRL + W + direction key -- switch to the front / bottom / top / back pane
CTRL + W + H / J / K / L - same as above
CTRL + WW -- switch back to the next pane
Modify the file code to UTF-8 with VIM
Web pages often appear the situation of garbled code, which is generally caused by incorrect coding settings. For example, the encoding of a web page source file is not utf8, but it is declared as utf8 < meta http-equiv = "content type" content = "text / HTML; charset = UTF-8" / > the web page will appear garbled. You can use VIM to change the source file code to utf8. The command is
:set fileencoding=utf-8
If there is a garbled code in the file when you open it with VIM, you may not be able to save it after modifying the file with the above command. You can open the file with other software, copy the content to vim and save it.
GVIM encoding configuration
There are two problems in the character encoding of vim / GVIM under Chinese windows:
No code detection function by default
If the character set of a file itself is larger than GBK (such as UTF-8, utf-16, GB18030), the characters that can't be corresponding in GBK will appear garbled, and will be lost when saving. It doesn't help to detect the file format correctly when editing a file.
The solution to the first problem is to add the following configuration to ~ /. Vimrc:
set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1
The solution to the second problem is to force VIM's internal encoding to use some UTF encoding. For example, UTF-8:
set encoding=utf-8
However, setting VIM's internal encoding to UTF-8 creates the following new problems
When using VIM of non GUI interface, it will be garbled
The prompt message (e.g. e492: not editor command: foo) will be garbled
In order to solve the problem of miscoding of vim in non GUI interface, it is necessary to set the terminal code as the system default code:
set termencoding=cp936
In order to keep the prompt information from being garbled, you need to use the UTF-8 version of the prompt information:
language messages zh_CN.UTF-8
To sum up, to correctly configure character encoding in Chinese windows, you need to add the following content to your ~ /. Vimrc
set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1
set encoding=utf-8
set termencoding=cp936
language messages zh_CN.UTF-8
What is the
Help leader
Installation of ctags
Ctags tool is used to traverse the source code files to generate tags files. These tags files can be used by editors or other tools to quickly find and locate the symbols (tag / symbol) in the source code, such as variable name, function name, etc. For example, the tags file is the basis of taglist and omnicppcomplete.
sudo apt-get install ctags
Run ctags-r in the root directory of the program, generate tags file, and press Ctrl +] when editing the program to jump to the definition of the current cursor. If there are multiple tags, execute: ts to select. Press Ctrl + O to jump back. However, when the code has been modified, the tags need to be regenerated.
VIM reload file
Sometimes you need to use VIM to open some files, but you have changed the secondary file in other places, such as using git for checkout and other operations. You need to reload this file.
1 reload current file:
E
: e! Discard the current modification and force reload
2 reload all open files:
: bufdo e or: bufdo: e!
The: bufferdo command means to apply the following commands to all files in the buffer.
toggle case
The command of case conversion in VIM is: Gu or Gu. The image explanation is that small U means to be lowercase, and large u means to be uppercase. Next, I'll explain the restrictions on these two commands (the lines, letters, words that define the operation), and so on.
Full article case conversion
After opening a file, you do not need to enter command line mode. typing
:ggguG/:gggUG
Explain: GG GG GG is divided into three segments: GG Gu (U) g
GG = cursor to first character of file
Gu (U) = write all the selected ranges small (large)
G = to end of file
Guw, GUI, guw and gue only convert a word, and the words behind the cursor will be case converted
Gu5w, gu5e, gu5w, gu5e convert 5 words
1gu convert lowercase to uppercase from the next line of the cursor
10gu converts 11 lines from lowercase to uppercase
GU0 is capitalized from the cursor position to the beginning of the line
Gu $is capitalized from the cursor position to the end of the line
GUG changes to uppercase from the cursor position to the last character of the article
Gu1g is capitalized from the cursor position to the first character of the article
VIM script
Map
Re: reduce mapped sequence is recursively mapped
I: insert
N: normal
No: no
Python related configuration of vim7.4
Here we only discuss the support and configuration of the official Windows version installation file for Python. As for the compilation of vim, we generally know how to configure python, not here. The official GVIM installation file supports two modes by default: Python and python3. This option is available at compile time, but there is no corresponding runtime library and environment attached. Therefore, when Python is not installed locally, it is directly executed in vim
:py echo "ABCDE"
You will be prompted that python27.dll cannot be loaded. For this reason, please download the 32-bit Python 2.7. X installation file of Windows version. Using 64 bit Python cannot be used in GVIM normally. Python 3. X series is still not supported in some VIM related plug-ins, so 2.7. X is still recommended
Format code with VIM
Found in VIM's official FAQ (http://vimdoc.sourceforge.net/cgi-bin/vimfaq2html3.pl):
Format full text: GG = g
Auto indent current line:==
This is an excerpt from the original:
14.6. How do I format/indent an entire file?
You can format/indent an entire file using the gg=G command, where
gg - Goto the beginning of the file
=   - apply indentation
G   - till end of file
For more information, read
Help GG
Help =
Help G
:help ‘formatprg‘
:help C-indenting
VIM edit binary (recorded in VIM manual)
Help document: 23.4
VIM merges all lines of the file into one line
In normal mode:
GgvGJ
GG is used to jump to the beginning of a line
Convert V to visual mode
G jumps to the last line
J parallel
How can VIM view dynamically growing log files
:set autoread
Summary of common usage 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.