Skip to content

Instantly share code, notes, and snippets.

@esayler
Last active September 17, 2015 21:12
Show Gist options
  • Save esayler/35f86431e509720ac8b2 to your computer and use it in GitHub Desktop.
Save esayler/35f86431e509720ac8b2 to your computer and use it in GitHub Desktop.

vim notes

Note on notation

<C-p> = press the <Ctrl> and p keys at the same time dw = press the d key, then immediately the w key

etc

'tabstop' -- number of columns to expand a 'tab' character to onscreen

differences from vanilla vim

<C-a> and <C-x> for performing addition and subtraction on numbers (<C-a> is my tmux prefix)

NORMAL mode

cmd action
. repeat last command
dd delete line
>G inc indentation from current line until EOF
A insert at end of line
I insert at beginning of line
C change from cursor to end of line
S change entire line
o open line below
O open line above
s delete char under cursor, enter insert mode
f{char} goto next occurrence of {char} in line
; repeat char search forward

repeatable actions and how to reverse them

cmd action repeat undo
make a change {edit} . u
f{char}/t{char} scan line for next char ; ,
F{char}/T{char} scan line for prev char ; ,
/pattern<CR> scan document for next match n N
?pattern<CR> scan document for prev match n N
:s/target/replacement perform substitution & u
qx{changes}q execute a sequence of changes @x u
cmd action
<C-a> increment by one or provided count (change this)
<C-x> decrement by one or provided count
c change
d delete
y yank into register
g~ swap case
gu make lowercase
gU make lowercase
> shift right
< Shift left
= auto indent
! filter {motion} lines through an external program
ga give numeric code for character under cursor

INSERT Mode (i)

INSERT mode cmd effect/action
<Esc>, <C-[> switch to NORMAL mode
<C-o> Switch to Insert Normal mode
<C-h> delete back one char (backspace) [also works in ex cmd-line]
<C-w> Delete back one word [also works in vim command line/bash]
<C-u> Delete back to start of line [also works in vim cmd-line]
<C-r>= enter the expression register (can use to paste register)
<C-v>{123} insert unusual character using 3 digit decimal code
<C-v>u{ffff} insert unicode character using 4 digit hexadecimal code
<C-v>u{non-digit} insert non-digit literally
<C-k>{char1}{char2} insert character represented by digraph

REPLACE mode

cmd action
gR enter virtual replace mode
r{char}, gr{char} replace single character

VISUAL mode

cmd action
<C-g> toggle between VISUAL and SELECT modes
v character-wise visual mode
V line-wise visual mode
<C-v> block-wise visual mode
gv reselect the last visual selection
o toogle free end of highlighted text

Command-Line mode

cmd action
<C-g> toggle between VISUAL and SELECT modes
@: repeat Ex last command from NORMAL MODE, @@ again
q/ command-line window (search history) from NORMAL MODE
q: command-line window (Ex cmd history) from NORMAL MODE
<C-c> exit command-line window
<C-f> switch from cmd-line to cmd-line window (saves current cmd)

Ex Commands that operate on buffer text

Ex cmd operation
:[range]delete [x] delete lines into register x
:[range]yank [x] yank lines into register x
:[line]put [x] put text from register x after line
:[range]copy {address} copy lines ([range]) to below line {address}
:[range]move {address} move lines ([range] to below line {address}
:[range]join {address} join lines
:[range]normal {commands} execute normal mode {commands} on each line
:[range]s/{pattern}/{string}/[flags] replace occurrences of {pattern} with {string} on each specified line ([range])
:[range]g/{pattern}/[cmd] Execute Ex cmd on all lines in [range] where {pattern} matches
command tips

:%s/target/replacement :{start},{end}

Ex-cmd pattern definition
1 first line
0 virtual line above first line (use if moving or copying lines to top of file)
. current line (where cursor is placed)
$ last line
% entire file
'm line containing mark m
'< start of visual selection
'> end of visual selection
:copy, :co, :t copy to
:move, :m "cut"/"paste"
:normal{NORMAL mode cmd} use with . for repeating commands or @q for more complex tasks
key-map action
<C-r><C-w> copy current word into Ex command
<C-r><C-a> copy current WORD into Ex command
<C-p> previous history
<C-n> next history

Tips

cmd action
[INSERT MODE] <C-o>zz center page on current line while typing
<C-o> jump list

Shell commands

tip: use <C-z> to suspend vim, then jobs to list ps, and fg to reenter vim

([range] == {start},{end}) :!{shell-cmd}

cmd action
% refers to current file name
:shell enter shell session (exit to return to vim)
:!{cmd} execute {cmd} with the shell
:read !{cmd} execute {cmd} w/ shell and insert resulting standard output below curso
:[range]write !{shell-cmd} execute {cmd} in the shell with [range] lines as standard input
:[range]!{shell-cmd aka filter} lines specified by [range] passed to standard input for {cmd}, output of {cmd} overwrites the original contents of range
!{motion} drops you into command-line mode with pre-populated range indicated by {motion

Files

Manage Multiple Files

Buffer List

:write, :update, :saveas -- all cmds that act on files (stored on disk)

key-map action
<C-^> toggle between current and alternate files
buffer list -- :ls
symbol meaning
% current window
# alternate file
+ unsaved changes
a active
h hidden (vim will remind of unsaved changes when quitting)
Ex cmds
Ex cmd action
:b {bufname} switch to buffer by filename
:b N switch to buffer by number (N)
:bufdo run Ex cmd in all buffers listed by :ls
:argdo
:bdelete N1 N2 N3 delete a buffers N1, N2, and N3
:N,M bdelete delete buffers N through M
:wa write all buffers
:qa quit all buffers
:qa! force quit all buffers
tpope's unimpaired remaps
key-map ex-cmd action
]b :bnext, :bn next buffer
[b :bprev, :bp prev buffer
[B :bfirst first buffer
]B :blast last buffer
]a, [a next/prev argument
]q, [q next/prev quickfix
]l, [l next/prev location
]t, [t next/prev tag
argument list - group buffers into a collection

buffer list like a messy desktop...argument list like a separate tidy workspace

:args -- show argument list (initially is the list of files passed as an arg when opening vim)

* -- match 0 or more characters, limited to current directory (:h wildcard)

** -- match 0 or more characters...recurse downward into subdirectories (:h starstar-wildcard)

Ex cmd action
:args cat .some file :h backtick-expansion
:args {arglist} clear the argument list and repopulate it
:next next file in argument list
:prev prev file in argument list
:argdo excute same command on each buffer in set
split windows
key-map action
<C-w>s horizontal split
<C-w>v vertical split
:sp {filename} horizontal split with {filename}
:vsp {filename} vertical split with {filename}
<C-w>w cycle between open windows (also <C-w><C-w>)
<C-w>{h/j/k/l} move focus left, down, up, right (can also hold ctrl and type wh, wj, wk, wl)
<C-w>c :cl[ose], :q
<C-w>o :on[ly]
<C-w>= equalize width/height of all windows
<C-w>_ maximize height of active window
<C-w> maximize width of the active window
[N]<C-w>_ set active window height to [N] rows
[N]` `
<C-w>+ increase height by 1 line
<C-w>- decrease height by 1 line
<C-w>r rotate all windows
<C-w>x xchange current window with neighbor
<C-w>H move current window to far left
<C-w>J move current window to bottom
<C-w>K move current window to top
<C-w>L move current window to far right
tab pages -- container that holds collection of window splits
NORMAL key-map ex-cmd action
gt, SHIFT-l :tabn[next] go to next tab page
gT, SHIFT-r :tabp[revious] go to prev tab page
{N}gt :tabn[ext] {N} go to Nth tab
<C-w>T move current window into a new tab page
:tabmove [N] rearrange tab page (if N=0, then goto 1st; if [N] omitted, then goto end)
:lcd {path} set working directory locally for current window
:windo lcd {path} set working directory for all windows
:tabe[dit] [{filename}] open new tab page (filename optional)
:close closes active tab page (single window)
:tabc[lose] close current tab page & all windows (single/many windows)
:tabo[nly] keep active tab page only, close all others
Open files and save them to disk

:edit :netrw :write

%% -- expand to path of active buffer -- (cnoremap %% ..)

:find

:set path+=app/** starstar wildcard matches all subdirectories beneath app/ Note: rails.vim auto-sets the path to include all directories in a conventional rails project!

Plugin Specific

Ruby specific

Precision motions provided by vim-ruby
cmd action
]m go to start of next method definition
]M go to end of next (or current) method definition
[m go to start of previous method definition
[M go to end of previous method definition
]] goto start of next module or class definition
[[ goto start of previous module or class definition
% jump between matchit keyword pairs (matchit)
Text objects for working with Ruby

:help ruby-text-objects

cmd action
ir inside the current ruby block (textobj-rubyblock)
ar around the current ruby block (textobj-rubyblock)
im inside the current method definition (vim-ruby)
am around the current method definition (vim-ruby)
iM inside the current class or module (vim-ruby)
aM around the current class or module (vim-ruby)
jump to file names

when your path option is properly configured (as automatically done by vim-ruby), you can use these commands to navigate your project

cmd action
gf jumpt to the filename under the cursor
:find {file} jump to the specified {file} in path
jump to definitions (using ctags)
cmd action
<C-]> jump to the first tag that matches the word under the cursor
:tag {keyword} jump to the first tag that matches {keyword}
g<C-]> multiple matches gives prompt, single match jumps to it
:tselect {keyword} multiple matches gives prompt, single match, jumps to it
Tab completion at vim's command-line
cmd effect
<Tab> expand commandline to use next match
<S-Tab> expand cmdline to use previous match
<C-d> show a list of all possible matches
rails.vim -- also provides commands:

:Rcontroller :Rmodel :Rview

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