The vimrc file ($HOME/.vimrc
on linux/mac or $HOME/_vimrc
on Windows) stores
personal configuration for vim.
These options are maximally recommended to all skill levels.
""" Essential
set mouse=a " allow use of mouse
set backspace=indent,eol,start " always allow backspace in insert mode
set nocompatible " ensure vim features are available
""" Critical
syntax on " syntax highlighting
filetype plugin indent on " detect file type
set smartcase " case insensitive search, unless you use a capital letter.
set incsearch " Starts searching while you type in your search
set hlsearch " Highlights all search instances
set autoindent " copy previous line's indent
set smartindent " increase indent after start of new block (e.g. '{')
" In command mode, first tab fills in the longest common match
" Second tab displays list. Two more tabs will iterate over that list.
set wildmode=longest,list,list,list:full
set wildmenu
""" Personal Preference
colorscheme desert " best stock colourscheme
set background=dark
set noeb vb t_vb= " disable beeping
The thing that makes vim actually good is how shortcut keys are typed in combination to form custom and powerful actions (almost like combining words into a sentence). Each keybind performs a particular role in these commands. You'll see what I mean later, but to help I've marked each keybinding with the category it falls under using the following legend:
- ๐ - Movement key. Changes the position of the text cursor or text selection.
- ๐ฅ - Action key. Actually edits text, or modifies a file, etc
- ๐ง - Mode change key. Going from normal mode/insert mode/command mode etc
- ๐ฃ - Command-line command. Not technically a keybinding, and doesn't actually interact with the special keybind grammar. But still lets you do powerful things.
- ๐ฆ - Other
- ๐ [Mouse click/scroll] - Move cursor (make sure you have the vimrc above)
- ๐ง i - insert mode
- ๐ง Escape - leave insert mode
- ๐ฃ :wq - save and quit (mnemonic: write, quit)
- ๐ฃ :q! - quit without saving (mnemonic: "quit!")
- ๐ / - search file (mnemonic: ? key) (full regex support)
- ๐ [arrow keys] - Move cursor
A 'Selection' or 'Highlight' mode, where you select/highlight the text you want to perform an action on. To activate it, just click and drag over text as you would in a GUI text editor (or press 'v'). Pressing ๐ Movement keys in this mode will change the text selection.
When in visual mode, any ๐ฅ Action key you press will instantly operate on the text
you have currently selected. For instanmce, if you press d
, your highlighted
text will be deleted.
- ๐ฅ d - delete text
- ๐ฅ y - copy text (mnemonic: 'yank')
- ๐ฅ p - paste text
- ๐ฅ u - undo last action
- ๐ฅ ctrl-r - redo last action
- ๐ฃ :s/{old}/{new} - substitute occurrences of {old} with {new}
Holding Shift and pressing a ๐ movement key reverses the direction of the movement.
Like /, ? searches text, but backwards from the cursor position, instead of forwards. Some more useful examples are in the following keybinds.
Some ๐ฅ Action keys (like 'p/P' for paste) also operate this way.
- ๐ f/F{character} - move back/forward to the next instance of the character. (mnemonic: 'find')
- ๐ n N - move to next/previous search occurence
- ๐ {number}G - move to line number (or last line if no number given)
- ๐ฅ < > - indent left/right
- ๐ % - Jump to the bracket (
()<>[]{}
) matching the one under the cursor - ๐ง o O - create an empty newline below/above the current line, and enter insert mode on it.
- ๐ ^ - move to start of current line
- ๐ $ - move to end of current line
- ๐ { } - move to the previous/next blank line after a block of text (i.e, move a paragraph up/down).
From normal mode (without pressing any keybind beforehand), pressing an action
keybind twice will perform that action on the line the cursor is currently on.
For instance, dd
will delete the entire line, and yy
will copy the entire
line.
- ๐ i{key}
- ๐ a{key}
{key} can be one of the following:
- any bracket or quote (
'"`{}[]()<>
) - inside the closest surrounding pair of chars - p - paragraph
- s - sentence
- t - tag (
<html></html>
) - w - word (up to alphanumeric only)
- W - word (including symbols, up to spaces)
- ๐ h j k l - Home row < v ^ >
- ๐ w W b B e E - Word-wise movement (follows word/Word convention instead of reverse convention)
An ๐ฅ Action key followed by a ๐ Movement key will perform that action from the current cursor postion to the postion the cursor would be after that movement.
-
๐ฅ . - repeat last ๐ฅ Action (best used from normal mode)
-
๐ฅ c - change text, i.e. delete and enter insert mode. Very powerful in combination with '.', since it includes the replaced text!
-
๐ ctrl-o ctrl-i - go back and forward between your recent movement 'jumps'
-
๐ง V ctrl-v - line-wise and column-wise visual selection modes
- ๐ t/T{character} - move forward/back to just before the next instance of the character (equivalent to f{char}โ or F{char}โถ)
- ๐ฅ r{character} - replace character under the cursor, without going into insert mode
- ๐ง a - enter insert mode after the current cursor (equivalent to
li
) - ๐ง I - enter insert mode at the start of the line (after indentation)
(equivalent to
^i
). Works great with '.' - ๐ง A - enter insert mode at the start of the line (after indentation)
(equivalent to
$a
). Works great with '.') - ๐
* #
- search for word under cursor
- ๐ฅ gu - make lowercase
- ๐ฅ gU - make uppercase
- ๐ฅ J - join next line
- ๐ฆ zt zb zz - move viewport so the current line is the top/bottom/middle line
- ๐ฆ q{letter} - record macro
- ๐ฅ @{letter} - record macro
- ๐ฆ m{letter} - set location bookmark in file
- ๐ '{letter} - move to location bookmark
- ๐ฅ !{shell-command} - run lines as input to given shell command, and replace them with the command output.
- ๐ฅ gq -- split long lines
- ๐ฃ :g/{match}/{command} - on every line with {match}, perform {action}
- ๐ฃ :v/{match}/{command} - on every line without {match}, perform {action}
- ๐ H L M - move cursor to the top/bottom/middle line (mnemonic: high/low/middle)
set scrolloff=7 # leave 7 lines of extra space above/below the maximum cursor height
# Decent font options
if has ('gui_running')
if s:MSWindows
set guifont=Consolas\ 14
elseif has("gui_photon")
set guifont=Courier\ New:s12
elseif has("gui_kde")
set guifont=Courier\ New/12/-1/5/50/0/0/0/1/0
elseif has("x11")
set guifont=-*-courier-medium-r-normal-*-*-180-*-*-m-*-*
else
set guifont=Courier_New:h12:cDEFAULT
endif
endif