Vim Tips & Tracks

Why I Still Use Vim

  • Vim is exists on all Unix systems, support all file formats and programming languages
  • Mouse free navigation
  • Vim is very light-weight and fast.
  • Vim is highly configurable. (~/.vimrc)
  • Memory Usage Low
  • Startup Time short

Installing

1
2
3
4
5
6
7
> Since VIM comes pre-installed on a number of *nix systems, let's first check to see if it's installed
$ vim --version

# *NIX/Linux (Debian or Ubuntu)
$ sudo apt-get remove vim-tiny
$ sudo apt-get update
$ sudo apt-get install vim
  • VIM Extensions

    • vim-plug

      Minimalist Vim Plugin Manager

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    " junegunn/vim-plug -- 🌺 Minimalist Vim Plugin Manager
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    if empty(glob('~/.vim/autoload/plug.vim'))
    silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs
    \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
    autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
    endif

    " vim-plug configuration
    " For Mac/Linux users
    " Plugins will be download under the specified directory.
    call plug#begin('~/.vim/plugged')

    "List ends here. Plugins become visible to Vim after this call.
    call plug#end()

Vim Tips

  • Pro Tip #1: Make sure to utilize tab completion to find files after typing
  • Pro Tip #2: Specify different areas of the screen where the splits should occur by adding the following lines to the .vimrc file
    1
    2
    set splitbelow
    set splitright
  • Pro Tip #3: Jump between splits with just one key combination
    1
    2
    3
    4
    5
    " split navigations
    nnoremap <C-J> <C-W><C-J>
    nnoremap <C-K> <C-W><C-K>
    nnoremap <C-L> <C-W><C-L>
    nnoremap <C-H> <C-W><C-H>
  • Pro Tip #4: Buffers
    1
    2
    # :ls - to list all buffers
    # :b <buffer number> - pick the buffer immediately
  • Pro Tip #5: Code Folding
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    # Collapse (or fold) methods and classes
    " Enable folding
    set foldmethod=indent
    set foldlevel=99

    " Enable folding with the spacebar
    nnoremap <space> za

    # VimL Script
    plug "tmhedberg/SimpylFold"

    let g:SimpylFold_docstring_preview=1
  • Pro Tip #6: PEP 8
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    " Add the proper PEP 8 indentation
    au BufNewFile,BufRead *.py
    \ set tabstop=4
    \ set softtabstop=4
    \ set shiftwidth=4
    \ set textwidth=79
    \ set expandtab
    \ set autoindent
    \ set fileformat=unix

    " full stack development
    au BufNewFile,BufRead *.js, *.html, *.css
    \ set tabstop=2
    \ set softtabstop=2
    \ set shiftwidth=2
  • Pro Tip #7: Flagging Unnecessary Whitespace
    1
    au BufRead,BufNewFile *.py,*.pyw,*.c,*.h match BadWhitespace /\s\+$/
  • Pro Tip #8: UTF-8 Support
    1
    set encoding=utf-8
  • Pro Tip #9: Syntax Checking/Highlighting
    1
    2
    3
    4
    5
    6
    7
    8
    # check syntax on each save with the syntastic extension
    Plug 'vim-syntastic/syntastic'

    # add PEP 8 checking with this nifty little plugin
    Plug 'nvie/vim-flake8'

    let python_highlight_all=1
    syntax on

File Browsing

  • NERDTree
    1
    > File system explorer for the Vim. It enables you to visually browse complex directory hierarchies, quickly open files for reading or editing, and perform basic file system operations.

Super Searching

  • ctrlP
    1
    2
    3
    Plug 'kien/ctrlp.vim'

    Ctrl + P : enable the search

Using Vim for Python Development

  • Verifying VIM Install
    1
    2
    3
    4
    5
    Make sure have installed VIM > 7.3 with Python support.

    :python3 import sys; print(sys.version)
    3.8.10 (default, Sep 28 2021, 16:10:42)
    [GCC 9.3.0]

Using Vim for Go Development

  • vim-go
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    > Go development plugin for Vim. It has everything you need for Go development.

    :GoBuild - Compile package
    :GoInstall - Install it
    :GoTest - Test it
    :GoRun - Quickly execute current file
    :GoDef - Go to symbol/declaration
    :GoDoc - look up documentation
    :GoDocBrowser -

    # Auto Completion
    > It's really hard to code without auto-completion and not being able to going to the definition.
    > Vim-go use **gopls** for completion etc. Completion(Ctrl-x Ctrl-p) is enabled by default via omnifunc.

    # check if vim-go has been configured correctly for autocompletetion
    :verbose setlocal omnifunc?

    # autocomplete prompt to appear automatically whenever you press the dot(.)
    au filetype go inoremap <buffer> . .<C-x><C-o>

Vim and Rust

Reference