28 Jan 2022

Setup Your Pure VIM Dev Environment--Part II

VIM SETUP

[ Y-2022   vim   configuration   tools   summary   ]

Install Vim Configurations

We have uploaded our vim configurations to repo, and you can install with following script:

$ wget -qO - https://raw.github.com/sunbingfeng/dot-vim/master/setup.sh | bash

Usage

Basic

  • Leader key is set to ,, and you can change it to any character as you like
  • :: input a command manually
  • ,+h:: show history vim commands, and it can boost the input speed of command

NERDTree: the File Explorer

A typical NERDTree workspace contains a navigation pane, and several file panes.
Following is my blog project opened through NERDTree:

img

Some key mappings used with high frequency:

  • ,+n: toggle NERDTree pane
  • ,+v: open NERDTree pane and the cursor focuses on current file
  • ,+tn: switch to the next tab
  • ,+tp: switch to the previous tab
  • Control+h/j/k/l: switch between different panes

When the cursor is at the NERDTree pane, you can:

  • Press ? to show list of quick help
  • Press j, k to navigate down/up through files, and press Enter to open it in current active pane
  • i: open the file under cursor in a vertical split pane
  • s: open the file under cursor in a horizontal split pane
  • t: open the file under cursor in a new tab

You can also press m at the NERDTree pane to toggle the menu:

img

Thus, you can copy/move/delete file according to the tips.

Easy Navigation

Modern IDEs provide many useful shortcuts to make you code easier, e.g., goto definition, goto file, etc.
If Vim can’t do that, it definitely will drop out. However, basic Vim doesn’t have these features. Luckily, we can realize these functions through plugins, e.g., FZF in our configuration.

High Frequency Usage

  • Control+p: search file by name
    img
  • Control+g: search symbols at global range
    img
  • Control+f: list symbols in current file, and you can view functions/members easily. Mote that you should create tags first according to tips in README
    img
  • ,+b: list opened buffers, and previous accessed file will be at the highest priority. You can press Enter to switch back and forward.
  • ,hh: list history accessed files

Bookmark Management
You can also bookmark at where you add a TODO, or the line of code you want to access quickly later.

img

  • mm: toggle bookmark at current line
  • mc: clear all bookmarks
  • ma: list all bookmarks

Pure Coding

The charm of Vim is that it provides you the probability to focus on the pure coding, and it definitely will make you happier.
In Vim, key shortcuts are everything. If you master them well, you can almost do everything getting rid of mouse. Not only can it boost your coding efficiency, but also avoid wrist and finger pain caused by heavy mouse use. These are huge advantages over other alternatives.
Coding with ease
There are many tutorials related to Vim, e.g, [1], and actually it’s impossible to give you an entire introduction in a few words. You can google yourself, and I only list some basics here:

  • h/j/k/l: [normal mode]move left/down/up/right
  • Control+u/d: [normal mode]page scroll up/down
  • i/a/o/s: enter edition mode; jk: exit edition mode
  • u: undo edition; Control+r: redo; Control+s: save

Beautify Your Code
It’s nearly impossible to write a totally formatted code with just manual typing, so we need the tools to assist us.
We use clangformat to beautify our c++ code with specified style, and yapf on python code.

  • ,cf: format your code with configured style, e.g., google style for .cpp files

View Code Changes

  • [c: take you to previous modified place
  • ]c: take you to next modified place
  • ,hu: revert your specified change quickly and one by one

That’s all, hope you have a nice journey to work with pure Vim environment!

Reference