Some common settings for VIm

Source: Internet
Author: User
Tags imap



The following content is from the network, thanks to the original author. If the citation is incorrect, please let us know to modify it.


1. Several modes of VIM and key mapping


Reprinted from: "1"



Map is a powerful reason for vim, you can customize a variety of shortcut keys, used naturally handy.
The most basic map usage in Vim is



: Map C A
Here the C is mapped to a, in case the map is in effect, press C is equivalent to pressing a
Naturally, the usual ctrl,shift,alt are also supported.



Make Ctrl + a correspond to a
: Map <C-a> A
Make the alt+a correspond to a
: Map <A-a> A
Make the ctrl+alt+a correspond to a
: Map <C-A-a> A
We have been able to do a lot of things here.
But the map command is far more than this one, and in different modes, the same set of keys can be mapped to different combinations.
There are many modes of vim, but it is generally mentioned in the following ways:



Normal Mode
That is, the general Normal mode, the default into Vim, in this mode.



Visual Mode
General translation visual mode, in which some characters, rows, and multiple columns are selected.
In normal mode, you can press V to enter.



Insert Mode
Insert mode, in fact, refers to the state of editing input. In normal mode, you can press I to enter.



Select Mode
In the gvim under the commonly used mode, can be called as a choice mode bar. When you drag the area with the mouse, you enter the selection mode.
And the visual mode is different, in this mode, after the selection of the highlighted area, tap any button to directly enter and replace the selected text.
The same effect as the editor selected under Windows. In normal mode, you can enter by GH.



Command-line/ex Mode
It's called command-line mode and ex-mode. The two are slightly different, normal mode by the colon (:) into command-line mode, you can enter a variety of commands,
Use the various powerful features of VIM. In normal mode, pressing Q into the EX mode is actually a multi-line command-line mode.



For map, there are several basic concepts



Combination of commands
Like other commands under Vim, the name of the command is often made up of several paragraphs. The prefix is used as the modifier of the command itself to fine-tune the effect of the command.
For map, there may be several prefixes



Nore
Represents non-recursive, see the following introduction



N
Indicates effective in normal mode



V
Indicates effective in visual mode



I
Indicates effective in insert mode



C
Indicates effective in command-line mode



Recursive Mapping
A recursive mapping. In fact, it is very well understood, that is, if the key A is mapped into a b,c and mapped to a, if the map is recursive, then C is mapped to B.



: Map a B
: Map C A
For C effects equal to



: Map c B
The default map is recursive. If a prefix such as [Nore] is encountered, such as noremap, it means that the map is non-recursive.



Unmap
Unmap followed by a key combination, indicating the deletion of this mapping.



: Unmap C
Then, in map effective mode, C is no longer mapped to a.



Similarly, unmap can add a variety of prefixes to indicate the pattern that is affected.



Mapclear
Mapclear directly clears all mappings in the correlation mode.
Similarly, mapclear can add a variety of prefixes to indicate the pattern that is affected.



Some commonly used map commands are listed here, and the default map command affects both normal and visual modes.



: Map:noremap:unmap:mapclear
: Nmap:nnoremap:nunmap:nmapclear
: Vmap:vnoremap:vunmap:vmapclear
: Imap:inoremap:iunmap:imapclear
: Cmap:cnoremap:cunmap:cmapclear



You can try these commands.



command-line mode to build a mapping
Nmap B A
Now in normal mode, press B, you can enter the insertion mode, just type some characters
command-line mode to build a mapping
Vmap b D
Now in normal mode, press V, enter the visual mode, and select a whole line, press B, you can delete the entire row
command-line mode to build a mapping
IMAP B A
Now try typing a character "B" into the file you are editing:p
command-line mode to build a mapping
CMap b C
command-line mode, press B to come up with a
Well, the keys to this vim have been messed up by you, try to clear the mapping with Unmap and mapclear. :]


Reference


Several modes and key mapping for "1" vim (http://haoxiang.org/2011/09/vim-modes-and-mappin/)





2. What is the role of <leader> in keyboard mapping?


The <Leader> key is mapped to \ by default. So if you had a map of <leader>t, you can execute it by default with \ t"1".



For more detail or re-assigning it using the Mapleader variable, see



: Help leader



To define a mapping which uses the "Mapleader" variable, the special string
"<Leader>" can be used. It is replaced with the string value of "Mapleader".
If "Mapleader" is not set or empty, a backslash is used instead.
Example:
: Map <leader>a Oanother Line <Esc>
Works like:
: Map \a Oanother Line <Esc>
But after:
: Let Mapleader = ","
It works like:
: Map, A Oanother line <Esc>



Note that the value of "Mapleader" was used at the moment, the mapping is
Defined. changing "Mapleader" after that have no effect for already defined
Mappings.


Reference


"1" What's the <leader> in a. vimrc file? (http://stackoverflow.com/questions/1764263/ What-is-the-leader-in-a-vimrc-file)





3. Vim several parameters related to the tab


Reprinted from "1":



Normal Mode:

>> Increase indent of current line
<< Current line decreases indent
In insert mode:

CTRL + SHIFT + T: increase the indent of the current line (tried under Ubuntu, wrong)
CTRL + SHIFT + D: reduce the indent of the current line
Tab-related parameters include shiftwidth, tabstop, softtabstop, and expandtab.

shiftwidth The number of columns to indent during indentation operations (<< and >>) (here one column is equivalent to a space)
tabstop The number of columns occupied by a tab key. The Linux kernel code recommends that each tab occupy 8 columns
softtabstop The number of columns actually occupied when typing the tab key.
expandtab automatically converts it into spaces when entering a tab
When softtabstop is greater than tabstop, and expandtab is not set, for example: softtabstop = 12, tabstop = 8, then when entering a tab (softtabstop: actually occupying 12 columns), it will eventually become a tab (tabstop) plus 4 Space (8 + 4), enter two tabs (2 softtabstop: 24 columns) will become 3 tabs (tabstop), that is to say vim or use tabstop + space to represent, in the end you can see the indented column The number must be the number of tab keys pressed by softtabstop *. (Ps :: set list can view the tab symbol)

When softtabstop is smaller than tabstop and expandtab is not set, if softtabstop = 4, tabstop = 8, enter a tab (softtabstop), it will become 4 spaces (because it is not enough to use a tabstop), enter two tabs will become a tab ( 8 columns).

If softtabstop is equal to tabstop, and expandtab is not set, there is no difference between softtabstop and tabstop.

If you set expandtab, enter a tab, it will be expanded into softtabstop value spaces, if softtabstop = 4, then a tab will be replaced with 4 spaces.

Commonly used tab settings are:

"Set tab indent to space
set expandtab "set et
"Set line breaks to automatically indent to 4 spaces
set shiftwidth = 4
"Set the tab key indent to a distance of 4 spaces, vim default is 8
set tabstop = 4 "set ts = 4
"When typing the tab key, an actual number of columns occupied
set softtabstop = 4

Reference
[1] One Vim (9) per day-indent (http://liuzhijun.iteye.com/blog/1831548)

 

4. Vim detects the file type
Execute: filetype to check whether Vim's file type detection function is turned on. By default, you will see: detection: ON plugin: OFF indent: OFF.

The specific meanings of these three parameters are described in detail below [1].

detection: By default, vim will automatically detect the file type of the file, which is the ‘detection: ON’ you see. Similarly, you can manually close it: filetype off. You can use: set filetype to see what type the current file is. If the filetype of the file.txt file is set to python, then the display effect is the same as the ordinary python file: set filetype = python.

Another way is to specify in the file content, Vim will automatically scan the file from the first few lines of the file for the code that declares the type of the file, such as adding # vim: filetype = python at the beginning of the line of the file / * vim: filetype = java * /, in short, this line is used as a comment, so that it does not affect the compilation of the file, so Vim can detect what type the file is without the file name.

plugin: If the plugin status is ON, then this type of plugin will be loaded in the Vim runtime environment directory. For example, in order for Vim to better support Python programming, you need to download some Python-related plugins. At this time, you must set the plugin to ON to take effect. The specific setting method is: filetype plugin on

indent: There are different ways for different types of files. For example, Python requires 4 spaces for indentation, and c uses two tabs for indentation, so indent can choose the appropriate indentation method for different file types. You can see many indent-related scripts defined in the indent directory of Vim's installation directory. Specific setting method: filetype indent on.

The above three parameters can be written as a line filetype plugin indent on and set in the _vimrc file.

Reference
[1] Daily Vim (25) filetype ---- file type detection (http://liuzhijun.iteye.com/blog/1846123)

 

5. Several ways for vim to automatically indent
There are a number of methods enabling automatic indentation in Vim, ranging from fairly "stupid" and unintrusive ones, like 'autoindent' and 'smartindent', to complex ones such as 'cindent' and custom indentation based on filetype using 'indentexpr'. The amount of indentation used for one level is controlled by the 'shiftwidth' option (the number of indentations is controlled by the 'shiftwidth' setting). [1]

‘Autoindent’
‘Autoindent’ does nothing more than copy the indentation from the previous line, when starting a new line. It can be useful for structured text files, or when you want to control most of the indentation manually, without Vim interfering.

'autoindent' does not interfere with other indentation settings, and some file type based indentation scripts even enable it automatically. s Choice)

‘Smartindent’ and ‘cindent’

‘Smartindent’ automatically inserts one extra level of indentation in some cases, and works for C-like files. ‘Cindent‘ is more customizable, but also more strict when it comes to syntax.

‘Smartindent’ and ‘cindent’ might interfere with file type based indentation, and should never be used in conjunction with it.

When it comes to C and C ++, file type based indentations automatically sets 'cindent', and for that reason, there is no need to set 'cindent' manually for such files. In these cases, the 'cinwords', 'cinkeys' and 'cinoptions' options still apply.

Generally, 'smartindent' or 'cindent' should only be set manually if you're not satisfied with how file type based indentation works. (You only need to set 'smartindent' or 'cindent')

 Refer to the introduction of [2],

smartindent is an old script that was meant, when it was written, to be a "smart" complement to autoindent. Since then, most languages have either specific indentation functions or use cindent with specific options.

Generally, smartindent should n‘t be used at all.

The following lines are usually enough to deal with indentation (usually, the following settings are sufficient):

set autoindent
filetype plugin indent on

Reference
[1] Methods for automatic indentation (http://vim.wikia.com/wiki/Indenting_source_code)

[2] autoindent is subset of smartindent in vim? (Http://stackoverflow.com/questions/18415492/autoindent-is-subset-of-smartindent-in-vim)

 

6. Vim paste code format is disordered
Sometimes copy and paste the code from the editor into vim, the code format will be completely messy. The reason is that vim has turned on smartindent (intelligent reduction) or autoindent (automatic alignment) mode. In order to maintain the format of the code, you can stop the above two modes before pasting. The command in Normal mode is [1]:

: set nosmartindent
: set noautoindent
It's really troublesome to do so many things for one paste. However, there is an easier way to start the paste mode with the command, namely:

(1) Open
: set paste
(2) Press i to enter insert mode, and then perform the paste operation
Why do you need to enter insert mode before pasting? If the content you want to paste contains the character i, in Normal mode, the character i will be regarded as an insert command by Vim, and the content before the i character will be lost (such as copy #include <stdio.h> Nclude <stdio.h>).
(3) Close
: set nopaste or: set paste!
Since the paste mode and the smartindent and autoindent modes above are mutually exclusive, and smartindent and autoindent are indispensable, use one of the above two commands to close the paste mode after pasting.

 

In addition, you can quickly switch by binding a custom shortcut key, such as adding the subordinate configuration to .vimrc

Method 1:
set pastetoggle = <F4>

Method 2:
: map <F8>: set paste
: map <F9>: set nopaste
Note: Method 1 can be used in both reading and editing modes, switching the paste mode on and off; method 2 is used in reading mode, pressing the corresponding shortcut key is equivalent to executing the commands defined later.

Reference
[1] Vim paste code format is disordered (http: //www.netingcn.com/vim-paste-mode.html)

 

7. Vim folding settings
1. Folding method 【1】
Use the option ‘foldmethod’ to set the folding method: set fdm = *****.
There are 6 ways to select a fold:
manual Manually define folding
indent More indentation means higher levels of folding
expr uses expressions to define folds
syntax uses syntax highlighting to define folds
diff collapses text that has not changed
marker folds the marker in the text
Note that each folding method is incompatible. If expr and marker methods are not available, I mainly use the indent and marker methods for folding.

When using, use: set fdm = marker command to set the marker folding mode (fdm is the abbreviation of foldmethod).
To make folding effective every time you open vim, add settings in the .vimrc file, such as adding: set fdm = syntax, just like adding other initialization settings.

2. Fold command

zi / za open and close folding
zv view this line
zm close fold
zM close all
zr open
zR opens all
zc collapse current line
zo opens the current fold
zd delete fold
zD delete all folds

3. How to not fold by default when opening a file? 【2】

setfoldlevel = 99

Should open all folds, regardless of method used for folding. With foldlevel = 0 all folded, foldlevel = 1 only somes, ... higher numbers will close fewer folds.

VIm some commonly used settings

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.