Aller au contenu

Feuille de chaleur Vim

Aperçu général

Vim est un éditeur de texte hautement configurable construit pour rendre la création et le changement de tout type de texte très efficace. C'est une version améliorée de l'éditeur vi distribué avec la plupart des systèmes UNIX.

Commandements de mouvements

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

Saut

%             # 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
```_

## Modes

### Changement de mode
```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
```_

## Édition des commandes

### Édition de base
```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

Édition avancée

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

Recherche et remplacement

Rechercher

/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

Remplacer

: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

Opérations de fichiers

Gestion de fichiers

: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

Gestion des tampons

: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

Mode visuel

Sélection visuelle

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

Opérations visuelles

d             # Delete selection
y             # Yank (copy) selection
c             # Change selection
>             # Indent selection
``<             # Unindent selection
=             # Auto-indent selection
u             # Lowercase selection
U             # Uppercase selection

Objets textuels

Sélection des objets texte

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)

Exemples d'utilisation

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

Macros

Enregistrement et lecture de Macros

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

Exemples de macro

# 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

Registres

Utilisation des registres

"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

Marques

Réglage et utilisation des marques

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

Pliage

Plier les commandes

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

Configuration

Base .vimrc Paramètres

" 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

Configuration avancée

" Leader key
let mapleader = ","

" Custom key mappings
nnoremap <leader>``w :w<CR>
nnoremap <leader>q :q<CR>
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>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

Greffons

Gestionnaires de greffons

" 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

Greffons populaires

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

Ligne de commande

Commandes ex

: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

Commandes utiles

: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

Caractéristiques avancées

Fichiers multiples

: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

Séances

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

Mode diff

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)

Conseils et astuces

Conseils en matière de productivité

# 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

Commandes d'économie de temps

# 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

Dépannage

Questions communes

# 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

Ressources

  • Documentation officielle: :help à Vim
  • Vim Wiki : vim.fandom.com
  • ** Tutoriel interactif** : vimtutor commande
  • Ressources en ligne: vim.org