Skip to content

Instantly share code, notes, and snippets.

@dwmoore
Last active August 29, 2015 14:17
Show Gist options
  • Save dwmoore/9552050cfb1d9fec2893 to your computer and use it in GitHub Desktop.
Save dwmoore/9552050cfb1d9fec2893 to your computer and use it in GitHub Desktop.
Vim CheatSheet

#Vim CheatSheet

First, a note on the conventions used in this cheatsheet: Vim makes great use of the key modifiers shift and ctrl. I have noted any combined keypress as <command> key. In the case of shift, I have also included the normally recognized output: <shift> a (A) This is for clarity only and not part of the actual command.

###Moving Around First up, try to stay in normal mode as much as possible and edit with objects (discussed below) rather than going into insert mode and using arrow keys. Also, don't hold the h, j, k, l keys to move large distances,

h, j, k, l - Normal Mode - Move the cursor left, down, up, and right.

gg - Normal Mode - Go to the top of the document.

<shift> g (G) - Normal Mode - Go to the bottom of the document.

<ctrl> f - Normal Mode - Move forward one page.

<ctrl> b - Normal Mode - Move back one page.

<ctrl> d - Normal Mode - Move down a half page.

<ctrl> u - Normal Mode - Move up a half page.

?some_word <enter> - Normal Mode - Find the closest previous occurence of some_word.

/some_word <enter> - Normal Mode - Find the closest next occurence of some_word.

<shift> 8 (*) - Normal Mode - Find the next occurrence of the word under the cursor.

<shift> 3 (#) - Normal Mode - Find the previous occurrence of the word under the cursor.


###Commands

<shift> d (D) - Normal Mode - Delete to end of line

If you need to insert characters at a specific point across multiple lines in a column, such as making a multi line comment in Ruby, here is a sequence to do just that:

<ctrl> v - Visual Block Mode - Enters visual block mode to allow block selection. Then with your block selected...

<shift> i (I) - Visual Block Insert Mode - Enters block insert mode. Screen output will only appear on one line but will be cloned across all lines selected upon exiting insert mode. Allow up to 3 seconds for this command to process before issuing additional commands.


###Text Objects Text objects are used in combination with commands (as well as numbers) to form powerful "Editing Command" operations with minimal keystrokes. A text object will always begin with either `a` or `i` which specifies to either modify around the object and include the surrounding whitespace or inside the text object and leave the surrounding white space in place.

aw - Normal Mode - Select the word under the cursor including leading and trailing whitespace.

iw - Normal Mode - Select the word under the cursor, leaving surrounding whitespace in tact.

as - Normal Mode - Select the sentence under the cursor including leading and trailing whitespace.

is - Normal Mode - Select the sentence under the cursor, leaving surrounding whitespace in tact.

ap - Normal Mode - Select the paragraph under the cursor including leading and trailing whitespace.

ip - Normal Mode - Select the paragraph under the cursor, leaving surrounding whitespace in tact.


#####Editing Commands Again, be aware you can use any command (change, delete, yank) with any object.

ciw - Normal Mode - Change inside the word under the cursor.

yap - Normal Mode - Yank around the paragraph under the cursor.

dis - Normal Mode - Delete inside the sentence under the cursor.


#####More Objects These all are meant to be utilized in the same format as above, just add your command and modifier.

', ", ], }, ) - Change in or around common programming punctuation.

t - Change in or around a full opening and closing set of HTML tags.

> - Change in or around a single opening or closing HTML tag.


##Vim Plugins

###Rails.Vim

#####Navigation

<leader> gf - Normal Mode - Go to corresponding file in Rails directory structure under cursor

:A - Normal Mode - Go to alternate file

:R - Normal Mode - Go to related file

:Eview, :Emodel, :Econtroller - Normal Mode - Edit the corresponding model under cursor

:help rails-navigation - Normal Mode - Help on navigation commands


#####Rake

:Rake - Normal Mode - Run the current test, spec, or feature.

:.Rake - Normal Mode - Run the current method, example, or scenario on the current line.

:help rails-rake


#####Scripts

:Rails console - Normal Mode - Alias for rails console.

:Rgenerate model, controller, migration - Normal Mode - Alias for rails g.

:help rails-scripts


####Rextract

:Rextract - Normal Mode (based on Visual Mode selection) - Replaces selection with render call and places selection in new partial.

:help rails-:Rextract


###Thanks and keep on Viming!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment