Zum Inhalt

Spickzettel, Cheat sheet Vim

Verfasst: 2021-12-08, zuletzt aktualisiert 2022-12-07

Great resource for vim and regex: Best of Vim Tips, zzapper 16 Years of Vi + 10+ years of Vim and still learning: http://zzapper.co.uk/vimtips.html

How to learn vim

:help vimtutor

Matthieu Cneude, The Valuable Dev:


open file todo.md and jump to line 39
vim +39 todo.md

delete from current line to last line visible on screen

delete from current cursor position up to PATTERN

display the character count of the current file

To turn off highlighting until the next search

Indent in insert mode
Indent: ctrl+t
Unindent: ctrl+d

Movement command in insert mode e.g. to go forward one word type 
ctrl+o w

jump to next / previous empty line
} / {

cancel auto completion
ctrl + e

Pipe result to vim
    rg -i PATTERN | vim -
    python abr.py | vim -

Paste clipboard content while in insert mode 
(depends on OS and compile options listed :version)
ctrl+r *
ctrl+r "

send current line to vim terminal
1. copy current line with yy
2. :term
3. ctrl+w ""

Substiute with confirmation

Count number of matches of a pattern

Show content of registers

Insert from register e.g. m

To run your last search again


vimgrep /PATTERN/f % | copen
    vimgrep   - grep for
    f         - with fuzzy mode
    %         - in current file
    | copen   - open quickfix window for results

Special chars

search and replace

Cursor over char, typing ga to identify what it is
Use \%u pattern to search for the four digits hex
Use %s/\%ufb01//gn to remove fb01


To insert a Unicode character while in insert mode, type ctrl+vuxxxx


za toggle fold under cursor

zR open all folds

zM close all folds

Mapping in vimrc to toggle a fold when cursor is on the fold
nmap <space> zA


:Buffers (if fzf plugin is used )

jump between last and current buffer
ctrl+6 or :b#

alle Buffer schließen, bis auf die die noch nicht gespeichert sind

Split buffer vertical

Split buffer horizontal


show current marks

set mark m at current cursor location

jump to line of mark m

jump to position (row and column) of mark m

yank text to from cursor to position of mark m

delete from current line to line of mark m

delete from current cursor position to mark m position

Export diff as html

vim -d old.txt new.txt -c TOhtml -c "w! diff.html" -c q! -c q! -c q!

Macro execution

Source: https://stackoverflow.com/questions/390174/in-vim-how-do-i-apply-a-macro-to-a-set-of-lines

Execute the macro stored in register a on lines 5 through 10.
:5,10norm! @a

Execute the macro stored in register a on lines 5 through the end of the file.
:5,$norm! @a

Execute the macro stored in register a on all lines.
:%norm! @a

:%normal @a

Execute the macro store in register a on all lines matching pattern.
:g/pattern/norm! @a

To execute the macro on visually selected lines, press V and the j or k 
until the desired region is selected. Then type :norm! @a and observe 
the that following input line is shown.
:'<,'>norm! @a

Creating scripts

vim -w script.vim

Additional info: https://stackoverflow.com/questions/3981535/using-the-w-option-of-vim

Convert German Umlaute


in vimrc
command! CONV :so d:\vim\vimscripts\conv.vim

show defined key mappings

redirect output to vim_keys.txt
:redir! > vim_keys.txt
:silent verbose map
:redir END

If you just want to see what mappings you have that are prefixed by a 
certain key, you can do
:map <key> 
to list them all.

Using Languagetool without plugins

update 2022-12-07

Very very, crude way!

new | r! java -Dfile.encoding=UTF-8 -jar d:\apps\LanguageTool-5.1\languagetool-commandline.jar -c utf-8 -d WHITESPACE_RULE,EN_QUOTES -l de-DE
  1. copy the above line
  2. : ctrl + r * followed by ctrl + r "


Info: https://vi.stackexchange.com/questions/2003/how-do-i-debug-my-vimrc-file

vim -u NONE -U NONE -N

Capture ex command output to buffer e.g. :version

:redir @m | silent version | redir END

Python in vim

Check if Python and Vim are the same bit (64), not mixed!

Python dependencies and Anaconda on Win OS
chechk if `pythonhome=c:\Anaconda3` is set

check if defined in vimrc
let &pythonthreedll = 'C:\Anaconda3\python36.dll'

in vim
:echo has('python3')

vim --startuptime perf

my plugins

For years I didn't know what a plugin is nor that they exist. Currently I use:

Plug 'https://github.com/majutsushi/tagbar'
Plug 'https://github.com/godlygeek/tabular'
Plug 'https://github.com/tpope/vim-surround'
Plug 'https://github.com/vim-scripts/CaptureClipboard'
Plug 'https://github.com/inkarkat/vim-ingo-library'
Plug 'https://github.com/vim-scripts/VisIncr'
Plug 'https://github.com/tpope/vim-ragtag'
Plug 'https://github.com/tpope/vim-repeat'
Plug 'https://github.com/vim-scripts/zoom.vim'
Plug 'https://github.com/justinmk/vim-sneak'
Plug 'https://github.com/ntpeters/vim-better-whitespace'
Plug 'mbbill/undotree'
Plug 'https://github.com/unblevable/quick-scope'
Plug 'https://github.com/junegunn/rainbow_parentheses.vim'
Plug 'https://github.com/junegunn/fzf.vim'
Plug 'junegunn/fzf'
Plug 'https://github.com/flazz/vim-colorschemes'
Plug 'https://github.com/adelarsq/vim-matchit'
Plug 'luochen1990/rainbow'
Plug 'https://github.com/machakann/vim-highlightedyank'
Plug 'https://github.com/junegunn/limelight.vim'
Plug 'https://github.com/maralla/completor.vim'
Plug 'davidhalter/jedi-vim'

plugins to check out

Vim plugin that provides additional text objects https://github.com/wellle/targets.vim

Set of operators and textobjects to search/select/edit sandwiched texts. https://github.com/machakann/vim-sandwich

Vim plugin: Create your own text objects https://github.com/kana/vim-textobj-user

Why vim and my way to vim

Why vim, because it works for me.

If someone asks me for an editor recommendation, I will always recommend Vim. That doesn't mean it's the best choice for everyone. Try it out. Test VSCode, it's more an IDE, Emacs, Atom, Sublime, UltraEdit, neovim etc. Find out what is the best tool for to getting things done and stick with it and learn.

Vim is the only constant in the last > 25 years of my IT project life.

The first time I used Vim was in a remote session via telnet! on a Windows 95 machine. I was watching a developer converting our ideas into Perl code. I noticed a typo in the text (not in the code) which I tried to change during a short break of the developer. Unfortunately the editor only blinked and nothing happened. I thought the editor had hung up (Windows 95 + ISDN + Telnet session on a Linux machine was not the most stable combo)... and after a short query if the code was saved I quit Vim for the first time with Alt+F4, no joke.

At that time about 1998 I used jed and also already PFE and had nothing to do with Linux yet.

At some point I ordered a Linux distribution. It came on >30 disks and was a Debian. After the installation I wanted to make some settings and there it was again Vim. If I remember right there was also pico or nano in the distribution. But if Vim was included by default the editor couldn't be that bad.

The developer, thanks Achim, gave me some tips how to learn Vim and that it is a modal editor. From then on Vim was my constant companion, 90% GVim and 10% Vim, it was 1998 and Vim 5.x - still learning and discovering new ways to edit.