Home  >  Article  >  Operation and Maintenance  >  Make Vim work better - VIM mapping

Make Vim work better - VIM mapping

齐天大圣
齐天大圣Original
2020-06-12 10:38:352203browse

Mapping is my favorite vim function. Through it, I can freely set my own shortcut keys, which can greatly improve my writing efficiency. Mapping can be understood as setting shortcut keys, which allows you to replace more complicated keys with a small number of keys that are easy to press.

When to use mapping

If you have one or more of the following problems, then you can use mapping

  • Dissatisfied with the current keys

  • I want to set some shortcut keys to improve the editing speed

  • I want to combine some keys Complete new requirements

Mapping classification

We know that vim has multiple modes, corresponding to them, Mapping also includes nmap (normal mapping), imap (insertion mapping), and vmap (block selection mapping).

Let’s choose a few cases to talk about these three mappings.

Normal mapping

" 使用F1切换显示行号
nmap <F1> :set nu! nu?<cr>

The CR here means the Enter key.

Insert mapping

" 使用jj来代替esc键,完成从插入模式退回到普通模式
imap jj <esc>`^

Generally we will use jj instead of the esc key to quickly switch from insert mode to normal mode.

" 在插入模式下向前向后删除字符
imap <C-b> <Backspace>
imap <C-d> <Esc>lxi

Here c represents the ctrl key. In insert mode, use ctrl b to delete one character backward, and use ctrl d to delete one character forward.

Block mode mapping

Generally, we often increase or decrease the indentation of multi-line text. As mentioned in a previous article, this requirement can be achieved through block selection combined with the command line or through macros. Today, after learning mapping, you can use a simpler method to achieve your needs.

vmap < <gv 
vmap > >gv

Now, if we want to indent multiple lines, we only need to select these lines and press > or < to achieve it.

Recursive mapping

What will happen if you perform the next mapping

nmap dd O<esc>jddk

If you execute dd Later, serious problems will occur. Because there are dd on both sides of the mapping, it will cause an infinite loop. This infinite loop is caused by recursion, so it is called recursive mapping.

As you can see, recursive mapping is harmful, so recursive mapping is never allowed to be used in work, but non-recursive mapping must be used. So what if we do non-recursive mapping? It’s also very simple, just add 4 letters nore

  • ##nmap recursive nnoremap non-recursive

  • ##imap recursive inoremap non-recursive
  • vmap recursive vnoremap non-recursive

The above is the detailed content of Make Vim work better - VIM mapping. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn