Updated: 2021-11-24 21:06:08
I finally (2007) bit the bullet and commited to learning a real text editor. Having muddled on and off with
vi since the late 90s, decided on Vim. Once you break through its initial, steep, learning curve its truly life changing. Level up and become a text surgeon today.
Vim is incredibly customisable. Its important to take the time to craft your own
:options to understand the various levers you can pull to make Vim your editor. Almost 15 years later, I’m still constantly fine tuning things.
- Object Selection
- The Edit (e) Command
- Spell checking
- Sudo Save
- Normalise line endings
- Setup behind a proxy
Vim has brilliant built-in help. Its only one
:h operator or
:h cmdnormal mode cmd help
:h i_cmdinsert mode cmd help
:h v_cmdvisual mode cmd help
:h c_cmdcommand line cmd help
||filter through a program|
:h operator for more. All of these support being combined with a motion (or a visual mode selection). Some examples:
- gUaw - make a word shout case
- dap - delete entire paragraph
- g?ap - ROT13 encode paragraph
- gUgU - shout case entire line (factoid: when two operators are invoked in duplicate, applies to current line)
Learning object selectors is one of the best ways of becoming more efficient with Vim. Commands that start with
i select inner objects without white space, and thus always select less text than their
a equivalents. When you discover these can be paired with operators (discussed above), life changing, e.g.
daw delete a word,
gUis uppercase inner sentence, and so on.
||tag block, as in XML tags
||inner tag block|
||quoted string including back ticks|
||quoted string including back ticks|
Leverage the built-in windows manager, which can do splits to view multiple files at the same time.
ctrl+w o- close all windows other than the active one.
ctrl+w x- exchange active window with the next one.
ctrl+w c- close the current window.
ctrl+w r- rotate windows clockwise (or counter clockwise).
The Edit (e) Command⌗
Vim’s built-in edit command, will present you with a nice file system explorer, for example
:e . to present the current working directory.
Makes line numbering relative. So good! Makes it fast to figure out how many lines up or down you need to move, to get to the line you want. Example,
14j to jump 14 lines down.
2 I usually clone my `scripts` git repo straight into my home 1 ¬ 13 ln -nfs ~/git/scripts/linux/vim/vimrc ~/.vimrc¬ 1 ln -nfs ~/git/scripts/linux/vim ~/.vim¬ 2 ¬ 3 Vim has brilliant built-in help. Its only one `:help` away.
<C>r= in insert mode, can do quick calculations are spit the out into the buffer.
<C>r=16*4<CR> will output 128 where the cursor is currently located.
Built in spell checker, enable with
]sjump to next error
[sjump to previous error
z=suggest corrections for current word
zgadd word to dictionary
zwremove word from dictionary
zwfor current word
Editing a file, but don’t have privileges to save.
:w !sudo tee %
:w writes to
sudo tee %.
tee flows the output of the file write to
%, the name of the current file. I have a handy key binding
w!! to do this:
cmap w!! w !sudo tee %
Vim records the location before and after making a jump.
:jumpsshow jump list
||Jump to line|
||Jump to matching parenthesis|
||Jump to prev/next sentence|
||Jump to prev/next paragraph|
||Jump to top/middle/bottom of screen|
||Jump to file name under cursor|
||Jump to definition of keyword under cursor|
||Jump to mark|
Handy named memory slots, using
:registers to list them.
""the unnamed register, general dumping register
"0the yank register
When in insert mode use
<C-r><register> to paste in a register content on the cursor position, such as
<C-r>" for the unnamed register or
<C-r>0 for the yank register.
To record, hit
q<register>. For example to use the
qa, followed by the sequence of actions, finalise recording with another
To edit a macro, is easy, its just stored in the corresponding register. For example a macro recorded with
qa is stored into the
- Paste the macro into the buffer with
- Edit it as needed
- Yank it back into the register
Normalise line endings⌗
#!/bin/bash no such file or directory error message. Shebang is busted likely due to encoding problems.
$ head -1 <your_file> | od -c
0000000 # ! / b i n / b a s h \n
Vim to the rescue:
vim <your_file> :set ff=unix :set nobomb :wq
While core Vim functionality is like a rock, changing rarely, the plugin eco-system is where you can make Vim level up to doing tasks you commonly do with it. Consequently plugin selection can be quite personal based on specific langs one works with.
The Vim community seems obsessed with writing new plug-in managers, there’s quite a bit of choice and tradeoffs, I have used a few in the past (Pathogen, Vundle) and as of 2020 have settled with vim-plug, which balances minimalism and functionality nicely.
Coming from bloated power tool IDE’s like awesome JetBrains tools, in order to be remotely productive, I need to be able to efficiency locate and jump between files within a large code base.
My #1 fav.
Ditch NERDTree, and instead surface the awesome nnn terminal file manager within Vim, genius.
<leader>nstart nnn in window
An LSP (Language Server Protocol) provides deep language specific knowledge, supporting functionality such as autocompletion, linting and syntax highlighting. It is editor agnostic and underpins other editors such as vscode.
The nvim-lspconfig plugin provides common configuration for many servers.
Once the client plugin is done, install a language server.
- Make sure
goplsis installed and on the PATH
go install golang.org/x/tools/gopls@latest
init.vimto include a supported client lspconfig
- The LSP is called rls
- Install with
rustup component add rls rust-analysis rust-src
init.vimto include a supported client lspconfig
Brilliant fuzzy finder. Picker sources include a range of file pickers (e.g. git files, find files, live grep), Vim pickers (e.g. buffers, tags), LSP pickers (e.g. symbols, references, actions) and Git pickers (commits, branches).
rg is installed
nnoremap <leader>ff <cmd>Telescope find_files<cr> nnoremap <leader>fg <cmd>Telescope live_grep<cr> nnoremap <leader>fb <cmd>Telescope buffers<cr> nnoremap <leader>fh <cmd>Telescope help_tags<cr>
||Next/previous (in normal mode)|
||Select High/Middle/Low (in normal mode)|
|‘gg/G’||Select the first/last item (in normal mode)|
||Go to file selection as a split|
||Go to file selection as a vsplit|
||Go to a file in a new tab|
||Scroll up in preview window|
||Scroll down in preview window|
||Show mappings for picker actions (insert mode)|
||Show mappings for picker actions (normal mode)|
||Close telescope (in normal mode)|
||Toggle selection and move to next selection|
||Toggle selection and move to prev selection|
||Send all items not filtered to quickfixlist (qflist)|
||Send all selected items to qflist|
The crown jewel of Fugitive is
:Git (or just
:G), which calls any
arbitrary Git command. If you know how to use Git at the command line, you
know how to use
:Git. It’s vaguely akin to
:!git but with numerous
- The default behavior is to directly echo the command’s output. Quiet
:Git addavoid the dreaded “Press ENTER or type command to continue” prompt.
:Git rebase -i, and other commands that invoke an editor do their editing in the current Vim instance.
:Git log, and other verbose, paginated commands have their output loaded into a temporary buffer. Force this behavior for any command with
:Git blameuses a temporary buffer with maps for additional triage. Press enter on a line to view the commit where the line changed, or
g?to see other available maps. Omit the filename argument and the currently edited file will be blamed in a vertical, scroll-bound split.
:Git difftoolload their changesets into the quickfix list.
- Called with no arguments,
:Gitopens a summary window with dirty files and unpushed and unpulled commits. Press
g?to bring up a list of maps for numerous operations including diffing, staging, committing, rebasing, and stashing.
- This command (along with all other commands) always uses the current buffer’s repository, so you don’t need to worry about the current working directory.
- View any blob, tree, commit, or tag in the repository with
:Gsplit, etc.). For example,
:Gedit HEAD~3:%loads the current file as it existed 3 commits ago.
:Gvdiffsplit) brings up the staged version of the file side by side with the working tree version. Use Vim’s diff handling capabilities to apply changes to the staged version, and write that buffer to stage the changes. You can also give an arbitrary
:Geditargument to diff against older versions of the file.
:Greadis a variant of
git checkout -- filenamethat operates on the buffer rather than the file itself. This means you can use
uto undo it and you never get any warnings about the file changing outside Vim.
:Gwritewrites to both the work tree and index versions of a file, making it like
git addwhen called from a work tree file and like
git checkoutwhen called from the index or a blob in history.
:lgrepfor the same.
git mvon the current file and changes the buffer name to match.
:GRenamedoes the same with a destination filename relative to the current file’s directory.
git rmon the current file and simultaneously deletes the buffer.
:GRemovedoes the same but leaves the (now empty) buffer open.
Surround chunks of text with quotes or tags.
ysiw"surround word with double quotes
v$S"surround visual selection with double quotes
vipS<p>surround paragraph with
cs"'change double quotes to single quotes
Make a table of contents inferred from markdown heading tags.
OCD much? This helps to line up code based on a particular symbol.
vipga=visual select code block, align all
=symbols by padding out
Smart commenting based on the file type.
gcccomment current line
gc<motion>comment motion based selection e.g.
gcapfor paragraph selection
Vim motioning on steroids! Creates micro jumps based on a search pattern you provide, and dims down the buffer with each highlight getting a unique id you can hit.
<leader><leader>wbeginning of all words
ocharacters going forward
<leader><leader>tomotion before (till)
<leader><leader>njump to latest
Utilities to highlight and remove redundant use of whitespace.
<leader>prun the current buffer through prettier
Colorise hex codes in the buffer based on CSS standards.
Setup behind a proxy⌗
Getting my Vim setup on a corporate network.
TODO: Investigate containerising it.
git config --global http.proxy http://proxy.evilcorp.com:8080
- nnn.vim: nnn
- prettier: node v10+
- denite: python3, pip3 install –user pynvim
- node v10+
yarn config set proxy http://proxy.evilcorp.com:8080
yarn config set https-proxy http://proxy.evilcorp.com:8080
- Smash into Vim the awesome PeepCode screencast that help me break through the learning curve back in 2010.
- Vim Cheat Sheet good quick reference.
- A guide to modern Web Development with (Neo)vim convinced me that coc and denite are really the two power plugins needed, integration with ripgrep and LSP (lang server protocol) for autocompletion is unreal.
- How I’m still not using GUIs in 2019: A guide to the terminal excitement around (neo)Vim, some good basic tips, I recommend using
ycmfor autocompletion, which integrates with the VSCode autocomplete engine.