DEV Community

Cover image for Supercharge Your Vim
Connor Dillon
Connor Dillon

Posted on

Supercharge Your Vim

Supercharge Your Vim

Basic Setup

The following changes should be made to your .vimrc file:

# Enter the current millenium (Force Vim to NOT behave like Vi)
set nocompatible

# Enable syntax and plugins (for `netrw`)
syntax enable
filetype plugin on
Enter fullscreen mode Exit fullscreen mode

Finding Files

The following changes should be made to your .vimrc file:

# Search down into subfolders
# Provides tab-completion for all file-related tasks
set path+=**

# Display all mathicng files when we tab complete, navigable with Tab and Shift+Tab
set wildmenu
Enter fullscreen mode Exit fullscreen mode

Sanity check: view Vim's path

:set path?
Enter fullscreen mode Exit fullscreen mode

Usage:

  • Type :find and use Tab & Shift+Tab to search by partial match
  • Use * to make it a fuzzy Search

Things to consider:

  • :ls will print out all open Vim buffers
  • :b lets you autocomplete any open buffer (:b is short for buffer)

Tag Jumping

# Create the `tags` file (may need to install `ctags` first)
command! MakeTags !ctags -R .
# `!` tells Vim to run this as a shell command, i.e. pretend we type it into the console
# Alternatively, execute `ctags -R .` in your project root directory (back in the console, not Vim!), and check out the new `tags` file
set tags? # View `tags` path
Enter fullscreen mode Exit fullscreen mode

ctags often comes pre-installed with most Linux distros. For OSX, ctags can be installed via brew install ctags

Usage:

  • Use ^] to jump to tag under cursor, (^ = Ctrl, not Caret)
  • Use g^] for ambiguous tags, (^ = Ctrl, not Caret)
  • Use ^t to jump back up the tag stack, (^ = Ctrl, not Caret)

Things to consider:

  • This doesn't help if you want a visual list of tags
  • Using this can cause you to jump around files, use :echo expand("%") within Vim to print working directory (i.e. pwd).

Autocomplete

Now we're getting to the good stuff. All of this is documented in :help ins-completion.

Autocomplete comes pre-configured with Vim. It automatically reads from your tags file (if one exists!). Even if you don't have a tags file, it will still work within the file that you're working in, as well as language-specific configurations that allows it to follow language-specific dependency chains.

For example, Vim's built-in autocomplete will recursively search through any files/modules/etc that you've set as requireed dependencies for your current file.

Highlights:

  • ^x^n for JUST this file
  • ^x^f for filenames only (works with our path trick!)
  • ^x^]for tags only
  • ^n for anything specified by the "complete" option
  • Please see :help ins-completion for further

Now we can:

  • Use ^n and ^p to go back and forth in the suggestion list.

File Browsing

Tweaks for file browsing

let g:netrw_banner=0            # disable annoying banner
let g:netrw_browse_split=4      # open in prior window
let g:netrw_altv=1              # open splits to the right
let g:netrw_liststyle=3         # tree view
let g:netrw_list_hide=netrw_gitignore#Hide()
let g:netrw_list_hide.=',\(^\|\s\s\)\zs\.\S\+'
Enter fullscreen mode Exit fullscreen mode

Usage:

  • :edit a folder to open a file browser
    • :edit . for current working directory
  • <CR>/v/t to open in an h-split/v-split/tab
  • Check out :help netrw-browse-maps for more mappings

Snippets

As with any snippet manager, effective use of snippets in Vim can save you tons of time. Snippets are natively supported in Vim, and can be easily created by remapping a relevant keyword that will read from a skeleton file and auto-complete it at your cursor.

# Read an empty HTML template and move cursor to title
nnoremap ,html :-1read $HOME/.vim/.skeleton.html<CR>3jwf>a
Enter fullscreen mode Exit fullscreen mode
  • nnoremap remaps for normal mode (usually nremap), and nnoremap ensures that there are no collisions with default Vim commands, if any exist (i.e. overrides pre-existing commands)
    • Please see :help map-modes for documentation on Vim's key mapping modes
  • ,html is the mapping that we're assigning
  • : tells Vim to enter command mode
  • -1read invokes the read command into the current Vim buffer, with -1 tweaking cursor placement (for quality of life)
  • $HOME/.vim/.skeleton.html is the file that we're reading from (in this example)
  • <CR> sends the command automatically, so that we don't have to hit enter every time
  • 3jwf>a Moves the cursor to a specific location (This will be unique to each snippet, and is not necessary)

Build Integration

Pro tip: Steal Mr. Bradley's formatter & add it to our spec_helper

Configure the make command to run RSpec:

set makeprg=bundle\ exec\ rspec\ -f\ QuickfixFormatter
Enter fullscreen mode Exit fullscreen mode

Usage:

  • Run :make to run RSpec
  • :cl to list errors
  • :cc# to jump to error by number (#)
  • :cn and :cp to navigate forward and back through test failures

Registers

  • "add to delete into the a register
  • "by to yank into the b register
  • "cp to paste from the c register
  • :reg to display all non-empty registers
    • :reg a b c to filter and display only the a, b, and c registers.
  • "+ to interface with the system clipboard as a register

Registers can be accessed via Ctrl + r + registerName, such as Ctrl + r + f to paste the f register into the current buffer.

Numbered Registers

Numbered registers are handled automatically by Vim, and allow us to access the last 10 deleted/yanked texts. The numbered registers will roll over, such that "0 contains the content of the most recent delete/yank, and "9 contains the content of the oldest delete/yank.

Fun Vim Toys

  • ⌘ + / to immediately find that pesky, runaway cursor of yours.
  • :set showcmd to display current chain of command keystrokes in lower-right of Vim - great for those of us with short attention spans!
  • :help key-notation opens documentation for Vim's keyboard shortcuts - a huge life-saver!
  • :helpgrep SEARCH_TERM to use grep to search through all of Vim's help documentation for your search term
  • Look up any shortcut or command in any mode with prefixes:
    • :help i_^n to look up what Ctrl+N does in insert mode
    • :help c_^n to look up what Ctrl+N does in command mode
    • :help v_^n to look up what Ctrl+N does in visual mode

Top comments (0)