12 tips to improve your Vim usage efficiency

12 tips to improve your Vim usage efficiency

1. Use the space bar as a leader (hot key)

Leader is a very creative design. It can execute various commands by pressing different keys in sequence without using key combinations. Since using Leader, I rarely use key combinations such as ctrl-xxx.

For a long time I used , as Leader, until I realized I could use a much better shortcut on the keyboard: the space bar (<Space>).

  1. let mapleader = "\<Space>"

This completely changed my Vim operation efficiency. I can now use any thumb of both hands to operate, while the other fingers can stay on the main key area of ​​the keyboard. Because Leader is very easy to use, I mapped various commonly used operations to Leader.

2. Map the most commonly used operations to Leader operations

I first identified the most frequently used operations and mapped them to Leader operations, which I usually use:

Use <Space>o to create a new file:

  1. nnoremap <Leader>o :CtrlP<CR>a

Use <Space>w to save the file (significantly faster than :w<Enter>):

  1. nnoremap <Leader>w :w<CR>

Use <Space>p and <Space>y to copy and paste from the clipboard:

  1. vmap <Leader>y "+y
  2. vmap <Leader>d "+d
  3. nmap <Leader>p "+p
  4. nmap <Leader>P "+P
  5. vmap <Leader>p "+p
  6. vmap <Leader>P "+P

Use <Space><Space> to enter Vim editing mode:

  1. nmap <Leader><Leader> V

I strongly recommend that you find the most common operations and map them to Leaders.

3. Use the region expansion function

Make key mapping for the plugin terryma/vim-expand-region:

  1. vmap v <Plug>(expand_region_expand)
  2. vmap <Cv> <Plug>(expand_region_shrink)

So I can:

  • Press v once to select a character.
  • Press v again to automatically expand the selected word.
  • Press v again to automatically expand the selected code.
  • And so on…
  • Press <Cv> to rewind the last selection operation.

Although vvv seems to be slower than vp, when using this method, I don’t need to think about what to select and which key combination to use.

In this way, v replaces operations such as viw, vaw, vi", va", vi(, va(, vi[, va[, vi{, va{, vip, vap, vit,vat, …. Now you understand.

4. Find the text search operation tool

I never really liked the search and replace operation in Vim until I found the following configuration on the Vim wiki:

  1. vnoremap <silent> s //e<Cr>=&selection=='exclusive'?'+1':''<CR><CR>
  2. :<Cu>call histdel('search',-1)<Bar>let @/=histget('search',-1)<CR>gv
  3. omap s :normal vs<CR>

This is a direct replacement for my usual sequence of operations:

  • Use /something to find
  • Replace the first one with cs and then press <Esc>
  • Find and replace remaining matches with nnnnn.

PS: You can also consider using the cgn command provided by Vim 7.4.

5. Try more awesome keyboard mappings

I use these shortcuts every day, and I've estimated that they've saved me months of time.

Automatically jump to the end of the pasted text

Use ppppp to paste multiple lines at once

  1. vnoremap <silent> y y`]
  2. vnoremap <silent> p p`]
  3. nnoremap <silent> p p`]

Prevent buffer contents from being overwritten after pasting

The following configuration can prevent the buffer content from being overwritten by the deleted text content (put it at the end of the ~/.vimrc file)

  1. " vp doesn't replace paste buffer
  2. function!RestoreRegister()
  3. let @" = s:restore_reg
  4. return ''
  5. endfunction
  6. function! s:Repl()
  7. let s:restore_reg = @"
  8. return "p@=RestoreRegister()<cr>"
  9. endfunction
  10. vmap <silent> <expr> p <sid>Repl()

Translation note: There is no Title here, probably the author missed it, so I added one.

Quickly jump within a file

  • Use 12<Enter> to jump to line 12 (12G is not suitable for me)
  • Press <Enter> to jump to the end of the line file.
  • Press <Backspace> to return to the beginning of the file.
  1. nnoremap <CR> G
  2. nnoremap <BS> gg

Quickly select pasted text

  1. noremap gV `[v`]

Close boring window prompts

  1. map q: :q

6. Improve unit test execution efficiency

I used the vim-vroom plugin and the corresponding tmux configuration to perform my tests.

By default, vim-room uses <Leader>r to execute tests. Since I have mapped Leader to <Space>, I use <Space>r to run the test tool.

Since the tests are run in a separate tmux window, I can review my code while watching the tests progress.

#p#

7. Use Ctrl-Z to return to Vim

I often need to execute some commands in the shell. I suspend Vim through Ctrl-Z, and after completing the shell command execution, I return to Vim through <Enter>.

Using fg to exit Vim was annoying, and I just wanted to be able to switch between Vim and the shell with Ctrl-Z , but I couldn't find a solution, so I wrote a script that works perfectly under ZSH:

  1. fancy-ctrl-z () {
  2. if [[ $#BUFFER -eq 0 ]]; then
  3. BUFFER="fg"
  4. zle accept-line
  5. else
  6. zle push-input
  7. zle clear-screen
  8. fi
  9. }
  10. zle -N fancy-ctrl-z
  11. bindkey '^Z' fancy-ctrl-z

If you put the above code in your ~/.zshrc file, you can quickly switch between shell and Vim, it is really worth trying it.

8. Configure Tmux Correctly

Using Tmux and Vim tools on OS X is very inconvenient because:

  • The system's clipboard handling function is very weak
  • Vim and Tmux have different window switching operations
  • The hotkeys for executing commands in Tmux are different (using Cb )
  • Copy mode in Tmux is super hard to use

I spent a lot of time to correct the above configuration, as shown below:

Configure to use <C-Space> as tmux hotkey

Some people are used to using <Ca> as a hotkey, but I use this hotkey to go back to the beginning of the line, so I won't go into details here. Using <C-Space> is more convenient, and I'll explain the reason later:

  1. unbind Cb
  2. set -g prefix C-Space
  3. bind Space send-prefix

Use <Space> to enter copy mode

Imagine how convenient it is to use <C-Space><Space> to directly enter Tmux's copy mode.

  1. bind Space copy-mode
  2. bind C-Space copy-mode

Use y and reattach-to-user-namespace (OSX based)

Before using the system clipboard, you need to run brew install reattach-to-user-namespace

  1. bind-key -t vi-copy y
  2. copy-pipe "reattach-to-user-namespace pbcopy"

Using vim-tmux-navigator

You need to use the shortcut keys <Ch>, <Cj>, <Ck>, <Cl> to quickly switch between various windows of vim and tmux.

I also recommend using the <C-Space>l and <C-Space>j mappings for Tmux window splitting, which is definitely faster than using <C-Space>% and <C-Space>|:

  1. bind j split-window -v
  2. bind Cj split-window -v
  3. bind l split-window -h
  4. bind Cl split-window -h

See my tmux.conf file for more info.

9. Improve the efficiency of Ctrl-P in Git projects

Add the following to your .vimrc file (configure using <Ctrl-P> to use git or silver finder for autocompletion):

  1. let g:ctrlp_use_caching = 0
  2. if executable('ag')
  3. set grepprg=ag --nogroup --nocolor
  4. let g:ctrlp_user_command = 'ag %s -l --nocolor -g ""'
  5. else
  6. let g:ctrlp_user_command = ['.git', 'cd %s && git ls-files . -co --exclude-standard', 'find %s -type f']
  7. let g:ctrlp_prompt_mappings = {
  8. 'AcceptSelection("e")': ['<space>', '<cr>', '<2-LeftMouse>'],
  9. }
  10. endif

I recommend using the vim-scripts/gitignore plugin.

10. Use a package manager

neobundle.vim is a powerful tool for managing Vim plugins:

  • You don't need to manually manage git submodules
  • Ability to install and update plugins in parallel
  • It supports plugins like YouCompleteMe that require build
    1. NeoBundle 'Valloric/YouCompleteMe', {
    2. 'build' : {
    3. 'mac' : './install.sh',
    4. },
    5. }
  • There are also plugins that support patching such as pry:
    1. NeoBundle 'rking/pry-de', {'rtp': 'vim/'}

11. Take advantage of Vim plugins

  • YouCompleteMe
  • ack.vim (ag.vim is also good)
  • tpope/vim-commentary
  • tpope/vim-rsi
  • tpope/vim-endwise
  • tpope/vim-fugitive mainly uses :Gblame
  • tpope/vim-repeat
  • tpope/vim-sleuth
  • mmozuras/vim-github-comment
  • vim-airline and add the following configuration:
    NeoBundle 'bling/vim-airline'
    let g:airline_theme='powerlineish'
    let g:airline_left_sep=''
    let g:airline_right_sep=''
    let g:airline_section_z=''

I'm a Ruby programmer, so I also use some Ruby plugins:

  • tpope/vim-rails
  • vim-textobj-rubyblock (use var, vir to find ruby ​​code blocks)
  • ruby_pry
  • AndrewRadev/splitjoin.vim configures the following mappings nmap sj :SplitjoinSplit<cr> nmap sk :SplitjoinJoin<cr>

12. Quickly configure Vim on the server

I often need to use Vim for configuration on my servers, and unfortunately Vim's default configuration is pretty unreasonable.

One solution is to use the vim-sensible plugin to generate a compressed package, but this is not good enough for me. I wrote a vimrc plugin to do a reasonable initialization of Vim (especially for Ruby developers). This plugin configures Vim to only use ~/.vimrc as the configuration file. It also includes optimized color matching, package management tools, and syntax coloring for multiple development languages.

This means that I don't need to manually configure the ~/.vim directory on the server. Instead, I can easily configure the Vim environment on the server by doing the following:

  1. git clone --recursive https://github.com/sheerun/vimrc.git ~/.vim

I also wrote a dotfiles to quickly configure my development environment.

think

The key to using Vim well is to constantly discover Vim problems you encounter during the software development process and actively deal with them.

The solution could be to add a keymap to your .vimrc, search for a solution on google, ask on IRC, or something else.

How have you improved your Vim usage efficiency?

<<:  How I taught myself Android, sharing my experience

>>:  Siri may be able to answer calls for you and convert them into text messages

Recommend

Pornographic mobile games investigated: Just wear more clothes?

These days, gamers may find that the female charac...

Apple releases iOS 11.4 beta 2 with more than just ClassKit

Recently, Apple released iOS 11.4 beta 2. Two wee...

How Big Data Analytics Can Create Better Mobile App User Experiences

Nowadays, more and more people use mobile applica...

Vehicles brake by friction, what do ships brake by?

Braking devices are essential for the safe drivin...

Android 11 new improvements exposed: remove the 4GB size limit for single files

According to the XDA forum, Google may remove the...

Why does China launch so many satellites?

This article was reviewed by Liu Yong, space phys...

12 rules to tell you how to write a headline that gets 100,000+ views

Content is the core at all times. But a good cont...