This repo features my personal neovim config as well as a cheat sheet for nifty vim commands.
Let's say you want to replace Hello World
in the following text:
We are going to write a simple Hello World program...
Given that you searched for it via /Hello World
, you can simply use the
following command to change the text:
cgn
Or delete the text via:
dgn
The following will display the count, but will not change the buffer:
:%s/pattern//gn
See Tip 860 by Marc Weber on the vim wiki.
Search for foobar
in all python files in the current directory and all
subdirectories:
:noautocmd vimgrep /foobar/gj ./**/*.py
Disable autocommands to increase search speed.
Use :cw
to open the quickfix window.
See Tip 1543 by Fritzophrenic on the vim wiki.
Toggle the current fold:
za
Set mark a
at current cursor location:
ma
Jump to position (line and column) of mark a
:
`a
Delete mark a
:
:delm a
Add /e
to your search to place the cursor at the end of the match:
/foobar/e
Reselect the last block:
gv
See Tip 1584 by Metacosm on the vim wiki.
Run macro q
on all tabs (files are not saved to disk):
:tabdo normal! @q
You can also write all the changed tabs to disk in a single step:
:tabdo execute "normal! @q" | update
Depeneding on how you work with buffers you may need to choose a slightly different command:
argdo
- all files in argument listbufdo
- all bufferstabdo
- all tabswindo
- all windows in the current tab
Run the macro from register q
on each line of your selection:
:normal @q
Given that both ignorecase
and smartcase
are enabled, vim does a case
sensitive search only if the pattern contains an uppercase letter.
To explicitly search for a case sensitive pattern use the following:
/foobar\C
To replace each occurrence of abc with xyz_N where N is an ascending number (xyz_1, xyz_2, xyz_3, and so on):
:let i = 1 | %s/abc/\='xyz_' . Inc()/g
Whereas Inc()
is a custom function you may want to put in your vimrc:
function Inc(...) let result = g:i let g:i += a:0 > 0 ? a:1 : 1 return result endfunction
You can refer to match groups in the replacement expression via submatch
:
:let i = 1 | %s/\(foo\s*\)\(abc\)\(\s*bar\)/\=submatch(1) . 'xyz_' . Inc() . submatch(3)/g
Open files in tabs:
vim -p foo.py bar.py vim -p *.py
List all tabs:
:tabs
Go to next/previous tab in normal mode:
gt gT
Move a split to a new tab:
<C-W>T
Move each of the opened buffers to a new tab:
:tab sball
Move the current file relative to the its containing directory:
:Rename
Run find
and load the results into the quickfix list:
:Find
Plugin by Tim Pope. See vim-eunuch.
Mark a word for exchange:
cxiw
Replace the word under the cursor by repeating the command:
.
The same works for lines:
cxx
Or in Visual mode:
X
Clear selection:
cxc
Plugin by Tom McDonald. See vim-exchange.
Resolve merge conflicts. Open three-way diff:
:Gdiff
Jump to the next/previous hunk in a diff:
]c [c
Write the current file to the index:
:Gwrite
Commit the staged changes along with a message:
:Gcommit -m 'Create a settings json file and load it in the app'
Imagine the following scenario:
git checkout master git merge feat/user-settings-panel
Pull over a hunk from the master
:
:diffget //2
Pull over a hunk from feat/user-settings-panel
:
:diffget //3
Compare the current file with another branch:
:Gdiff feat/user-settings-panel
Plugin by Tim Pope. See vim-fugitive.
For a great tutorial on Fugitive by Drew Neil please visit vimcasts.org.
Completion:
<C-Space>
Goto assignments:
<leader>g
Goto definitions:
<leader>d
Renaming:
<leader>r
Usages:
<leader>n
Plugin by Dave Halter. See jedi-vim.
Change "Hello world!"
to 'Hello world!'
:
cs"'
Remove delimiters entirely:
ds"
Add another pair of parentheses to (Fizz Buzz)
:
ysi))
Plugin by Tim Pope. See vim-surround.
This cheat sheet is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.