These notes are based on VimCasts
1. Show invisibles
Add keybinding to toggle between showing visible and non-visible characters
" Shortcut to rapidly toggle `set list` nmap <leader>l :set list!<CR>
To change to use more standard keybindings
" Use the same symbols as TextMate for tabstops and EOLs set listchars=tab:▸\ ,eol:¬
Can update colour schemes to make these more discreet
2. Tabs and Spaces
- Tabstop Specifies the width of a tab character
- Expandtab When enabled, uses spaces instead of tabs
- Softtabstop Fine tunes the amount of whitespace to be inserted
Shiftwidth the amount of whitespace to insert or remove using the indentation commands
Using spaces ensure that
softtabstop == shiftwidth
3. Whitespace preferences and filetypes
- Can change whitespace settings for a particular filetype using
autocmd FileType <filename>.
For example, make requires tabs to indent instead of spaces so can use
autocmd FileType make setlocal ts=8 sts=8 sw=8 noexpandtab
setlocalensures that whatever changes are only made to the open buffer and not globally.
To enable filetype detection add
filetype onto the
if has("autocmd")so that on a vim without autocmd support, the FileType commands will be skipped
To treat a specific filetype as another use the
setfiletypecommand. For example,
autocmd BufNewFile,BufRead *.rss setfiletype xml
5. Indentation Commands
- Use the angle bracket keys to indent.
- In normal mode use
>>to indent the current line. Prepend this with a count to indent a number of lines
- In visual mode use a single '>' to indent the visual selection. This will drop you back out of visual mode. Use the
.operator to repeat the indentation and
uto undo the indentation.
==to auto-indent the current line (
=in visual mode)
- These commands can be used with motions e.g.
=i}will auto-indent the current inner block.
=Gwill auto-indent until the end of the file.
6. Working with Buffers
:ls- show the buffer list
:bn- open the next buffer
:bp- open the previous buffer
Ctrl-^- switch to the alternate file (displayed with the # in ls)
+sign in ls means that the buffer has been modified
- A hidden buffer has unsaved changed and is not loaded in a window
:wsaves the buffer
:e!reverts any changes
:q!quits Vim and discards any changes in any buffers.
7. Working with Windows
- We can split the current window and open the same file in the new window. This gives us two views into the same file.
Ctrl-w ssplit the window horizontally
Ctrl-w vsplit the window vertically
- We can also open a different file in the new window
:sp filenameopen filename in a new horizontal split
:vsp filenameopen filename in a new vertical split
Closing split windows
:q[uit]closes the current window
:on[ly]closes all the other windows except for the currently active one
Navigating between windows
Ctrl-w wcycle between the open windows
Ctrl-w hmove to the window on the left
Ctrl-w jmove to the window below
Ctrl-w kmove to the window above
Ctrl-w lmove to the window on the right
8. Working with tabs
In Vim tabs are used to have different layouts for viewing buffers. The same buffer can be open in multiple tabs.
:tabedit filenameopens the file in a new tab
Ctrl-W Tmoves the current split window to its own tab
:tabclosecloses the current tab page and all its windows
:tabonlycloses all other tabs except the current tab
Navigating between tabs
gtmove to the next tab
gTmove to the previous tab
<number>gtmove to the specific tab number. Note that tabs are numbered from 0.
:tabmovemove the tab to the end.
:tabmove <number>move the tab to the specified tab position.