Skip to content

Instantly share code, notes, and snippets.

@jasoncodes
Last active December 21, 2015 00:08
Show Gist options
  • Save jasoncodes/6217581 to your computer and use it in GitHub Desktop.
Save jasoncodes/6217581 to your computer and use it in GitHub Desktop.
Vim Tips

Vim Tips

fresh

Build your dotfiles (shell, Vim config) from multiple sources with fresh.

Both of our dotfiles are built using fresh. We source Vim and other config from each other:

An example freshrc entry for sourcing vim config from another repo:

fresh jasoncodes/dotfiles vim/mappings/pasteboard.vim --file=~/.vimrc

vim

Buffer management

CtrlP is great for opening files in a project using fuzzy searching.

  • :CtrlP lists all files in the project
  • :CtrlPBuffer lists all open buffers
  • :CtrlPCurFile lists files in the same directory as the current buffer

CtrlP Modified by Jason gives you a few extra handy modes:

  • :CtrlPModified lists all modified files in your git working tree
  • :CtrlPBranch lists all files which have changed in the current branch

fresh jasoncodes/dotfiles vim/plugins/ctrlp.vim --file=~/.vimrc

Saving modified buffers and reloading externally modified files

map <Leader>r :checktime<CR>:wall<CR>:redraw!<CR>

Automatically creating directories for new files

fresh jasoncodes/dotfiles vim/config/auto_mkdir.vim --file=~/.vimrc

Using shell commands

Pipe selection or buffer through external tools. :!sort, :!cat -n.

Read output of external command into current buffer. :read !ls

Moving or copying lines by number

Copy range to current line: :42,50t. Move a line below current line: :42m.

Navigating by word

w: move to beginning of next word b: move to previous start of word e: move to next end of word

These respect inter-word punctuation such as -, /, etc. Hold shift when using these to ignore these and move based on whitespace tokenisation

Move to matching token

% moves between (), [], and {} pairs. With runtime macros/matchit.vim, you can also jump between things like do/end in Ruby.

Jump list

When jumping to another position (that is, not using j/k) by searching, tags, etc., a marker is inserted into the jump list. You can go back to the previous jump marker by pressing Ctrl-O. Ctrl-I will navigate forward in the jump list.

Bubbling text

With a plugin you can move the current line or selection up and down in a buffer. This is really handy when rearranging commits in a git rebase --interactive.

fresh jasoncodes/dotfiles vim/config/bubbling.vim --file=~/.vimrc

Toggling lines in a diff

When editing a patch in git add --patch, this plugin makes it easy to discard a range of lines.

https://github.com/twe4ked/vim-diff-toggle

Registers

:reg will show the value in all registers. Use :help registers to get a definition of each.

To insert the content of a register in either insert mode or command mode, you can press Ctrl-R followed by the register's character.

Pairing with Vim (tmux and wemux)

Command-line Vim (the only way to fly :)) plus tmux makes for an awesome pairing tool.

A set of helper scripts exist to assist in setup. On the host machine:

# install wemux
brew install wemux
# install helper scripts using fresh
fresh jasoncodes/dotfiles bin/adduser --bin
fresh jasoncodes/dotfiles bin/wemux/\* --bin
# setup pairing environment
_wemux_setup
# give permission to pairing users
wemux-add-user $OTHER_USER_GITHUB_USERNAME 
# start a pairing session
wemux

On the client machine:

ssh wemux@$HOST_MACHINE

Filtering lines in a selection/buffer

:g/foo/d deletes every line matching /foo/ :v/bar/d deletes every line NOT matching /bar/

Diffing

Open a vertical split with two files and run :diffthis on both sides.

You can diff ranges in a file (rather than whole files) with Linediff

Different iTerm cursor in insert mode

fresh jasoncodes/dotfiles/ vim/config/cursor.vim --file=~/.vimrc

Persistent Undo

Vim can remember undo history across sessions.

fresh jasoncodes/dotfiles vim/config/undo.vim --file=~/.vimrc

Can be really handy when combined with Gundo.

Searching

Ack is great for searching files by content.

The following file has a bunch of handy mappings for searching within a buffer:

fresh jasoncodes/dotfiles vim/config/search.vim --file=~/.vimrc

Tags

Tags are great for navigating between files. Use Ctrl-] to jump to a tag match.

Install ctags with Homebrew:

brew install ctags

Enable automatic tag generation in Vim:

fresh jasoncodes/dotfiles vim/config/tags.vim --file=~/.vimrc

CtrlP can disambiguate tags with the ctrlp-tjump plugin.

Miscellaneous handy plugins

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