Nov 20, 2012

Practical Vim: Edit Text at the Speed of Thought - Drew Neil

Editing

. - repeats the last change
u - undo
<C-r> - redo
x - deletes character under the cursor d{motion} - delete dl dd - delete current line db - deletes from the cursor’s starting position to the beginning of the word dw
diw
daw
dap
df{char}
d/{expression}

c{motion} - change

y{motion} - yank into register
A - append to the and of line
A = $a
C = c$
s = cl
S = ^C
I = ^i
o = A<CR>
O = ko

ea - append at the end of the current word
xp - transpose characters
ddp - transpose lines
yyp - duplicate line
g~{motion} - swap case
gu{motion} - to lower case
gU{motion} - to upper case
gUaw

J - join two lines

> - shift left
< - shift right
= - autoindent
gg=G - autoindent from start of file 
>G - increases the indentation from the current line until the end of the file

Movement

j | Down one real line gj | Down one display line k | Up one real line gk | Up one display line 0 | To first character of real line g0 | To first character of display line ^ | To first nonblank character of real line g^ | To first nonblank character of display line $ | To end of real line g$ | To end of display line --------------------------------------- w | Forward to start of next word b | Backward to start of current/previous word e | Forward to end of current/next word ge | Backward to end of previous word gg - to the start of file {line}G - go to line

Jump

:jumps <C-o> - to prev edition place <C-i> - to next edition place [count]G | Jump to line number //pattern <CR> /?pattern <CR> / n / N | Jump to next/previous occurrence of pattern % | Jump to matching parenthesis (/) | Jump to start of previous/next sentence {/} | Jump to start of previous/next paragraph H/M/L | Jump to top/middle/bottom of screen gf | Jump to file name under the cursor <C-]> | Jump to definition of keyword under the cursor ’{mark} / `{mark} | Jump to a mark

Changes

:changes g; - traverse backward through the change list g; - traverse forward through the change list see also `. and `^

Selection

a) or ab | A pair of (parentheses) i) or ib | Inside of (parentheses) a} or aB | A pair of {braces} i} or iB | Inside of {braces} a] | A pair of [brackets] i] | Inside of [brackets] a> | A pair of <angle brackets> i> | Inside of <angle brackets> a’ | A pair of 'single quotes' i’ | Inside of 'single quotes' a" | A pair of "double quotes" i" | Inside of "double quotes" a` | A pair of `backticks` i` | Inside of `backticks` at | A pair of <xml>tags</xml> it | Inside of <xml>tags</xml> ---------------------------------------------------- iw | Current word aw | Current word plus one space iW | Current WORD aW | Current WORD plus one space is | Current sentence as | Current sentence plus one space ip | Current paragraph ap | Current paragraph plus one blank line

Registers

P - paste the contents of our unnamed register in front of the cursor gp gP - leave the cursor positioned at the end of the pasted text <C-r>{register} - paste from register in Insert mode "{register} :reg "0 - content of register "" - unnamed "0 - yank "_ - black hole "+ - system "* - selection "= - expression "a-"z - named Register | Contents ----------------------------------- "% | Name of the current file "# | Name of the alternate file ". | Last inserted text ": | Last Ex command "/ | Last search pattern

Search

f{char} | Forward to the next occurrence of {char} F{char} | Backward to the previous occurrence of {char} t{char} | Forward to the character before the next occurrence of {char} T{char} | Backward to the character after the previous occurrence of {char} ; | Repeat the last character-search command , | Reverse the last character-search command

Marks

m{a-zA-Z} ’{mark} Keystrokes | Buffer Contents -------------------------------------------------------------- `` | Position before the last jump within current file `. | Location of last change `^ | Location of last insertion `[ | Start of last change or yank `] | End of last change or yank `< | Start of last visual selection `> | End of last visual selection % - jump between opening and closing sets of parentheses

Insert mode

i - activate <C-h> - delete back one char <C-w> - delete back one word <C-h> - delete back to start of line <C-r>{register} - paste from register <C-a> - perform addition on numbers <C-x> - perform subtraction on numbers <C-r>= - open calculator {number}<C-a> set nrformats= this will cause vim to treat all numerals as decimal, regardless of whether they are padded with zeros.

Insert unusial character

<C-v>{123} - Insert character by decimal code <C-v>u{1234} - Insert character by hexadecimal code <C-v>{nondigit} - Insert nondigit literally <C-k>{char1}{char2} - Insert character represented by {char1}{char2} digraph

Insert normal mode

<C-o> - enable

Replace mode

R - activate

Virtual Replace mode (tab as spaces)

gR - activate

Visual modes

gv - Reselect the last visual selection
o - Go to other end of highlighted text
vit - select inside tag
U - to uppercase

Character-wise Visual mode

v

Line-wise Visual mode

V

Block-wise Visual mode

<C-v>

Command-Line mode

:[range]delete [x] | | Delete specified lines [into register x] :[range]yank [x] | | Yank specified lines [into register x] :[line]put [x] | | Put the text from register x after the specified line :[range]copy {address} | :t | Copy the specified lines to below the line specified by {address} :[range]move {address} | :m | Move the specified lines to below the line specified by {address} :[range]join | | Join the specified lines :[range]normal {commands} | | Execute Normal mode {commands} on each specified line :[range]substitute/{pattern}/{string}/[flags] | | Replace occurrences of {pattern} with {string} on each specified line :[range]global/{pattern}/[cmd] | | Execute the Ex command [cmd] on all specified lines where the {pattern} matches @: - repeat last ex command :registers <C-r><C-w> - copies the word under the cursor and inserts it at the command-line prompt <C-w> - delete backward to the start of the previous word <C-u> - delete backward to the start of the line

Examples

:1 :print :$ :p :3p :2,5p :.,$p % - all lines of file :%p :%s/Practical/Pragmatic/ :'<,'>p - visual selection :/<html>/,/<\/html>/p - range of Lines by Patterns :{address}+n - Modify an Address Using an Offset :/<html>/+1,/<\/html>/-1p :.,.+3p :%normal A; - adds semicolons to all file lines ends :%normal i// - comments all lines :col<C-d> - autosuggestions set wildmode=longest,list - like bash # like zsh: set wildmenu set wildmode=full :%s//<C-r><C-w>/g - copy word under cursor to substitute command :write | !ruby % - combine two commands with | sign

Ranges

Symbol | Address -------|----------------------- 1 | First line of the file $ | Last line of the file 0 | Virtual line above first line of the file . | Line where the cursor is placed 'm | Line containing mark m '< | Start of visual selection '> | End of visual selection % | The entire file (shorthand for :1,$) Command | Effect ---------|------------------------------------------- :6t. | Copy line 6 to just below the current line :t6 | Copy the current line to just below line 6 :t. | Duplicate the current line (similar to Normal mode yyp ) :t$ | Copy the current line to the end of the file :'<,'>t0 | Copy the visually selected lines to the start of the file Command | Action --------------------------------------------------------------- q/ | Open the command-line window with history of searches q: | Open the command-line window with history of Ex commands ctrl-f | Switch from Command-Line mode to the command-line window Command | Effect ---------------------|--------------------------------------------- :shell | Start a shell (return to Vim by typing exit) :!{cmd} | Execute {cmd} with the shell :read !{cmd} | Execute {cmd} in the shell and insert its standard output below the cursor :[range]write !{cmd} | Execute {cmd} in the shell with [range] lines as standard input :[range]!{filter} | Filter the specified [range] through external program {filter} :2,$!sort -t',' -k2

Repeat

Intent | Act | Repeat| Reverse ---------------------------------|-----------------------|-------|-------- Make a change | {edit} | . | u Scan line for next character | f{char} / t{char} | ; | , Scan line for previous character | F{char} / T{char} | ; | , Scan document for next match | /pattern <CR> | n | N Scan document for previous match | ?pattern <CR> | n | N Perform substitution | :s/target/replacement | & | u Execute a sequence of changes | qx{changes}q | @x | u * - search words under cursor

Files

:ls :bn(ext) :bprev(ious) :bdelete N1 N2 N3 :N,M bdelete <C-^> - switch back to the already editing buffer <C-g> - command echoes the name and status of the current file :args :args {arglist} :args index.html app.js :args **/*.js **/*.css :args `cat .chapters` :lcd {path} - command lets us set the working directory locally for the current window :set path? - inspect path value gf - go to file under cursor Command | Effect ---------|----------------------------------------- :w[rite] | Write the contents of the buffer to disk :e[dit]! | Read the file from disk back into the buffer (that is, revert changes) :qa[ll]! | Close all windows, discarding changes without warning :wa[ll] | Write all modified buffers to disk

Windows

<C-w>s | Split the current window horizontally, reusing the current buffer in the new window <C-w>v | Split the current window vertically, reusing the current buffer in the new window :sp[lit] {file} | Split the current window horizontally, loading {file} into the new window :vsp[lit] {file} | Split the current window vertically, loading {file} into the new window Command | Effect ------------------------------------ <C-w>w | Cycle between open windows <C-w>h | Focus the window to the left <C-w>j | Focus the window below <C-w>k | Focus the window above <C-w>l | Focus the window to the right Ex Command | Normal | Command Effect --------------------------------------------- :cl[ose] | <C-w>c | Close the active window :on[ly] | <C-w>o | Keep only the active window, closing all others Keystrokes | Buffer Contents ----------------------------------------------------- <C-w>= | Equalize width and height of all windows <C-w>_ | Maximize height of the active window <C-w>| | Maximize width of the active window [N]<C-w>_ | Set active window height to [N] rows [N]<C-w>| | Set active window width to [N] columns

Tabs

Command | Effect ---------------------------------------------------- :tabe[dit] {filename} | Open {filename} in a new tab <C-w>T | Move the current window into its own tab :tabc[lose] | Close the current tab page and all of its windows :tabo[nly] | Keep the active tab page, closing all others Ex Command | Normal Command | Effect --------------------------------------------------------------- :tabn[ext] {N} | {N}gt | Switch to tab page number {N} :tabn[ext] | gt | Switch to the next tab page :tabp[revious] | gT | Switch to the previous tab page :tabmove [N] - rearrange tabs

Recipies

Open Files and Save Them to Disk

:pwd :edit lib/framework.js :edit % <Tab> - % symbol is a shorthand for the filepath of the active buffer :edit %:h <Tab> - :h modifier removes the filename while preserving the rest of the path

Save files to nonexistent directories:

:!mkdir -p %:h :write

Save a file as the super user

:w !sudo tee % > /dev/null :find Main.js :set path+=app/**

netrw

set nocompatible filetype plugin on Ex Command | Shorthand | Effect ------------------------------------------------------------------------- :edit . | :e. | Open file explorer for current working directory :Explore | :E | Open file explorer for the directory of the active buffer

Plugins

https://github.com/tpope/vim-commentary \\ap \\G \\\ - comment current line https://github.com/kana/vim-textobj-entire http://github.com/tpope/vim-surround - !!! Surround.vim p.129

Settings

:set shiftwidth=4 softtabstop=4 expandtab

No comments: