Mac Keyboard
Almost every Mac programmer I know is a vim heretic.
I wonder why…

Vim Escape Key Substitutes

Constantly reaching for the Esc key is very uncomfortable on a typical 101-key desktop PC keyboard. Having an Esc alternative vastly improves the Vim experience for most desktop users.

Any of the following key combinations will get you out of insert mode—no config tweak required:

  • Ctrl+[
  • Ctrl+c
  • Alt+[any motion key: h, j, k, l, y, p, ...]

Vim Clipboard Operations

Using The System Clipboard

If the version you are running was compiled with the +clipboard feature, you can have it use the system’s clipboard by adding set clipboard=unnamedplus to your ~/.vimrc file.

To check for this feature, check :echo has('clipboard') for a 1 result. If it returns zero, you will need to recompile with +clipboard enabled, or install vim-gtk from your distribution’s repositories and change it to become your default vim.

Copy/Paste Via Terminal

When running vim from a text console, vim may interpret some of the pasted input as command syntax. Autoindentation may also alter the pasted text. To prevent this from happening, put vim in paste mode with :set paste.

When you are finished with paste mode, you can leave it with :set nopaste.

In normal mode, y is copy and p is paste. You can paste while in insert mode with Ctrl+r followed by the register you want to paste from. The default register for copy/paste is the double-quote character: ".

If your terminal client supports xterm-style mouse reporting, you may use the following shortcuts to copy and paste:

Paste into PuTTY: Shift+Insert
Copy from PuTTY while in visual mode: Shift+<mouse-highlight>

Editing HTML in Vim

Making the % Motion Work with HTML Tags
  1. Add the line runtime macros/matchit.vim to your ~/.vimrc file.
  2. Restart vim.
  3. Put your cursor on the tag text (i.e. <html>, then press % to jump to the matching tag.
Plugin-Free % Substitute

In normal mode, place the cursor on the starting tag, then type vat. v enters visual mode, at selects surrounding tags inclusive. The cursor will jump to the closing tag.

Reformat and Auto-Indent HTML
  1. Make sure that the filetype is set to HTML:
    :set ft=html
  2. Set the indent spacing:
    :set shiftwidth=<indent size>
  3. Use gg=G to reformat the document. (gg moves the cursor to the top, = is the reformatting function, and the motion G applies it from the cursor to the end of the file.

Unlock Vim from Accidental Ctrl+s

If you accidentally lock your terminal by pressing Ctrl+s (GUI Save) instead of :w (vim Save), you can unlock the terminal by pressing Ctrl+q.

A way to avoid this problem entirely is to disable XON/XOFF flow control in your terminal session by running stty -ixon from the command line.

Whitespace Conversion

Show whitespace characters: :set list
Convert tabs to spaces: :set expandtab
Convert spaces to tabs: :set noexpandtab

Use Vi Key Bindings in Bash

To use Vi key bindings in your Bash shell, type set -o vi at the shell prompt. Each new prompt will have vi bindings and will start in insert mode.

To persist this setting, add it to the end of your ~/.bashrc file.

Working with make and Makefiles

Build target: :make <target>
Go to error n: :cc!n
Go to next error: :cn
Go to previous error: :cp
List all errors: :clist

Favorite Vim Plugins

Pathogen Easier installation and management of plugins and runtime files.
VimRepress Manage and edit your WordPress blog directly through Vim.
Multiple Cursors
Sublime Text style multi-cursor, multi-select feature for Vim.
Vim-style substitute for multi-cursor editing.
(uses macros instead of a plugin)
Tabular Align text by an internal delimiter.
Useful for pretty-formatting JSON, CSS, arrays, etc.

Best Vim Cheat Sheet

Jon Beltran de Heredia, author of ViEmu, Vi/Vim Emulation for Visual Studio, Xcode, Word, Outlook, and Microsoft SQL Server, made a superb cheat sheet for the most common commands.

Click the image below to download an optimized PDF of his fine work:

← Older Newer →

Leave a Reply

You must be logged in to post a comment.