Skip to content

Instantly share code, notes, and snippets.

@adamk33n3r
Last active February 8, 2016 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamk33n3r/ae76889c92f5097bb63f to your computer and use it in GitHub Desktop.
Save adamk33n3r/ae76889c92f5097bb63f to your computer and use it in GitHub Desktop.
Vim Tips and Tricks

Adam Keenan's Vim tips and tricks cheatsheet

Commands

Navigation

Learn these. It's tricky at first but it improves productivity by a lot.

  • j: up
  • k: down
  • h: left
  • l: right
  • gg: top of file
  • G: bottom of file
  • w: next word
  • e: end of word
  • b: back word
  • n: next search
  • N: prev search

Modifiers

  • t: up to
  • T: back to

Editing

  • i: insert mode (at current character)
  • I: insert mode (at beginning of line)
  • a: insert mode (1 after current character)
  • A: insert mode (at end of line)
  • o: insert mode (new line under)
  • O: insert mode (new line above)
  • u: undo
  • ctrl-r: redo

Selection

  • v: select mode
  • V: line select mode
  • ctrl-v: block select mode
  • y: yank selection (copy)
  • Y: yank line

Deleting

  • x: delete
  • d: delete (prefix)
    • d: delete and copy current line
    • $: delete and copy do end of line

Recommended configuration options

set nocompatible            " Use Vim settings rather than Vi settings. Should have this first because it changes a lot of options
set mouse=a                 " It is OK to use the mouse.....but try not to.
set bg=dark                 " Set to having dark bg (if you have a dark bg)
set t_Co=256                " Force 256 colors
colorscheme xoria256        " My favorite as of now
set shell=/bin/bash         " If using a non sh-compatible shell. Like fish

filetype plugin indent on   " Set file specific settings
set ai                      " Sets autoindent on
set tabstop=4               " Sets how many columns a tab is
set softtabstop=4           " Sets how many columns to insert when pusing tab
set shiftwidth=4            " Sets how many columns to indent with << and >> and cindent
set expandtab               " Makes spaces when pusing tab

set nu                      " Shows numbers in left margin
set hls                     " Highlights all matching words when searching
set incsearch               " Search as you type
set ruler                   " Always show cursor position
set showcmd                 " Show command info in statusline like visual selection ranges
set laststatus=2            " Always show statusline
set noesckeys               " Remove delay when pushing escape in insert mode although makes things like the delete key not work
set scrolloff=1             " Always show one line in advance when scrolling
set splitright              " When vsplitting, open new pane to right
set splitbelow              " When hsplitting, open new pane below

set autochdir               " Automatically changes working dir to dir of file when opened
set undofile                " Enables undo file which stores all history for every file
set undodir=~/.vim/undo     " Place to store undofiles
set directory=~/.vim/swap   " Place to store swapfiles
set backupdir=~/.vim/backup " Place to store backupfiles

set wildmenu                " Command line completion
set wildmode=full           " Shows full list to tab through

set iskeyword-=_            " Removes _ from being a keyword so commands like cw treat it as end of word
" Turns on spell check. Use z=
set invspell spelllang=en_us

" Keybindings
map <ScrollWheelUp> <C-Y>   " Sets scrolling to do one line at a time
map <ScrollWheelDown> <C-E> " May only be useful on trackpads
nnoremap "" :w<CR>          " Sets `""` in normal mode to save file (you could change to your own shortcut)
nnoremap ^_ I// <esc>j      " Sets both ctrl-_ and ctrl-/ to put two slashes at beginning of line for commenting and goes to next line. Note that the ^ is an escape character not an actual ^. To insert this do ctrl-v ctrl-/. 

" Jump back to last known cursor position when opening a file again
autocmd BufReadPost *
    \ if line("'\"") > 1 && line("'\"") <= line("$") |
    \   exe "normal! g`\"" |
    \ endif

If you haven't noticed by now many configuration options can be shortcut with using only part of the word or using letters of it like set number;set nu or colorscheme xoria;colo xoria or even set expandtab;set et.

You can also prefix options with no to turn them off like set nopaste and appending a ? after an option will display what it is set to

Recommended plugins - I use pathogen to load

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment