Ryan Brown: Sysadmin, Programmer, Vim user
s/user/addict/
Hi, I'm Ryan. I make things for computers, and I use Vim for making most of those. (Even these slides)
If you have questions, ask.
But please hold "What if blarg foo's the honk?" type questions until the end.
Mac/Linux/UNIX: vim myfile.py
Windows: There's an icon or something
Press Escape, then type :q
Esc :q
This will fail if the file hasn't been saved. Fix that one of two ways.
Esc :q!
force quit, tells vim to ignore all warnings/errors/dragonsEsc :wq
save and quit^ The k key moves up k The h key is at the left and moves left. < h l > The l key is at the right and moves right. j The j key looks like a down arrow. v
i
to insert textYou can think of any Normal mode command as a sentence.
Copy (yank) the line your cursor is on
yy
Yank three lines
3yy
Commands can also accept arguments like movement commands. Yank accepts movement commands.
yj
hjkl
for movementhjklewbtfHMLN
Old-school vi users use Ctrl + [
instead of escape because Esc
used to sometimes send different key sequences. Use whichever you want to, they
both do the same thing
All these key sequences are intended so you never have to leave the home
row. hjkl
was chosen because it's on the home row.
Less time moving hands = more time working = done faster
:help text-objects
w : word
W : WORD
s : sentence defined as a block ending with '.', '!', '?', newline, or tab
p : paragraph is any block of text after an empty line
B : Block
t : tag is the contents of a <aa> and </aa>
Any matching character '(', '[', etc.
Forgot semicolon?
A;
Swap this line with the next?
ddp
Swapped-letter typo?
xp
Change the contents of a set of quotes?
ci"
Delete an HTML tag and its contents?
dat
Most common way to select text: Visual Mode
Shift+V
Next most common: Visual Block Mode
^V
After selecting text, you can then act on these blocks with commands, try these for starters:
d x c y ~
Sure Vim is a command-line program, but it's got windows. You can even move them around and such.
Just typing :help brings up a new window and you can close it with :q (:qa closes all of your open windows) or switch focus with ^W^W.
Open a new window horizontally split. (For vertical split use v instead of s)
^Ws
To cycle through all windows you can use:
^W^W
Or use motion hot keys to move in a direction (hjkl
, not arrow
keys). This, for example, goes one window to the left:
^Wl
Windows are useful when you have a big screen, or just a bunch of files you
want to see at once. For more info: :help window
Complete first match searching backwards
^P
Complete first match searching forwards
^N
Complete a filename
^X^F
Complete a token in any included files
^X^I
For example, to complete the token longvariablename
:
print lon^N
Not sure what a builtin does? No problem.
K
Not helpful for Java and some other languages, but for Perl, C, Python, and a bunch of others it's a real lifesaver.
Run an external command without leaving Vim:
:!cmd
For instance, ls
:!ls
Now let's rot13 our file:
:%!tr a-z n-za-m
Perl
:!perl -c %
Java
:!javac %
XML
:!xmllint %
The % just expands to the name of your current file
Vim is highly configurable and has a builtin language (vimscript) if you want to write plugins (of which there are many). For basic configuration, you need to know about:
:set :map :autocmd
:set
lets you enable, disable, and configure a lot of options
:set option
Enable an option if it is a boolean
:set nooption
Disable an option if it is a boolean
:set invoption
Invert an option if it is a boolean
:set option=value
Set an option to value
Autoindent will maintain a level of indentation for a new line. It's about as simple as it gets.
Hello foo bar(honk){ //stuff }
First, disable autoindent. Smartindent tries to recognize code that "should" be indented. It's right most of the time.
Try enabling smartindent and typing a Java function header:
public int myfunc(x, y){ |
After you press enter your cursor should be where the | character is.
Type some more, then close the brace. That should reduce the indentation level.
:set number
Show line numbers. (:NUM takes you to that line in a file)
:set list (see help :listchars)
Show whitespace, configure using listchars
:set nocompatible
Disable "vi compatibility" because Vim has better defaults
:set incsearch
Search while typing
:set hlsearch
Highlight search results in the file
:set shiftwidth=NUM
Set shift to NUM spaces for >> command when inserting.
:set expandtab
Use tabs instead of spaces when inserting. The number of spaces is dependent on the tabstop option.
:set tabstop=NUM
Set how many spaces to display tabs with
:set backpace=indent,eol
Enable sane backspace defaults
:set textwidth=NUM
Enable text wrapping after NUM characters
:set showcmd
Show unfinished command-mode commands
:set ruler
Put useful info at the bottom of your window
:map <C-s> :w<CR>
Maps Ctrl+s to :w+Enter. <CR> makes Vim act as if you pressed enter, and <C-s> is the notation for Ctrl+s.
The best way to make hotkeys more logical is to change them to what makes sense to you.
An autocmd is a command that executes only under a specific set of circumstances.
autocmd FileType java syntax on
Turn on syntax highlighting on the detected filetype is Java.
Auto commands are really useful, and are great for making Vim do exactly what you want.
vim has builtin syntax highlighting for perl, python, java, c, c++, ruby, bash, html, xml, and many, many others.
:set background=[dark|light]
tell vim about your background color so it can make good color choices.
:filetype on
turn on automatic filetype detection
:syntax on
turn on syntax highlighting
Vim has its own scripting language, and IBM has made an awesome Scripting the Vim editor tutorial if you want to see the insides.
More useful, though, is knowing that there are plugins for Vim that people have already written and put up on vim.org where you can search for and download them.
So just one plugin doesn't seem hard to deal with, but when you have a lot of plugins that .vim directory can get pretty cluttered.
Guess what?
There's a plugin for that!
Fortunately, there's a path to Sane Vim Plugin Management through the Vundle plugin.
First, make sure there's nothing in your .vim directory, then clone the Vundle repo with:
git clone http://github.com/gmarik/vundle.git ~/.vim/vundle.git
set nocompatible filetype off " required! set rtp+=~/.vim/vundle.git/ call vundle#rc() " Your bundles here filetype plugin indent on " required!"
And where it says " Your bundles here you can put in a list of plugins you want, just like this:
Bundle 'nerdcommenter.vim' Bundle 'tslime.vim'
Then when you run :BundleInstall
Vundle goes and finds those plugins and installs them.
Files
References
Find the slides later:
Find me later: