Vim From Essentials to Mastery 2011

294
Vim From Essentials to Mastery Tools, Techniques, and Configuration for the Vim Text Editor Bill Odom ( [email protected]) vimgeeks.org

Transcript of Vim From Essentials to Mastery 2011

Page 1: Vim From Essentials to Mastery 2011

VimFrom Essentials to MasteryTools, Techniques, and Configuration for the Vim Text Editor

Bill Odom ([email protected]) • vimgeeks.org

Page 2: Vim From Essentials to Mastery 2011
Page 3: Vim From Essentials to Mastery 2011
Page 4: Vim From Essentials to Mastery 2011
Page 5: Vim From Essentials to Mastery 2011
Page 6: Vim From Essentials to Mastery 2011
Page 7: Vim From Essentials to Mastery 2011
Page 8: Vim From Essentials to Mastery 2011

A Quick History Lesson...

Page 9: Vim From Essentials to Mastery 2011

What is Vim?

Page 10: Vim From Essentials to Mastery 2011

Modal Text Editor

Page 11: Vim From Essentials to Mastery 2011

Built by Bram Moolenar...

Page 12: Vim From Essentials to Mastery 2011
Page 13: Vim From Essentials to Mastery 2011
Page 14: Vim From Essentials to Mastery 2011

Based on vi...

Page 15: Vim From Essentials to Mastery 2011

...created by Bill Joy...

Page 16: Vim From Essentials to Mastery 2011
Page 17: Vim From Essentials to Mastery 2011

...in 1976.

Page 18: Vim From Essentials to Mastery 2011

19761976

Page 19: Vim From Essentials to Mastery 2011

So why bother?

Page 20: Vim From Essentials to Mastery 2011

The Sales Pitch

Page 21: Vim From Essentials to Mastery 2011

Multi-Language

Page 22: Vim From Essentials to Mastery 2011
Page 23: Vim From Essentials to Mastery 2011

Multi-Interface

Page 24: Vim From Essentials to Mastery 2011
Page 25: Vim From Essentials to Mastery 2011
Page 26: Vim From Essentials to Mastery 2011

Multi-Platform

Page 27: Vim From Essentials to Mastery 2011
Page 28: Vim From Essentials to Mastery 2011
Page 29: Vim From Essentials to Mastery 2011
Page 30: Vim From Essentials to Mastery 2011
Page 31: Vim From Essentials to Mastery 2011
Page 32: Vim From Essentials to Mastery 2011
Page 33: Vim From Essentials to Mastery 2011
Page 34: Vim From Essentials to Mastery 2011
Page 35: Vim From Essentials to Mastery 2011

Active Community

Page 36: Vim From Essentials to Mastery 2011
Page 37: Vim From Essentials to Mastery 2011

Ridiculously Powerful

Page 38: Vim From Essentials to Mastery 2011

Ridiculously Configurable

Page 39: Vim From Essentials to Mastery 2011

Open Source

Page 40: Vim From Essentials to Mastery 2011

Good for You

Page 41: Vim From Essentials to Mastery 2011

Good for Orphans:help uganda

Page 42: Vim From Essentials to Mastery 2011
Page 43: Vim From Essentials to Mastery 2011

The Fine Print...

Page 44: Vim From Essentials to Mastery 2011

Classical learning curves

for some common editors

Notepad pico

Visual Studio vi emacs

Page 45: Vim From Essentials to Mastery 2011

vi

Page 46: Vim From Essentials to Mastery 2011

and yet...

Page 47: Vim From Essentials to Mastery 2011
Page 48: Vim From Essentials to Mastery 2011

The Sales Pitch✓

Page 49: Vim From Essentials to Mastery 2011

So why bother?

Save Time

Increase Productivity

Reduce Tedium

Remain Sane

Have Fun

Page 50: Vim From Essentials to Mastery 2011

Use a single editor well.

The editor should be an extension of your hand;make sure your editor is configurable, extensible, and programmable.

—The Pragmatic Programmerby Andrew Hunt and David Thomas

Page 51: Vim From Essentials to Mastery 2011

Goals

Page 52: Vim From Essentials to Mastery 2011

How to Get Started

Page 53: Vim From Essentials to Mastery 2011

A Few Big Ideas

Page 54: Vim From Essentials to Mastery 2011

Lots of Tips

Page 55: Vim From Essentials to Mastery 2011

Discussion, QuestionsComments

Page 56: Vim From Essentials to Mastery 2011

Non-Goals

Page 57: Vim From Essentials to Mastery 2011
Page 58: Vim From Essentials to Mastery 2011

“Escape Meta Alt Control Shift”

Page 59: Vim From Essentials to Mastery 2011

“Emacs: A great operating“system, lacking only a“decent editor.”

Page 60: Vim From Essentials to Mastery 2011

vi has two modes:“beep repeatedly” and “break everything”

Page 61: Vim From Essentials to Mastery 2011

Sometimes, people ask me if it is a sin in the church of Emacs to use the editor Vi. It is true that Vi-Vi-Vi is the editor of the beast. But using a free version of Vi is not a sin but a penance.

— Richard Stallman (creator of EMACS)

Page 62: Vim From Essentials to Mastery 2011
Page 63: Vim From Essentials to Mastery 2011

Getting Started

Page 64: Vim From Essentials to Mastery 2011

Step One: Don’t

Page 65: Vim From Essentials to Mastery 2011
Page 66: Vim From Essentials to Mastery 2011
Page 67: Vim From Essentials to Mastery 2011

A small piece of advice:Don’t try to do it all at once.

Get a good version of vi that doesn't make you abandon everything you already know—then slowly, steadily build up your repertoire of magic tricks...

Page 68: Vim From Essentials to Mastery 2011

I’ve been using some form of vi for years, and I still learn new stuff all the time. Frankly, that’s part of its appeal—it’s as deep as I want to go, and there’s more power every time I take a few minutes to learn it.

– from an email exchange between Bill Odom and Merlin Mann

Page 69: Vim From Essentials to Mastery 2011

You should not try to learn every command an editor offers. That would be a complete waste of time. Most people only need to learn 10 to 20 percent of the commands for their work. But it’s a different set of commands for everybody.

—Bram Moolenar

Page 70: Vim From Essentials to Mastery 2011

I am amzedf at how mcuh easier it is to use vim when I jhave had a few beers. The modal commands just seem to flow from my fgingertips.

—Bill Odom (from “vim beer amazement.txt”)

Page 71: Vim From Essentials to Mastery 2011
Page 72: Vim From Essentials to Mastery 2011

Getting Vim

Page 73: Vim From Essentials to Mastery 2011

UNIX or Linux?Done.

Page 74: Vim From Essentials to Mastery 2011

Mac OS X:code.google.com/p/macvim/

Page 75: Vim From Essentials to Mastery 2011
Page 76: Vim From Essentials to Mastery 2011

Everybody else:www.vim.org/download.php

Page 77: Vim From Essentials to Mastery 2011

Getting Help

:help (or just :h)

:help :help

:helpgrep (or just :helpg)

:help!

:h 42

:h holy-grail

Page 78: Vim From Essentials to Mastery 2011
Page 79: Vim From Essentials to Mastery 2011
Page 80: Vim From Essentials to Mastery 2011

Modes

Page 81: Vim From Essentials to Mastery 2011

A Whole Lotta Modes

Insert Mode

Normal Mode

Command Mode

Visual Mode

Select Mode

Ex Mode

Operator-pending Mode

Replace Mode

Virtual Replace Mode

Insert Normal Mode

Insert Visual Mode

Insert Select Mode

Page 82: Vim From Essentials to Mastery 2011

A Whole Lotta Modes

Insert Mode

Normal Mode

Command Mode

Visual Mode

Page 83: Vim From Essentials to Mastery 2011

Insert Mode

Typical text entry and basic editing

Arrow keys (and mouse) work

Lots of ways to get there...

Page 84: Vim From Essentials to Mastery 2011

Insert Mode

i a c o r s

I A C O R S

Page 85: Vim From Essentials to Mastery 2011

Insert Mode

<Esc> to get out

<C-O> to temporarily get out

Page 86: Vim From Essentials to Mastery 2011

Normal Mode

Moving around

More complex editing

Default mode

Page 87: Vim From Essentials to Mastery 2011

Moving Around

kh • l j

Page 88: Vim From Essentials to Mastery 2011

Moving Around

H M L

gg

123G

:123

Page 89: Vim From Essentials to Mastery 2011

Moving Around

Many more advanced motions available

But the arrow keys & mouse still work!

Page 90: Vim From Essentials to Mastery 2011

Moving Around

gj

gk

Page 91: Vim From Essentials to Mastery 2011

Traveling Without Moving

Page 92: Vim From Essentials to Mastery 2011

Traveling Without Moving

zz

zt

zb

Page 93: Vim From Essentials to Mastery 2011

Traveling Without Moving

:let &scrolloff=999-&scrolloff

Page 94: Vim From Essentials to Mastery 2011

Instant Searches

*

#

g*

g#

Page 95: Vim From Essentials to Mastery 2011

Completion

<C-N>

<C-P>

<C-X><C-…>

Page 96: Vim From Essentials to Mastery 2011
Page 97: Vim From Essentials to Mastery 2011

Command-Line Mode

Ex commands

Search commands

Filter commands

Page 98: Vim From Essentials to Mastery 2011

Visual Mode

Page 99: Vim From Essentials to Mastery 2011

What?

Visual mode is a flexible and easy way to select a piece of text for an operator.

— :help visual-mode

Page 100: Vim From Essentials to Mastery 2011

Why use it?

Simple way to select text and operate on it

Often faster (and easier) than other methods

Sometimes it’s the only way to indicate an arbitrary block of text

Page 101: Vim From Essentials to Mastery 2011

Three Modes in One!

Visual

Visual Line

Visual Block

Page 102: Vim From Essentials to Mastery 2011

Three Modes in One!

Visual - characterwise

Visual Line - linewise

Visual Block - er... rectanglewise

Page 103: Vim From Essentials to Mastery 2011

Getting There

v for Visual

V for Visual Line

<C-V> for Visual Block

Page 104: Vim From Essentials to Mastery 2011

Getting Out

Page 105: Vim From Essentials to Mastery 2011

Defining the Visual Area

Enter Visual mode

Move around

...or use a text object

...or a search

...or even a mouse

Page 106: Vim From Essentials to Mastery 2011

Doing Stuff

Define a visual area

Use an operator

...or an Ex command

Page 107: Vim From Essentials to Mastery 2011

Visual Operators

All the usual suspects:

~ d c y > < ! = gq

...and a bunch more:

r s C S R x D

X Y p J U u ^] I

Page 108: Vim From Essentials to Mastery 2011

Oops, Wrong Mode

No problem

Switch among modes with

v, V, or <C-V>

Start and end points are preserved

Page 109: Vim From Essentials to Mastery 2011

Oops, Wrong Spot

Started in the wrong place?

Toggle between start end end with o(lowercase o)

Page 110: Vim From Essentials to Mastery 2011

Oops, Wrong Corner

For Visual Blocks

Need to move the other side?

Switch to the other corner with O(uppercase O)

Page 111: Vim From Essentials to Mastery 2011

o.O

Page 112: Vim From Essentials to Mastery 2011

Prepending Text in a Block

Define a Visual Block

Press I (“Insert at beginning”)

Type the text to be prepended

Hit Escape

Works in the middle of a block, too

Page 113: Vim From Essentials to Mastery 2011

Appending Text in a Block

Define a Visual Block

Press A (“Append”)

Type the text to be appended

Hit Escape

Page 114: Vim From Essentials to Mastery 2011

Appending Text in a Block

Start a Visual Block

...but press $ to move to the end of a line

Press A (for “Append”)

Type the text to be appended

Hit Escape

Text is appended to each line, sensitive to the length

Page 115: Vim From Essentials to Mastery 2011

Example Maps" Backspace in Visual mode deletes selection."vnoremap <BS> d

" Tab/Shift+Tab indent/unindent the highlighted" block (and maintain the highlight after changing" the indentation). Works for both Visual and" Select modes."vnoremap <Tab> >gvvnoremap <S-Tab> <gv

Page 116: Vim From Essentials to Mastery 2011

Example Maps

" Join all lines in a paragraph"nnoremap <S-F7> vipJ

" Format selected lines"xnoremap Q gq

Page 117: Vim From Essentials to Mastery 2011

Visual Key Map Gotchas

Watch out for :vmap

Prefer :xmap and :smap where possible

Page 118: Vim From Essentials to Mastery 2011

Example Tricks

Count bytes in an arbitrary selection

See text objects

Draw boxes

Abuse indentation

Filter a visual range

Page 119: Vim From Essentials to Mastery 2011

Important Settings

virtualedit

selectmode

keymodel

mouse

Page 120: Vim From Essentials to Mastery 2011

Important Settings

:set virtualedit=block

:set selectmode=key,mouse

:set keymodel=startsel,stopsel

:set mouse=a

Page 121: Vim From Essentials to Mastery 2011

Useful Help Topics

:help visual-mode

:help v_...

:help v_b_...

Page 122: Vim From Essentials to Mastery 2011
Page 123: Vim From Essentials to Mastery 2011

Core Configuration

Page 124: Vim From Essentials to Mastery 2011

The Usual Suspects

.vimrc

.gvimrc

~/.vim/

Page 125: Vim From Essentials to Mastery 2011

Strict vi Compatibility?

:set nocompatible

Page 126: Vim From Essentials to Mastery 2011

Extensions

:filetype plugin indent on

Page 127: Vim From Essentials to Mastery 2011

Backups and Swapfiles

:set backupdir-=.:set backupdir^=~/tmp,/tmp:set directory=~/tmp//,.

Page 128: Vim From Essentials to Mastery 2011

Backups and Swapfiles

:set backupdir-=.:set backupdir^=$TEMP:set directory=$TEMP\\\\

Page 129: Vim From Essentials to Mastery 2011

:set incsearch:set hlsearch:set ignorecase:set smartcase

Easier Searching

Page 130: Vim From Essentials to Mastery 2011

:set tabstop=4:set softtabstop=4:set expandtab:set smarttab

Controlling Whitespace

Page 131: Vim From Essentials to Mastery 2011

Controlling Whitespace

:set shiftwidth:set shiftround

Page 132: Vim From Essentials to Mastery 2011

Roam If You Want To

:set backspace=indent,eol,start

:set whichwrap+=<,>,[,]

Page 133: Vim From Essentials to Mastery 2011

Better Buffers

:set hidden

Page 134: Vim From Essentials to Mastery 2011

Blessed Silence

:set vb:set t_vb=

Page 135: Vim From Essentials to Mastery 2011
Page 136: Vim From Essentials to Mastery 2011

Operators

c

d

y

~

g~

gu

gU

!

=

gq

g?

>

<

zf

g@

Page 137: Vim From Essentials to Mastery 2011

Operators

:help operator

Page 138: Vim From Essentials to Mastery 2011

Motions

h j k l

gj gk

0 ^ $

g0 g^ g$ g_

f F t T

w W e E

b B ge gE

- +

( ) { }

]] ][ [[

| _ %

Page 139: Vim From Essentials to Mastery 2011

Motions

:help motion.txt

Page 140: Vim From Essentials to Mastery 2011

Text Objects

w - Words

W - WORDs

s - Sentences

p - Paragraphs

t - Tagged blocks

Blocks delimited by [] () {} <>

Strings delimited by " ' `

Page 141: Vim From Essentials to Mastery 2011

Text Objects

:help text-objects

Page 142: Vim From Essentials to Mastery 2011

Ranges

Line numbers (absolute or symbolic)

Marks (manual or automatic)

Search patterns (forward or backward)

Page 143: Vim From Essentials to Mastery 2011

Ranges

:help cmdline-ranges

Page 144: Vim From Essentials to Mastery 2011
Page 145: Vim From Essentials to Mastery 2011

Registers

Page 146: Vim From Essentials to Mastery 2011

What’s a Register?

Stores a chunk of text

Like the system clipboard

But much more flexible

Page 147: Vim From Essentials to Mastery 2011

The Unnamed Register

You already use this one

The one you get if you don’t specify

Target of typical delete, yank, and put operations (a.k.a. cut, copy, and paste)

Page 148: Vim From Essentials to Mastery 2011

Simple Registers

26 named registers

Accessed as "a through "z

Page 149: Vim From Essentials to Mastery 2011

Appending to Registers

Deletes and yanks overwrite registers

But they don’t have to

Uppercase the name to append instead

Page 150: Vim From Essentials to Mastery 2011

Numbered Registers

You don’t always have to be explicit

Vim tracks recent deletes

Look in "1 through "9

Page 151: Vim From Essentials to Mastery 2011

Other Registers

Current filename:

"%

Alternate filename:

"#

Page 152: Vim From Essentials to Mastery 2011

Other Registers

Last “small” delete:

"-

Last inserted text:

".

Page 153: Vim From Essentials to Mastery 2011

Other Registers

Last search:

"/

Last Ex command:

":

Page 154: Vim From Essentials to Mastery 2011

Other Registers

Expression:

"=

(Lots more on this one later)

Page 155: Vim From Essentials to Mastery 2011

Other Registers

System clipboard:

"*

System Selection (X11):

"+

Page 156: Vim From Essentials to Mastery 2011

Other Registers

Black hole:

"_

Page 157: Vim From Essentials to Mastery 2011

Viewing Registers

View with :registers

Or just :reg

View specific registers with :reg abc

Page 158: Vim From Essentials to Mastery 2011

Accessing Registers

Normal mode:

"<register><operator>

Page 159: Vim From Essentials to Mastery 2011

Accessing Registers

Visual mode:

"<register><operator>

Page 160: Vim From Essentials to Mastery 2011

Accessing Registers

Insert mode and command line:

<C-R><register>

<C-R><C-R><register>

<C-R><C-O><register>

<C-R><C-P><register>

Page 161: Vim From Essentials to Mastery 2011

Accessing Registers

Ex commands and scripts:

@<register>

Page 162: Vim From Essentials to Mastery 2011

Registers == Macros

Execute in Normal mode:

@<register>

Page 163: Vim From Essentials to Mastery 2011

Registers == Ex Commands?

Execute in Command mode:

:@<register>

Page 164: Vim From Essentials to Mastery 2011

Clearing Registers

Assign on the command line:

:let @a = ""

Record an empty macro:

qaq

Page 165: Vim From Essentials to Mastery 2011

Expression Register

Value is the result of an expression

Can be used interactively or in scripts

Accessed as "= or @= or \= depending on context

Page 166: Vim From Essentials to Mastery 2011

Expression Register Example

inoremap <F2>

<C-R>=strftime("%c")<CR>

Page 167: Vim From Essentials to Mastery 2011

Expression Register Example

:cnoremap <C-G>

<C-R><C-R>=

GetLiteralPattern()

<CR>

Page 168: Vim From Essentials to Mastery 2011

Expression Register Example

:xnoremap \/

"vy/

<C-R><C-R>

=StringToPattern(@v)

<CR>

Page 169: Vim From Essentials to Mastery 2011

Tips & Tricks

Copy to the OS clipboard:

"*yy

Page 170: Vim From Essentials to Mastery 2011

Tips & Tricks

Paste from the OS clipboard:

"*p

(Can be more reliable than pasting through a terminal window.)

Page 171: Vim From Essentials to Mastery 2011

Tips & Tricks

Link unnamed register and OS clipboard:

:set clipboard=unnamed

Page 172: Vim From Essentials to Mastery 2011

Tips & Tricks

Pre-load in .vimrc:

:let @a = "123 Main Street"

:let @p = "800-555-1212"

Page 173: Vim From Essentials to Mastery 2011

Tips & Tricks

“Search” for text without moving cursor:

:let @/ = "stuff"

Page 174: Vim From Essentials to Mastery 2011

Tips & Tricks

Execute accidentally-inserted command:

<Esc>u:@.

Page 175: Vim From Essentials to Mastery 2011

Tips & Tricks

Edit a macro:

"mp

...edit macro text...

"myy

Page 176: Vim From Essentials to Mastery 2011

Tips & Tricks

Combine several registers:

:let @* = @a . @b . @c

Page 177: Vim From Essentials to Mastery 2011

Tips & Tricks

Capture output to a register:

:redir @A

Page 178: Vim From Essentials to Mastery 2011

Tips & Tricks

Yank matching lines into a register:

:g/pattern/y A(Note uppercase register name)

Page 179: Vim From Essentials to Mastery 2011

Register Help

:help registers

:help copy-move

Page 180: Vim From Essentials to Mastery 2011

Register Help

:help i_CTRL-R

:help i_CTRL-R_CTRL-R

:help i_CTRL-R_CTRL-O

:help i_CTRL-R_CTRL-P

Page 181: Vim From Essentials to Mastery 2011
Page 182: Vim From Essentials to Mastery 2011

Vim as Language

Page 183: Vim From Essentials to Mastery 2011
Page 184: Vim From Essentials to Mastery 2011
Page 185: Vim From Essentials to Mastery 2011

Verbs:Operators & Commands

Page 186: Vim From Essentials to Mastery 2011

d

Page 187: Vim From Essentials to Mastery 2011

Subjects:Motions & Text Objects

Page 188: Vim From Essentials to Mastery 2011

dj

Page 189: Vim From Essentials to Mastery 2011

Adjectives:Ranges & Repetition

Page 190: Vim From Essentials to Mastery 2011

d10j

Page 191: Vim From Essentials to Mastery 2011

Objects:Registers

Page 192: Vim From Essentials to Mastery 2011

“xd10j

Page 193: Vim From Essentials to Mastery 2011

Examples

Page 194: Vim From Essentials to Mastery 2011

0y$

Page 195: Vim From Essentials to Mastery 2011

yy

Page 196: Vim From Essentials to Mastery 2011

gg=G

Page 197: Vim From Essentials to Mastery 2011

“adG

Page 198: Vim From Essentials to Mastery 2011

gqapvipJ

Page 199: Vim From Essentials to Mastery 2011
Page 200: Vim From Essentials to Mastery 2011
Page 201: Vim From Essentials to Mastery 2011

vat

Page 202: Vim From Essentials to Mastery 2011

“ayis

Page 203: Vim From Essentials to Mastery 2011

kyyp^v$r=ja

Page 204: Vim From Essentials to Mastery 2011
Page 205: Vim From Essentials to Mastery 2011
Page 206: Vim From Essentials to Mastery 2011
Page 207: Vim From Essentials to Mastery 2011

Macros

Record and repeat a series of keystrokes

Can be assigned to registers a to z

Run with @a

Repeat last macro with @@

Page 208: Vim From Essentials to Mastery 2011

Abbreviations

iabbrev for Insert Mode

cabbrev for Command-Line Mode

abbrev for both

Best for content expansion (vs. behavior)

Page 209: Vim From Essentials to Mastery 2011

Key Mapping

Page 210: Vim From Essentials to Mastery 2011

What?

Bind a series of keystrokes to a key sequence

Better for defining behavior (vs. abbreviations)

Better for long-term customization(vs. macros)

Can be defined across all of Vim’s modes

Fundamental to customizing Vim

Page 211: Vim From Essentials to Mastery 2011

{cmd} {attr} {lhs} {rhs}map command attributes left-hand side right-hand side

Map Command Structure

Page 212: Vim From Essentials to Mastery 2011

Map Command Structure

" Example map commandmap <silent> <F2> :echo 'Hello!'<CR>

Page 213: Vim From Essentials to Mastery 2011

Map Commands

nmapimapvmapxmapsmap

omapcmaplmapmapmap!

Page 214: Vim From Essentials to Mastery 2011

Map Commandsnnoremapinoremapvnoremapxnoremapsnoremaponoremapcnoremap

Page 215: Vim From Essentials to Mastery 2011

Map Commandsnnoremapinoremapvnoremapxnoremapsnoremaponoremapcnoremap

Page 216: Vim From Essentials to Mastery 2011

Map Attributes

<silent><buffer><expr><unique><script><special>

Page 217: Vim From Essentials to Mastery 2011

Available Keys

Function keys and shifted function keys

<Leader> and <LocalLeader> sequences (especially in plugins and scripts)

Page 218: Vim From Essentials to Mastery 2011

Sorta-Available Keys

Alt / Option / Meta Keys (safest in the GUI)

Commands you don’t use

Command “synonyms” you don’t use

Comma

Semicolon

Underscore

Page 219: Vim From Essentials to Mastery 2011

Finding Keys to Map

:help map-which-keys

:help index.txt

:help {key}

Page 220: Vim From Essentials to Mastery 2011

Displaying Maps

:nmap

:nmap {key}

:mkvimrc ~/scratch.vim

Page 221: Vim From Essentials to Mastery 2011

Removing Maps

:unmap {key}

:verbose map {key}

Page 222: Vim From Essentials to Mastery 2011

" Overload Control+L to also clear" search highlighting"nnoremap <C-L> :nohlsearch<CR><C-L>

Sample Maps

Page 223: Vim From Essentials to Mastery 2011

" Toggle screen wrapping of long lines," then display value of ‘wrap’ option"nnoremap <Leader>w\ :set invwrap<Bar>set wrap?<CR>

Sample Maps

Page 224: Vim From Essentials to Mastery 2011

" Make up/down arrows move by display" lines instead of physical lines"nnoremap <Up> gknnoremap <Down> gj

Sample Maps

Page 225: Vim From Essentials to Mastery 2011

" Reformat current paragraph" nnoremap Q gqap

Sample Maps

Page 226: Vim From Essentials to Mastery 2011

" Indent/unindent highlighted block" (and maintain highlight)"vnoremap <Tab> >gvvnoremap <S-Tab> <gv

Sample Maps

Page 227: Vim From Essentials to Mastery 2011

" Disable paste-on-middle-click"inoremap <MiddleMouse> <Nop>

Sample Maps

Page 228: Vim From Essentials to Mastery 2011

" Center display line after searches"nnoremap n nzznnoremap N Nzznnoremap * *zznnoremap # #zznnoremap g* g*zznnoremap g# g#z

Sample Maps

Page 229: Vim From Essentials to Mastery 2011

" Edit vimrc in new tab"nnoremap ,ev :tabedit $MYVIMRC<CR>

Sample Maps

Page 230: Vim From Essentials to Mastery 2011

" Delete to end of line, bash-style"inoremap <C-K> <C-O>D

Sample Maps

Page 231: Vim From Essentials to Mastery 2011

" Control+Up/Down move lines & selections up and down." (Based on http://vim.wikia.com/wiki/VimTip646)" " Define maps for Normal and Visual modes, then re-use" them for Insert and Select modes."nnoremap <silent> <C-Up> :move -2<CR>nnoremap <silent> <C-Down> :move +<CR>xnoremap <silent> <C-Up> :move '<-2<CR>gvxnoremap <silent> <C-Down> :move '>+<CR>gvimap <silent> <C-Up> <C-O><C-Up>imap <silent> <C-Down> <C-O><C-Down>smap <silent> <C-Up> <C-G><C-Up><C-G>smap <silent> <C-Down> <C-G><C-Down><C-G>

Sample Maps

Page 232: Vim From Essentials to Mastery 2011

$VIMRUNTIME/mswin.vim

Page 233: Vim From Essentials to Mastery 2011

:vmap <Tab> >gv:vmap <S-Tab> <gv

Page 234: Vim From Essentials to Mastery 2011

:nnoremap <F7> gqap:nnoremap <S-F7> vipJ

Page 235: Vim From Essentials to Mastery 2011
Page 236: Vim From Essentials to Mastery 2011

(and Files(and Buffers(and Windows(and Tabs and Instances

Page 237: Vim From Essentials to Mastery 2011

Definitions:

Page 238: Vim From Essentials to Mastery 2011

File

Um... a file

Page 239: Vim From Essentials to Mastery 2011

Buffer

Usually associated with a file, but doesn’t have to be

Usually has a name, but doesn’t have to

Doesn’t have to be visible

Several different types(that you mostly don’t have to care about)

Page 240: Vim From Essentials to Mastery 2011

Window

Displays the contents of a buffer

Always at least one window open

More than one window can display the same buffer

Page 241: Vim From Essentials to Mastery 2011

Tab

A group of windows

Not like tabs in other editors

Sometimes called “tab pages”

Better name might be “layout” or “viewport”

Page 242: Vim From Essentials to Mastery 2011

Buffers, Windows, Tab Pages

Often confused for one another

A buffer is the contents of a (potential) file

A window is a place where a buffer can be viewed

A tab page is a collection of windows

A buffer can be viewed in any number of windows

Page 243: Vim From Essentials to Mastery 2011

Instance

A single running copy of Vim

GUI or terminal

Doesn’t share (well) with other Vim instances

Page 244: Vim From Essentials to Mastery 2011

Loading Multiple Files

vim *.sh

vim foo.txt bar.txt baz.txt

vim -o widget.c header.h

vim -p widget.c header.h

Page 245: Vim From Essentials to Mastery 2011

Loading Multiple Files

:edit foo.txt

:args *.sh

:args foo.txt bar.txt baz.txt

The args list is modifiable with:argadd and:argdel

Page 246: Vim From Essentials to Mastery 2011

Moving Among Buffers

:set hidden

:ls

:b N

:b name

:bfirst :blast :bnext :bprev

<C-^>

Page 247: Vim From Essentials to Mastery 2011

Closing Buffers

:bdelete

:bd N

:bd name

:bd!

Page 248: Vim From Essentials to Mastery 2011

Buffer Settings

hidden

switchbuf

Page 249: Vim From Essentials to Mastery 2011

Buffer Commands

:ball

:bufdo

Page 250: Vim From Essentials to Mastery 2011

Creating Windows

:split

:vsplit

<C-W> s

<C-W> v

Page 251: Vim From Essentials to Mastery 2011

Moving Between Windows

<C-W> h

<C-W> j

<C-W> k

<C-W> l

Page 252: Vim From Essentials to Mastery 2011

Moving Between Windows

<C-W> p

<C-W> <C-W>

Page 253: Vim From Essentials to Mastery 2011

Resizing Windows

<C-W> +

<C-W> -

<C-W> <

<C-W> >

...or just use the mouse

Page 254: Vim From Essentials to Mastery 2011

Resizing Windows

<C-W> =

<C-W> _

<C-W> |

Page 255: Vim From Essentials to Mastery 2011

Moving Windows

<C-W> H

<C-W> J

<C-W> K

<C-W> L

<C-W> x

Page 256: Vim From Essentials to Mastery 2011

Closing Windows

:q

<C-W> c

:hide

Page 257: Vim From Essentials to Mastery 2011

Closing Other Windows

:only

<C-W> o

Page 258: Vim From Essentials to Mastery 2011

Window Commands

:vertical

:windo

:wincmd

Page 259: Vim From Essentials to Mastery 2011

Window Keys

More than you’ll ever need (or want)

Good candidates for your own key maps

:help windows.txt

:help ctrl-w

Page 260: Vim From Essentials to Mastery 2011

Window Settings

splitbelow

splitright

winminheight

winminwidth

winheight

Page 261: Vim From Essentials to Mastery 2011

Creating Tab Pages

:tabnew

:tabedit

<C-W> T

Page 262: Vim From Essentials to Mastery 2011

Moving Between Tab Pages

gt

gT

Ngt

Often mapped to OS-specific keystrokes

Page 263: Vim From Essentials to Mastery 2011

Moving Tab Pages

:tabmove

:tabmove N

...or just use the mouse (in the GUI)

Page 264: Vim From Essentials to Mastery 2011

Closing Tab Pages

:q in last window

:tabclose

GUI often provides a close button

Page 265: Vim From Essentials to Mastery 2011

Closing Other Tab Pages

:tabclose N

:tabonly

Page 266: Vim From Essentials to Mastery 2011

Tab Page Settings

tabline

guitablabel

May be OS-specific options as well

Page 267: Vim From Essentials to Mastery 2011

Tab Page Commands

:tab

:tabs

:tabdo

Page 268: Vim From Essentials to Mastery 2011

What Good are Tab Pages?

Full-page help, docs, etc.

Alternate layouts

Temporary work

Diffs

Multiple projects

Mini-sessions

Page 269: Vim From Essentials to Mastery 2011

Instances

A single instance includes all buffers, windows, tabs, settings, etc.

Easy to create more than one, especially in the terminal

...but that’s usually not necessary

...and usually isn’t a good idea

Page 270: Vim From Essentials to Mastery 2011
Page 271: Vim From Essentials to Mastery 2011

Syntax Highlighting

Highlights keywords, operators, blocks of text based on patterns in language-specific syntax files

Not a parser, but still very powerful

Can be nested

Useful for more than just programming languages

Page 272: Vim From Essentials to Mastery 2011

Indentation

Automatically indent different types of files based on rules in language-specific indentation files

Like syntax highlighting, can be used for more than just programming languages

Page 273: Vim From Essentials to Mastery 2011

Plugins

Page 274: Vim From Essentials to Mastery 2011

NERD Tree

Page 275: Vim From Essentials to Mastery 2011
Page 276: Vim From Essentials to Mastery 2011

Command-TPeepOpenFuzzyFileFinder

Page 277: Vim From Essentials to Mastery 2011
Page 278: Vim From Essentials to Mastery 2011
Page 279: Vim From Essentials to Mastery 2011
Page 280: Vim From Essentials to Mastery 2011
Page 281: Vim From Essentials to Mastery 2011

Bufexplorer

Page 282: Vim From Essentials to Mastery 2011
Page 283: Vim From Essentials to Mastery 2011

Taglist

Page 284: Vim From Essentials to Mastery 2011
Page 285: Vim From Essentials to Mastery 2011
Page 286: Vim From Essentials to Mastery 2011

ResourcesVimhttp://www.vim.org/

MacVimhttp://code.google.com/p/macvim/

Vim Plugins, Add-Ons, Scriptshttp://www.vim.org/scripts/index.php

Vim Mailing Listshttp://www.vim.org/maillist.php

Vim Tips Wikihttp://vim.wikia.com/wiki/Vim_Tips_Wiki

Page 287: Vim From Essentials to Mastery 2011

ResourcesEfficient Editing With Vimhttp://jmcpherson.org/editing.html

Zenclavier: Extreme Keyboardinghttp://www.oreilly.com/news/zenclavier_1299.html

Your problem with Vim is that you don’t grok vihttp://stackoverflow.com/questions/1218390/#1220118

Everyone Who Tried to Convince Me to use Vim was Wronghttp://yehudakatz.com/2010/07/29/everyone-who-tried-to-convince-me-to-use-vim-was-wrong/

Damian Conway’s “Scripting the Vim Editor” Serieshttps://www.ibm.com/developerworks/linux/library/l-vim-script-1/

Page 288: Vim From Essentials to Mastery 2011

Resourcesgodlygeek's explanation of buffers, windows, and tabshttp://vim.pastey.net/115548

VimCasts - How to use buffers/windows/tabshttp://vimcasts.org/episodes/working-with-buffers/http://vimcasts.org/episodes/working-with-windows/http://vimcasts.org/episodes/working-with-tabs/http://vimcasts.org/episodes/how-to-use-tabs/

Derek Wyatt - Working with Many Fileshttp://vimeo.com/6306508

Vim Tips Wiki - Tabshttp://vim.wikia.com/wiki/Category:Tabs

Page 289: Vim From Essentials to Mastery 2011

ResourcesVimcastshttp://vimcasts.org/

Lococast Screencastshttp://lococast.net/archives/category/screencast

Derek Wyatt’s Vim Tutorial Videoshttp://www.derekwyatt.org/vim/vim-tutorial-videos/

Page 290: Vim From Essentials to Mastery 2011

ResourcesBill Odom's Vim Environmenthttps://github.com/wnodom/wnodom-vim-environment

Steven Pritchard's Vim Environmenthttps://github.com/silug/vim-environment

Damian Conway's Vim Environmenthttps://github.com/thoughtstream/Damian-Conway-s-Vim-Setup

Steve Losh's Vim Environmenthttps://github.com/sjl

Tim Pope's Vim Environmenthttps://github.com/tpope/tpope

Page 291: Vim From Essentials to Mastery 2011

ResourcesVim Geekshttp://vimgeeks.org/

@vimgeekshttp://twitter.com/vimgeeks

Vim-Related Twitter Accountshttps://twitter.com/#!/VimGeeks/following

Page 292: Vim From Essentials to Mastery 2011
Page 293: Vim From Essentials to Mastery 2011

Thanks!

Page 294: Vim From Essentials to Mastery 2011