Skip to content

Instantly share code, notes, and snippets.

@aloispichler
Forked from awidegreen/vim_cheatsheet.md
Last active June 12, 2024 12:23
Show Gist options
  • Save aloispichler/9c0650fce17af172c36f7e8885c621c7 to your computer and use it in GitHub Desktop.
Save aloispichler/9c0650fce17af172c36f7e8885c621c7 to your computer and use it in GitHub Desktop.
Vim shortcuts

Introduction

For emacs see: https://gist.github.com/aloispichler/5e079adaacb90c1d66efa3af41cf1b96
* C-a ≙ Ctrl-a
* M-a ≙ Alt-a

ijkl vs hjkl:

  • vim, vimium (firefox) and evince use hjkl
  • separate hjkl from ↔ and ↕

Navigation

h, j, k, l      cursor navigaton
M-h, j, k, l    :<Esc> + cursor navigation
H, M, L         top/ mid/ bottom of screen
0, _, |         first column, character in screen, line
0, $, g_        last character in screen, line
*/ #            next/ previous occurance of word under the cursor

w, W, e, E, b, B  word navigation
f, F{char}, ;,  to {char}, repeat latest f/ F

gj, gk          go down/up one displayed line
gm              goto middle of the line

C-f/b           move forward/ back one screen
C-d/u           move forward/ back 1/2 a screen
C-e/ y          move screen up/ down
(, ), {, }      move to beginning/ end of this sentence/ paragraph
%               matching brackets, begin - end, if - else -end etc.

:ju[mps]        recent jumps
C-o/i           jump backward (old cursor position)/ forward
g; g,           goto older/newer position in changelist (edited position)
%               jump to matching braces
:12/ :±7        jump to line 12/ forward/backward 7 lines
7gg             goto line 7; gg goto the first line
17G             goto line 17; G goto last line

file navigation

:o[ld]          recently opened files. :e #<3 to edit file 3 there
b:ro[wse] o[ld] go to file from list
:cd %:h         change to the directory of this file
:e %:h ↹↹       edit file from the folder of the current file
:e #<{n}        edit file {n} from the list of :o[ld] files
<C-w>gf         goto file under cursor (in new tab)
gx              follow link under cursor
gd              goto definition under cursor
Tex             Tab & explore

General

u, C-r          undo, redo
:reg            list the content of all registers
"{register}     use {register} for next delete, yank or put.
"+y, "+p        yank and paste to the register ‘+’ (i.e., the clipboard; make sure xsel is installed)
:highlight      list all colors in syntax highlighting
:help index     list all commands
:help ctrl-u    help on shortcut Ctrl-u
:!<cmd>         execute shell command <cmd>
C-z, fg         suspend vim, and return (foreground)
:as, ga         ascii value of the character under the cursor
i Ctrl-v u0061  input unicode character (u+0061= a)

<Esc> or C-[    escape
C-a, C-x        increment, decrement the number under number
[count]~        switch the case of the character/ [count] characters under the cursor

Frequently used

i/ I/ a/ A      enter insert mode
r/ R            enter replacement mode
v, V            enter mode for selection
:%s/old/new/gc  replace 'old' by 'new'
ciw, ci{, ci(, ci"   change inner word, inside {, (, ", …
ciw/ diw/ viw/ siw   change/ change/ substitute/ view

y, yy, yiw      yank/copy selected region or word
"{register}y    yank/copy marked region into register <reg> (register from a-z)
c               cut selection
:j              join with next line

p/ P            paste yanked content (before)
"{register}y/ p paste yanked content in register {register} (from a-z)

d, 7dd, d/word  delete, delete 7 lines, delete until word
siw             substitute inside word

:%s/{\\alpha}/\\alpha/gc
                %(global) substitute (replace) ‘{\alpha}’ for ‘\alpha’ and global (g) and confirm (c)

Completion

i_C-x C-o       completion, omni completion
C-y             trigger (accept) coc compltetion
C-n/ p          insert next/ previous matching word
i_C-x C-]       tag completion
C-p/ C-n        previous/ next tag
i_C-x C-f       filename completion

ctags

to create tags run ctags -R . ~/Dropbox/Literatur/LiteraturAlois.bib
C-], C-Left Mouseclick    follow tag (as \cite or \ref), go to definition
C-t, C-Right Mouseclick   jump back from definition

vimtex

]]              close environment
\ref{eq:<C-x><C-o>, \cite{Don.*Knuth<c-x><c-o>
                complete references
\ll, \lk, \lc   start, stop compiling, clean auxiliary files.
\lt, \le        toggle table of content, QuickFix menu
se, sd          surrounding environment and delimiters
ic, ac          text objects with and without

Macros

q{r:register}…q record the macro … to register r
@r              execute macro r
@@              execute again

Visual block mode

<Esc>C-v        comment a block of code: enter visual block mode and select the lines
I, insert the text, e.g. %
<Esc><Esc>
vi(, vi), va(, va) etc. inner block select

Windows and tabs

C-w C-w/ C-w W  toggle windows
C-w h/ j/ k/ l  navigate between windows
C-w +/ -/ </ >  change window size

gt, gT, #gt     go to next/ previous/ tab #
gx              open link under cursor in browser

Spelling, dictionaries and folding

]s, [s          move to the next/ previous misspelled word
z=              suggest correctly spelled words for the word under the cursor
zg/ zw          add/ remove word to dictionary (good/ wrong)
zf/ zo/ zc/ zd  create/ open/ close/ delet fold
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment