HowToUseVim
This is just a start, hopefully this will get you on your way to being a vim power user
Contents
Why vim?
It works well in screen and is reasonably powerful programmer's editor. In addition, it is usually on every box in some form or another.
Minimal Custom Vimrc for Developers
Vim defaults are great in most cases, but some options need to be set. This is currently incomplete, but is a start.
version 6.0 # enable later-version vim functions set nocompatible # get rid of crufty vi compatibilites set expandtab # tabs can't be different on different systems if they don't exist syn on # enable syntax highlighting set autoindent # help with formatting set incsearch # incrementally highlight search. sort of like a suggest feature for search set hlsearch # highlights strings matching search set fo=cqrt # TODO
Here is some power user stuff can be added to the file above.
let loaded_matchparen=1 # vim 7 has an annoying highlighting parentheses matching feature. this turns it off. set laststatus=2 # displays a status line with the current file's name and position in the file set background=dark filetype on # turn file type detection on. filetype indent on # turns on file type specific indentation filetype plugin on # loads plugin for file type. syntax coloring and completion based on file type. set number # turn on line numbers set smartindent # automatically unindent closing braces, do end blocks etc. set wildmenu fun FunRuby() # start a function definition map <F5> :w<cr>:!ruby %<cr> # for command mode. map the <F5> key to: write changes to file and execute it. imap <F5> <esc>:w<cr>:!ruby %<cr> # for insert mode. map the <F5> key to: write changes to file and execute it. set shiftwidth=2 set tabstop=2 endfun autocmd FileType ruby call FunRuby() # if loaded file is a ruby source file, call FunRuby
Modal Editing
Vim is a modal editor, meaning that unless you are in insert mode characters you type will not show up in the input buffer. I cannot stress enough that unless you are actually typing something in, you should be in command mode. This is very non intuitive for beginners.
If you get stuck, jamming on the
Commands that start with : need to be finished by hitting
Quitting Vim
- :q - quit vim
File Operations
- :w - saves the file
- :wq - saves the file and quits vim
- :e
- edit filename - :e . - open a file browser
Command Mode
command mode is the default mode for vim.
- i - insert
- lets you type into the buffer. hit
when done to return to command mode
- lets you type into the buffer. hit
- u - undo
- will undo any commands. The undo buffer goes back to when you started your editing session and is really good, so it is better to stay in the habit of having an editor open for a long time and use ctrl-z and the fg command to drop to a shell
- ctrl-r - redo
- redoes something that has been undone
- ct
- change till - replace the text from here to the character with what you type. Puts you in insert mode
- cw - Change Word
- c$ - change to the end of line
Insert Mode
This is the mode you are in when you use the insert or change commands. Characters entered at the keyboard are inserted directly into the document, with automatic indentation for recognized source code constructs.
- ctrl-n - completion. Use multiple times to cycle through available completions. Can use ctags, to do project-wide completions.
Editing Multiple Documents
Since the undo buffer works better if you never exit out of vim, it is very handy to have multiple files open in one vim session and switch between them. ctrl-z to suspend vim and fg to get back into it are very handy.
- :bn - buffer next. Switch to the next buffer.
- :bp - buffer previous. Switch to the previous buffer
- \be - Buffer Explorer. Shows a list of open edit buffers, arrow keys and enter will select he buffer to switch to.
Multiple Windows
The most basic usage will be described here. All of these commands are from command mode.
- ctrl-w n - create new window, split horizontally
- ctrl-w v - create new window, split vertically
- ctrl-w w - switch window
- ctrl-w = - equalize window sizes
- :q - quit a window. If multiple windows are open, will just switch to the next window instead of exiting.
Copy & Paste
First, make a selection:
- by line - shift-v and then up / down arrows
- contiguous - v then arrow / movement keys
- block - ctrl-v then arrow keys, select a rectangular section of text (try it!)
Then, specify how to copy it:
- y - yank. Copies the selection to the paste buffer.
- d - delete. Copes the selection to the paste buffer then deletes it from where it was.
Line selection mode has some additional fun stuff, like so:
- - shift text to the right, by indent spaces
Once you have some text in your paste buffer, use these next two commands to put text back in.
- p - paste on this line
- P - Paste on the line below
Search and Replace
To search, from command mode hit / and type the first few characters to search for. Enter will end the search input, allowing you to switch between matches by bouncing on the 'n' key. This is especially handy with '.', which will repeat the last command. - will display on your screen in the lower left
- type s/search/replace/g
replacements will only happen within the selected area
ToDo
- Vertical Insert
- plugins / links / documentation
