Zum Inhalt

Spickzettel, Cheat sheet Vim


Verfasst: 2021-12-08, zuletzt aktualisiert 2022-02-21

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:

General

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

delete from current line to last line visible on screen
dL

delete from current cursor position up to PATTERN
d/PATTERN

display the character count of the current file
g CTRL-G

To turn off highlighting until the next search
:noh

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
e.g.:
    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
:%s/foo/bar/gc

Count number of matches of a pattern
:%s/pattern//n

Show content of registers
:reg

Insert from register e.g. m
"mp

To run your last search again
//

vimgrep

vimgrep /PATTERN/f % | copen
    vimgrep   - grep for
    /PATTERN/ - PATTERN
    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

insert

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

Folding

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

Buffer

:buffers
: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
:%bd

Split buffer vertical
ctrl+wv

Split buffer horizontal
ctrl+ws

Marks

show current marks
:marks

set mark m at current cursor location
mm

jump to line of mark m
'm

jump to position (row and column) of mark m
`m

yank text to from cursor to position of mark m
y`m

delete from current line to line of mark m
d'm

delete from current cursor position to mark m position
d`m

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

oder
:%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

conv.vim:
    :%s/ö/ö/g
    :%s/ä/ä/g
    :%s/ü/ü/g
    :%s/ß/ß/g

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.

Troubleshooting

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
"mp

Python in vim

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.

Zurück zum Seitenanfang