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
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
Sanity check: view Vim's
path
:set path?
Usage:
- Type
:findand use Tab & Shift+Tab to search by partial match - Use
*to make it a fuzzy Search
Things to consider:
-
:lswill print out all open Vim buffers -
:blets you autocomplete any open buffer (:bis 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
ctagsoften comes pre-installed with most Linux distros. For OSX,ctagscan be installed viabrew 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^nfor JUST this file -
^x^ffor filenames only (works with our path trick!) -
^x^]for tags only -
^nfor anything specified by the "complete" option - Please see
:help ins-completionfor further
Now we can:
- Use
^nand^pto 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\+'
Usage:
-
:edita folder to open a file browser-
:edit .for current working directory
-
-
<CR>/v/tto open in an h-split/v-split/tab - Check out
:help netrw-browse-mapsfor 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
-
nnoremapremaps for normal mode (usuallynremap), andnnoremapensures that there are no collisions with default Vim commands, if any exist (i.e. overrides pre-existing commands)- Please see
:help map-modesfor documentation on Vim's key mapping modes
- Please see
-
,htmlis the mapping that we're assigning -
:tells Vim to enter command mode -
-1readinvokes thereadcommand into the current Vim buffer, with -1 tweaking cursor placement (for quality of life) -
$HOME/.vim/.skeleton.htmlis 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>aMoves 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
Usage:
- Run
:maketo run RSpec -
:clto list errors -
:cc#to jump to error by number (#) -
:cnand:cpto navigate forward and back through test failures
Registers
-
"addto delete into thearegister -
"byto yank into thebregister -
"cpto paste from thecregister -
:regto display all non-empty registers-
:reg a b cto filter and display only thea,b, andcregisters.
-
-
"+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 showcmdto display current chain of command keystrokes in lower-right of Vim - great for those of us with short attention spans! -
:help key-notationopens documentation for Vim's keyboard shortcuts - a huge life-saver! -
:helpgrep SEARCH_TERMto usegrepto 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_^nto look up what Ctrl+N does in insert mode -
:help c_^nto look up what Ctrl+N does in command mode -
:help v_^nto look up what Ctrl+N does in visual mode
-
Top comments (0)