Vim

Un article de MonWiki.

Image:vim_header.gif

Editeur de texte indispensable (Mac/Unix/PC).

Site web : http://www.vim.org/

Sommaire

Commandes utiles

Commande Description
p Insère après. P insère avant, gP insère avant et place le curseur après l'insertion.
'. Retourne à la dernière modification.
'' Retourne à la position d'avant le dernier saut.
d0 Supprime jusqu'au début de la ligne.
D Équivalent à d$.
gd Va à la définition du mot sous le curseur. gD : idem sur des morceaux de mots.
gJ Supprime un retour à la ligne. J supprime l'indentation et insère jusqu'à deux espaces.
CTRL-V{motion}I{text}<Esc> Insère une chaîne avant une sélection. I peut être remplacé par A (après la sélection) et s (remplace la sélection).
[count]CTRL-A Ajoute <count> au nombre sous le curseur (CTRL-X pour soustraire).
ggyG Copie toutes les lignes du document (va au début et copie jusqu'à la fin).
={motion} Indente une zone (== indente une ligne, =a{ un bloc, =ap un paragraphe).
gg=G Indente tout le fichier.
gq{motion} Formate une zone (gqq indente une ligne, etc.)
gu{motion} Transforme en basse casse (gU transforme en haute casse).
. Reproduit les dernières opérations effectuées en mode insertion (voir aussi i_CTRL-A).
q{0-9a-z"%#:-=.} Commence l'enregistrement d'une macro dans un registre. q arrête l'enregistrement, @{0-9a-z"%#:-=.} exécute la macro. Exemple : qaYp<CTRL-A>q copie la ligne, la colle en dessous, incrémente le nombre. Appeler avec [count]@a.
% Trouve la parenthèse / le crochet correspondant.
i_CTRL-K {digraph} Insère un digraphe.
i_CTRL-V {digits} Insère un caractère définit par son numéro.
i_CTRL-O {command} Exécute la commande sans sortir du mode insertion. Exemple : CTRL-O "g3dw - This deletes up to the third word into register g.
i_CTRL-A Insère le texte de l'insertion précédente.
i_CTRL-Y (i_CTRL-E) Copie le caractère au dessus (au dessous).
i_CTRL-T (i_CTRL-D) Indente la ligne vers la droite (vers la gauche).
i_Ctrl-X Ctrl-L Complétion automatique de lignes.
c_CTRL-R{0-9a-z"%#:-=.} Insère le contenu d'un registre.
c_Ctrl-R Ctrl-W Insère le mot sous le curseur dans la ligne de commande.
c_Ctrl-Y Copie la sélection sans mode dans le presse-papier (si une sélection sans mode est en cours).
 :tab ball Ouvre chaque buffer de la fenêtre en cours dans un nouveau tab.
 :digraphs Affiche les caractères absents du clavier.
 :cd %:h Se déplace vers le répertoire du fichier en cours d'édition.
 :vert diffsplit {file} Ouvre un fichier en mode diff.
 :lh[elpgrep] {pattern} Un grep dans les fichiers d'aide.
 :TOhtml Converti un texte coloré syntaxiquement en html (pour insérer tel quel sur le Web ou dans un Wiki).
 :[line]pu[t] [x] Put the text [from register x] after [line] (default current line).
 :dis Affiche le contenu des registres.
 :[range]sor[t] Trie les lignes. sort i indépendant de la casse, sort n utilise le premier chiffre de la ligne.
 :set fo+=a Active le formatage automatique (au sens gq). :set fo-=a pour le désactiver. Voir 'formatoptions'.

Astuces


Commande Commentaire
 :%s/\n\+/^M/ Supprime les retours à la ligne consécutifs. Taper Ctrl-V<Enter> pour avoir ^M.
NB : \n dans le pattern, ^M dans le remplacement.

Outils

MiniBufExplorer.vim
http://vim.sourceforge.net/scripts/script.php?script_id=159
LaTeX-Suite
http://vim-latex.sourceforge.net/ (cf. LaTeX)
DirDiff
http://www.vim.org/scripts/script.php?script_id=102

Fichiers .vimrc


sra

"============
"=  Plugins
"============

"====================
"=  MiniBufExplorer

"let g:miniBufExplMapWindowNavVim = 1
"let g:miniBufExplMapWindowNavArrows = 1
"let g:miniBufExplMapCTabSwitchBufs = 1 
"let g:miniBufExplModSelTarget = 1
"let g:miniBufExplUseSingleClick = 1

"================
"=  LaTeX-Suite

let g:Tex_DefaultTargetFormat = 'pdf'
"let g:Tex_CompileRule_dvi = 'latex --interaction=nonstopmode "$*"'
"let g:Tex_CompileRule_ps = 'dvips -Ppdf -o "$*.ps" "$*.dvi"'
"let g:Tex_CompileRule_pdf = 'ps2pdf "$*.ps"'

"   Mac only
if has("macunix")
    let g:Tex_ViewRuleComplete_pdf = 'open -a Preview $*.pdf'
endif

""""
"   TagList
let Tlist_Show_One_File = 1
let Tlist_Sort_Type = "name"

"===============
"= User prefs
"===============

set autoindent
set backspace=2
set backup
set backupdir=~/Documents/Tmp/Vim
set directory=~/Documents/Tmp/Vim
set expandtab
set gdefault
set guioptions-=T       " Turn off useless toolbar
set hlsearch
set ignorecase
set incsearch
"set infercase
set laststatus=2        " Always want statusline
set lazyredraw          " Don't display macro steps
set linebreak
set nocompatible
set nowrap
set ruler
set scrolloff=1
set shiftwidth=4
set smartindent
set smarttab
set softtabstop=4
set spelllang=fr
set textwidth=0
set wildmenu
set winminheight=0
if exists('+shellslash')
    set shellslash
endif
"if has( 'win32' )
"    set shell=c:\WINDOWS\system32\cmd.exe /c
"endif
syntax on
filetype plugin indent on
colorscheme desert

"========
"= Maps

map  <F1>    :setlocal invhlsearch<CR>
imap <F1>    <Esc><F1>gi

map  <F2>    :b <C-R><C-W>
imap <F2>    <Esc><F2>

map  <F3>    :vimgrep/<C-R><C-W>/j
imap <F3>    <Esc><F3>

"map  <F5>    :nmap ,t :!(cd %:p:h;ctags *.lua)&<CR>
map  <F5>    :setlocal spelllang=en<CR>
imap <F5>    <Esc><F5>gi

map  <F6>    :setlocal spelllang=fr<CR>
imap <F6>    <Esc><F6>gi

map  <F7>    :setlocal invspell<CR>
imap <F7>    <Esc><F7>gi

map  <F8>    :silent !BuildConsole d:\\Prod\\whisky\Whisky.sln /CFG="OptimizedDebug\|Win32" /PRJ="Whisky" /OPENMONITOR<CR>
imap <F8>    <Esc><F8>gi

map  <F12>   :TlistToggle<CR>
imap <F12>   <Esc><F12>gi


"==========
"=  Fonts

set gfn=Dina:h10:cDEFAULT,\ Monaco:h10:cDEFAULT,\ Fixedsys:h9:cDEFAULT,\ Courier:h12:cDEFAULT
if exists("+antialias")
    set noantialias
endif


"==================
"=  Auto commands
"==================

augroup SRA
    autocmd!
    try
        "   Open a location window after a quickfix command
	autocmd QuickFixCmdPost * :botright cwindow 5
    catch
    endtry
augroup END

"==================
"=  Functions
"==================

"========
"=  SVN

function! <SID>SVNDo( what, where )

    let where = expand( a:where )
    if "" == where
        let where = expand( "%" )
    endif

    let result = system( 'C:\Progra~1\TortoiseSVN\bin\TortoiseProc.exe /command:'.a:what.'   /path:"' . where . '" /notempfile' )
"    if 0 != v:shell_error
"        echohl ErrorMsg
"        echo 'Error dealing with svn ' . a:what . ' command, on ' . a:where
"        echo result
"        echohl none
"    endif
endfunction

command! SVNLog call <SID>SVNDo( "log", "%" )
command! SVNDiff call <SID>SVNDo( "diff", "%" )
command! SVNUpdate call <SID>SVNDo( "update", "%" )
command! SVNCommit call <SID>SVNDo( "commit", "%" )

"========
"=  Lua

function! <SID>LuaCheckSyntax()
    cexpr substitute( system( 'luac -p '. expand( '%:p' ) ), '^luac: ', ,  )
endfunction

command! LuaCheckSyntax call <SID>CheckLuaSyntax()

"==========
"=  Xalan

function! <SID>XalanTransform()
    let cmd = 'silent .!xalan -a "'. expand( '%' ) . '"'
    new
    exec cmd
    setlocal nomodified
    setlocal nomodifiable
    set filetype=xml
endfunction

command! XalanTransform call <SID>XalanTransform()

" Added (fdy) helptag generation
" 
" function <SID>AutoGenerateHelp()
"     if expand( '%:h' ) == ''
"         helptag '.'
"     else
"         helptag '%:h'
"     endif
" endfunction
" 
" command AutoGenerateHelp call <SID>AutoGenerateHelp()
" 
" autocmd BufWrite *.txt AutoGenerateHelp

vimrc par défaut sous win

set nocompatible
source $VIMRUNTIME/vimrc_example.vim
source $VIMRUNTIME/mswin.vim
behave mswin

set diffexpr=MyDiff()
function MyDiff()
  let opt = '-a --binary '
  if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
  if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
  let arg1 = v:fname_in
  if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
  let arg2 = v:fname_new
  if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
  let arg3 = v:fname_out
  if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
  if &sh =~ '\<cmd'
    silent execute '!""C:\Program Files\Vim\vim64\diff" ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3 . '"'
  else
    silent execute '!C:\Program" Files\Vim\vim64\diff" ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3
  endif
endfunction

.vimrc d'Eric

" vimrc file.
"

" Use Vim settings, rather then Vi settings (much better!).
" This must be first, because it changes other options as a side effect.
set nocompatible

set bs=2		" allow backspacing over everything in insert mode
set viminfo='20,\"50	" read/write a .viminfo file, don't store more
			" than 50 lines of registers
set history=50		" keep 50 lines of command line history
set ruler		" show the cursor position all the time
set nohidden
set wrap

set autoindent			" always set autoindenting on
set smartindent
set smarttab
set expandtab
set shiftwidth=4
set tabstop=4
set scrolloff=8
set ignorecase
set infercase
set incsearch
set wildmenu
set nohlsearch
set clipboard=unnamed
filetype plugin on

set backup		" keep a backup file
set backupdir=d:/home/ebr/tmp,.,d:/home/ebr/
set dir=d:/home/ebr/tmp,.,d:/home/ebr/

" Don't use Ex mode, use Q for formatting
map Q gq

" for QWERTY and DVORAK
nmap ; :
vmap ; :

" Z mark is last search...
nmap n nmzz.`z
nmap N Nmzz.`z
nmap * *mzz.`z
nmap # #mzz.`z
nmap g* g*mzz.`z
nmap g# g#mzz.`z


" Make H toggle hlsearch
noremap H :set invhlsearch<CR> 

" Switch syntax highlighting on, when the terminal has colors
" Also switch on highlighting the last used search pattern.
if &t_Co > 2
  set bg=dark
  syntax on
endif

autocmd BufRead,BufNewFile *.hlsl,*.fx,*.fxh,*.fxc  setfiletype fx

" Only do this part when compiled with support for autocommands.
if has("autocmd")

 " In text files, always limit the width of text to 78 characters
 autocmd BufRead *.txt set tw=78

 augroup cprog
  " Remove all cprog autocommands
  au!

  " When starting to edit a file:
  "   For C and C++ files set formatting of comments and set C-indenting on.
  "   For other files switch it off.
  "   Don't change the order, it's important that the line with * comes first.
  autocmd FileType * set formatoptions=tcql nocindent comments&
  autocmd FileType c,cc,cpp,h,java set formatoptions=croql cindent comments=sr:/*,mb:*,el:*/,://
 augroup END

 augroup gzip
  " Remove all gzip autocommands
  au!

  " Enable editing of gzipped files
  " set binary mode before reading the file
  autocmd BufReadPre,FileReadPre	*.gz,*.bz2 set bin
  autocmd BufReadPost,FileReadPost	*.gz call GZIP_read("gunzip")
  autocmd BufReadPost,FileReadPost	*.bz2 call GZIP_read("bunzip2")
  autocmd BufWritePost,FileWritePost	*.gz call GZIP_write("gzip")
  autocmd BufWritePost,FileWritePost	*.bz2 call GZIP_write("bzip2")
  autocmd FileAppendPre			*.gz call GZIP_appre("gunzip")
  autocmd FileAppendPre			*.bz2 call GZIP_appre("bunzip2")
  autocmd FileAppendPost		*.gz call GZIP_write("gzip")
  autocmd FileAppendPost		*.bz2 call GZIP_write("bzip2")

  " After reading compressed file: Uncompress text in buffer with "cmd"
  fun! GZIP_read(cmd)
    " set 'cmdheight' to two, to avoid the hit-return prompt
    let ch_save = &ch
    set ch=3
    " when filtering the whole buffer, it will become empty
    let empty = line("'[") == 1 && line("']") == line("$")
    let tmp = tempname()
    let tmpe = tmp . "." . expand("<afile>:e")
    " write the just read lines to a temp file "'[,']w tmp.gz"
    execute "'[,']w " . tmpe
    " uncompress the temp file "!gunzip tmp.gz"
    execute "!" . a:cmd . " " . tmpe
    " delete the compressed lines
    '[,']d
    " read in the uncompressed lines "'[-1r tmp"
    set nobin
    execute "'[-1r " . tmp
    " if buffer became empty, delete trailing blank line
    if empty
      normal Gdd
    endif
    " delete the temp file
    call delete(tmp)
    let &ch = ch_save
    " When uncompressed the whole buffer, do autocommands
    if empty
      execute ":doautocmd BufReadPost " . expand("%:r")
  endfun

  " After writing compressed file: Compress written file with "cmd"
  fun! GZIP_write(cmd)
    if rename(expand("<afile>"), expand("<afile>:r")) == 0
      execute "!" . a:cmd . " <afile>:r"
    endif
  endfun

  " Before appending to compressed file: Uncompress file with "cmd"
  fun! GZIP_appre(cmd)
    execute "!" . a:cmd . " <afile>"
    call rename(expand("<afile>:r"), expand("<afile>"))
  endfun

 augroup END

endif " has("autocmd")

Références

  • Wiki Vim [1]
  • Refactoring avec Vim [2]
  • Tutoriaux visuels [3], [4]

Warning: main() [function.main]: open_basedir restriction in effect. File(/mnt/145/sdb/9/8/sroccaserra/wiki/skins/common/images/icons/Images-functions.txt) is not within the allowed path(s): (/mnt/109/sdb/9/8/sroccaserra) in /mnt/109/sdb/9/8/sroccaserra/wiki/includes/OutputPage.php on line 2

Warning: main(/mnt/145/sdb/9/8/sroccaserra/wiki/skins/common/images/icons/Images-functions.txt) [function.main]: failed to open stream: Operation not permitted in /mnt/109/sdb/9/8/sroccaserra/wiki/includes/OutputPage.php on line 2

Warning: main() [function.include]: Failed opening '/mnt/145/sdb/9/8/sroccaserra/wiki/skins/common/images/icons/Images-functions.txt' for inclusion (include_path='/mnt/109/sdb/9/8/sroccaserra/include:.:/usr/php4/lib/php') in /mnt/109/sdb/9/8/sroccaserra/wiki/includes/OutputPage.php on line 2