Skip to content

Instantly share code, notes, and snippets.

@nath1as
Last active November 8, 2021 11:44
Show Gist options
  • Save nath1as/2b186215a76b4d7871c16dad88cbe92a to your computer and use it in GitHub Desktop.
Save nath1as/2b186215a76b4d7871c16dad88cbe92a to your computer and use it in GitHub Desktop.
vim notes
# VIM
## movement mode
### cursor movement
#### basic line/char
h - move cursor left
j - move cursor down
k - move cursor up
l - move cursor right
#### words
w - start of next word
W - start of next big word
b - back a word
B - back a big word
e - end of the word
E - end of the big word
#### other
^ - first non blank symbol
g_ - last non blank symbol
% - to enclosing brackets
} - to end of paragraph
{ - to beginning of paragraph
% - to matched parenthes - ([{}])
'' - to previous jump starting location
fx - find next occurrence of character x
tx - jump to next occurrence of character x
Fx - find previous occurence of character x
Tx - jump after previous occurence of character x
; - repeat previous f, t, F or T movement
, - repeat previous f, t, F or T movement, backwards
\# - find word under cursor
\* - find word under cursor
### line movement
0 - beginning of the line
$ - end of the line
gg - jump to start line
G -jump to end line
20G - jump to line 20
20gg - jump to line 2 0
25% - jump to 25% of the document
C-g - shows where you are
'' - go back where you were before line movement
### screen movement
H - jump to beginning of the screen
M - jump to the middle of the screen
L - jump to end of the screen
C-d - jump down 1/4 of a screen
C-u - jump up 1/4 of a screen
C-F - jump up a screen
C-B - jump down a screen
zz - current line to the middle of the screen
zt - current line to the top of the screen
zb - current line to the bottom of the screen
## insert mode
i - insert before the cursor
I - insert at the beginning of the line
a - insert (append) after the cursor
A - insert (append) at the end of the line
o - append (open) a new line below the current line
O - append (open) a new line above the current line
ea - insert (append) at the end of the word
Esc - exit insert mode
20i-<Esc> - insert 20 -
8i<Enter><Esc> - insert 8 new lines
## edit
r - replace a charachter
R - replace line with insert
J - join line below with the line
cw - change word
cc - change line
s - delete and go to insert mode
C - change to the end of the line
. - repeat
xp - transpose two letters
C-a - will increment the number right of the cursor on the same line
## undo
u - undo
U - undo whole line
C-r - redo
:earlier - earlier + time (10m, 2h)
:ea 10m
:later - later
:lat
g+ - forward on undo branch
g- - backward on undo branch
## copy paste
y -yank
yy -yank (copy) a line
d -delete (cut)
dd - delete a line
D - delete to the end of the line
p - paste after cursor
P - paste before cursor
x - delete a charachter
## objects
It's better to use objects than motions, because its easier to repeat with . command.
i - inside
a - around
[command]iw -> inner word
[command]it -> inner tag
[command]i" -> inner quotes
[command]ip -> inner paragraph
[command]is -> inner sentence
## visual mode
v - start visual mode
V - start line visual mode
C-v - start block visual mode
O - move to other corner of a block
:normal A; - appends ; to all selected lines
a - mark
aw - mark a word
ab - a block with ()
aB - a block with {}
ib - inner block with ()
ivB - inner block with {}
> - shift text right
< - shift text left
~ - switch case
## substitution
:s/replace/replacement/g - changes all replace to replacement in the current line
:6,11s/replace/replacement/g - change all replace to replacement in lines 6 to including 11
:%s/replace/replacement/g - changes all replace to replacement in the current file
### substitution flags
c - confirm each
g - replace all
i - ignore case
I - don't ignore case
## marks
ma - mark location as a
`a - jump to mark location
'a - jump to mark location row
'' - jump to prvious location
## registers
- The unnamed register "" (default)
### types
- The unnamed register "" (default)
- Ten numbered registers "0 to "9
- The small delete register "-
- Twenty-six named registers "a to "z or "A to "Z
a and A point to the same register, uppercase letter appends, lowercase overwrites
- Four read-only registers ":, "., "% and "#
- The expression register "=
- The selection and drop registers "*, "+ and "~
- The black hole register "_
- Last search pattern register "/
### use
:registers - list rgisters in use
"add - delete and copy to the named register a
"ayy - yank and copy to the named register a
"ax - cut to the named register a
"ac - change to the named register a
"as - substitute to the named register a
"0p - paste the last yanked text
"+p - paste system clipboard (ctrl-c)
"*p - paste system primary (selected text)
"+3yy - yank lines 1 to 3 to the system register
## buffers
a buffer is the in-memory text of a file
:ls - list of buffers
:5b - switch to buffer 5
:bn - next buffer
:bp - previous buffer
:bd - delete buffer
## windows
a window is a viewport on a buffer
vim -O file1 file2 file3 - open three windows split vertically
vim -o file1 file2 file3 - open three windows split horizontally
:sp - split window horizontally
:vs - split window vertically
:20sp - horizontal split of 20 lines
C-w h - switch to window on the left
C-w j - switch to window on the bottom
C-w k - switch to window on the top
C-w l - switch to window on the right
C-w r - rotate
## tabs
tab is a collection of windows
## macros
qa - start recording macro a
q - stop recording macro
@a - play macro a
@@ - repeat last macro
qaq - empty register a
read file from shell
:r!
output file to shell
:!
## ctags
## exit
:w write
:q quit
:wq or ZZ write & quit
:q! or ZQ quit & discard
:x save & quit
## map
:map
:noremap
:unmap
## folding
zf5j - fold lin with five following lines
## custom commands
surround
action s <thingtosubsittute> <thingtosusbsitutewith>
cs"' - change " to '
cs'<q> - change ' to <q> tags
cst' - change tag to '
ysiw' - add surrounding ''
## resources:
https://sanctum.geek.nz/arabesque/series/unix-as-ide/
https://stackoverflow.com/questions/1218390/what-is-your-most-productive-shortcut-with-vim/1220118#1220118
https://wikimatze.de/vimtex-the-perfect-tool-for-working-with-tex-and-vim/
https://jdhao.github.io/2019/03/28/nifty_nvim_techniques_s1/
https://jdhao.github.io/2019/04/17/nifty_nvim_techniques_s2/
https://jdhao.github.io/2019/05/14/nifty_nvim_techniques_s3/
https://vimways.org
https://www.vimfromscratch.com/articles/spell-and-grammar-vim/
www.vimfromscratch.com
## plugins
https://github.com/rhysd/devdocs.vim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment