Zum Inhalt

Vim Cheatsheet

Überblick

Vim ist ein hoch konfigurierbarer Texteditor, der die Erstellung und Änderung von Texten sehr effizient macht. Es ist eine verbesserte Version des vi-Editors verteilt mit den meisten UNIX-Systemen.

Hauptnavigation

Befehle der Bewegung

vim h, j, k, l # Left, down, up, right w # Next word b # Previous word e # End of word 0 # Beginning of line ^ # First non-blank character $ # End of line gg # Go to first line G # Go to last line :n # Go to line n Ctrl+f # Page down Ctrl+b # Page up Ctrl+d # Half page down Ctrl+u # Half page up_

Springen

```vim % # Jump to matching bracket * # Find next occurrence of word under cursor

# Find previous occurrence of word under cursor

n # Next search result N # Previous search result `` # Jump back to previous position '. # Jump to last edit ```_

Moden

Mode-Schaltung

vim i # Insert mode (before cursor) a # Insert mode (after cursor) I # Insert at beginning of line A # Insert at end of line o # Open new line below O # Open new line above v # Visual mode V # Visual line mode Ctrl+v # Visual block mode : # Command mode Esc # Return to normal mode_

Befehle bearbeiten

Grundbearbeitung

vim x # Delete character under cursor X # Delete character before cursor dd # Delete line D # Delete from cursor to end of line dw # Delete word d$ # Delete to end of line d0 # Delete to beginning of line yy # Copy (yank) line yw # Copy word y$ # Copy to end of line p # Paste after cursor P # Paste before cursor u # Undo Ctrl+r # Redo . # Repeat last command_

Erweiterte Bearbeitung

```vim cc # Change entire line cw # Change word c$ # Change to end of line r # Replace single character R # Replace mode s # Substitute character S # Substitute line ~ # Toggle case

       # Indent line

<< # Unindent line J # Join lines ```_

Suchen und Ersetzen

Suche

```vim /pattern # Search forward ?pattern # Search backward n # Next match N # Previous match * # Search for word under cursor (forward)

# Search for word under cursor (backward)

:noh # Clear search highlighting ```_

Ersatz

vim :s/old/new/ # Replace first occurrence in line :s/old/new/g # Replace all occurrences in line :%s/old/new/g # Replace all occurrences in file :%s/old/new/gc # Replace with confirmation :5,10s/old/new/g # Replace in lines 5-10_

Dateioperationen

Dateiverwaltung

vim :w # Save file :w filename # Save as filename :q # Quit :q! # Quit without saving :wq # Save and quit :x # Save and quit (same as :wq) ZZ # Save and quit (normal mode) ZQ # Quit without saving (normal mode) :e filename # Edit file :r filename # Read file into current buffer_

Buffer Management

vim :ls # List buffers :b n # Switch to buffer n :bn # Next buffer :bp # Previous buffer :bd # Delete buffer :split # Horizontal split :vsplit # Vertical split Ctrl+w h/j/k/l # Navigate between splits Ctrl+w w # Switch between splits_

visueller Modus

Visuelle Auswahl

vim v # Character-wise visual mode V # Line-wise visual mode Ctrl+v # Block-wise visual mode o # Move to other end of selection gv # Reselect last visual selection_

Visual Operations

```vim d # Delete selection y # Yank (copy) selection c # Change selection

        # Indent selection

``< # Unindent selection = # Auto-indent selection u # Lowercase selection U # Uppercase selection ```_

Textobjekte

Text Objektauswahl

vim iw # Inner word aw # A word (including spaces) is # Inner sentence as # A sentence ip # Inner paragraph ap # A paragraph i" # Inner quotes a" # A quotes (including quotes) i( # Inner parentheses a( # A parentheses (including parens) it # Inner tag (HTML/XML) at # A tag (HTML/XML)_

Verwendungsbeispiele

vim diw # Delete inner word ciw # Change inner word yi" # Yank text inside quotes da( # Delete text including parentheses_

Macros

Aufzeichnung und Wiedergabe von Macros

vim qa # Start recording macro in register 'a' q # Stop recording @a # Play macro from register 'a' @@ # Repeat last macro 5@a # Play macro 5 times_

Macro Beispiele

```vim

Record a macro to add semicolon at end of line

qa # Start recording A; # Go to end of line and add semicolon Esc # Return to normal mode q # Stop recording @a # Play macro ```_

Register

Register verwenden

vim "ay # Yank into register 'a' "ap # Paste from register 'a' :reg # Show all registers :reg a # Show register 'a' "0p # Paste from yank register (register 0) "+y # Yank to system clipboard "+p # Paste from system clipboard_

Zeichen

Setzen und Verwenden von Markierungen

vim ma # Set mark 'a' at current position 'a # Jump to mark 'a' `a # Jump to exact position of mark 'a' :marks # Show all marks '' # Jump to previous position '. # Jump to last edit position_

Falten

Befehle falten

vim zf # Create fold zo # Open fold zc # Close fold za # Toggle fold zR # Open all folds zM # Close all folds zd # Delete fold_

Konfiguration

Basic .vimrc Einstellungen

```vim " Enable syntax highlighting syntax on

" Show line numbers set number

" Enable mouse support set mouse=a

" Set tab width set tabstop=4 set shiftwidth=4 set expandtab

" Enable auto-indentation set autoindent set smartindent

" Show matching brackets set showmatch

" Highlight search results set hlsearch

" Incremental search set incsearch

" Case insensitive search set ignorecase set smartcase

" Enable file type detection filetype on filetype plugin on filetype indent on

" Set color scheme colorscheme desert

" Show status line set laststatus=2

" Enable clipboard set clipboard=unnamedplus ```_

Erweiterte Konfiguration

```vim " Leader key let mapleader = ","

" Custom key mappings nnoremap ``w :w nnoremap q :q nnoremap h nnoremap j nnoremap k nnoremap l

" Auto-completion set wildmenu set wildmode=list:longest

" Backup and swap files set backup set backupdir=~/.vim/backup set directory=~/.vim/swap

" Persistent undo set undofile set undodir=~/.vim/undo ```_

Plugins

Plugin Manager

```vim " Vim-Plug (add to .vimrc) call plug#begin('~/.vim/plugged') Plug 'preservim/nerdtree' Plug 'vim-airline/vim-airline' Plug 'tpope/vim-fugitive' call plug#end()

" Install plugins: :PlugInstall " Update plugins: :PlugUpdate ```_

Beliebte Plugins

```vim " File explorer Plug 'preservim/nerdtree' :NERDTree

" Status line Plug 'vim-airline/vim-airline'

" Git integration Plug 'tpope/vim-fugitive' :Git status

" Fuzzy finder Plug 'ctrlpvim/ctrlp.vim' Ctrl+p

" Auto-completion Plug 'ycm-core/YouCompleteMe'

" Syntax checking Plug 'vim-syntastic/syntastic' ```_

Kommandozeile

Ausführende Befehle

vim :help # Open help :help topic # Help on specific topic :version # Show version :set # Show all options :set option # Show specific option :set option=value # Set option value :!command # Execute shell command :r !command # Read command output into buffer_

Nützliche Befehle

vim :retab # Convert tabs to spaces :sort # Sort lines :sort u # Sort and remove duplicates :%!xxd # Hex dump :%!xxd -r # Reverse hex dump :g/pattern/d # Delete lines matching pattern :v/pattern/d # Delete lines not matching pattern_

Erweiterte Funktionen

Mehrere Dateien

vim :args *.txt # Open multiple files :next # Next file :prev # Previous file :first # First file :last # Last file :argdo %s/old/new/g # Execute command on all files_

Sitzungen

vim :mksession session.vim # Save session :source session.vim # Load session vim -S session.vim # Start with session_

Diff-Modus

vim vimdiff file1 file2 # Start diff mode :diffthis # Make current window part of diff :diffoff # Turn off diff mode ]c # Next difference [c # Previous difference do # Diff obtain (get change) dp # Diff put (put change)_

Tipps und Tricks

Produktivität Tipps

```vim

Quick file switching

Ctrl+^ # Switch to alternate file

Quick line duplication

yyp # Duplicate current line

Quick word completion

Ctrl+n # Next completion Ctrl+p # Previous completion

Quick bracket matching

% # Jump to matching bracket

Quick indentation

=G # Auto-indent from cursor to end gg=G # Auto-indent entire file ```_

Zeitsparende Befehle

```vim

Change case

guu # Lowercase line gUU # Uppercase line g~~ # Toggle case of line

Join lines with space

J # Join current line with next

Increment/decrement numbers

Ctrl+a # Increment number under cursor Ctrl+x # Decrement number under cursor

Quick substitution

& # Repeat last substitution ```_

Fehlerbehebung

Gemeinsame Themen

```vim

Stuck in insert mode

Esc # Return to normal mode

Can't save file

:w! # Force write :w filename # Save with different name

Accidental recording

q # Stop recording macro

Clear search highlighting

:noh # No highlight

Reset to defaults

:set all& # Reset all options ```_

Ressourcen

  • offizielle Dokumentation: :help_ innerhalb von Vim
  • *Vim Wiki: vim.fandom.com
  • Interaktives Tutorial: vimtutor Befehl
  • *Online-Ressourcen: vim.org