Autocomplétion des tags avec Vim
by
xeo
@ 24/02/2006
Fermeture automatique des balises XML ou HTML
Si vous avez déjà écris des documents HTML ou XML avec Vim, vous avez sûrement trouvé très contraignant d'écrire le tag ouvrant et le tag fermant à la main.
Le morceau de code suivant vous permettra de fermer automatiquement le dernier tag ouvert et qui n'est pas fermé en appuyant sur la touche F8. De plus vous pouvez préciser des tags qu'il ne faut pas fermer, dans la liste nommée UnaryTags.
Rajoutez le code suivant dans votre fichier ~/.vimrc :
" Close HTML tags
nnoremap \hc :call InsertCloseTag()<CR>
imap <F8> <Space><BS><Esc>\hca
function! InsertCloseTag()
" inserts the appropriate closing HTML tag
"
" by Smylers http://www.stripey.com/vim/
" 2000 May 4
" list of tags which shouldn't be closed:
let UnaryTags = ' area base br dd dt hr img input link meta param '
" remember current position:
normal mz
" loop backwards looking for tags:
let Found = 0
while Found == 0
" find the previous <, then go forwards one character and grab the first
" character plus the entire word:
execute "normal ?\<LT>\<CR>l"
normal "zyl
let Tag = expand('<cword>')
" if this is a closing tag, skip back to its matching opening tag:
if @z == '/'
execute "normal ?\<LT>" . Tag . "\<CR>"
" if this is a unary tag, then position the cursor for the next
" iteration:
elseif match(UnaryTags, ' ' . Tag . ' ') > 0
normal h
" otherwise this is the tag that needs closing:
else
let Found = 1
endif
endwhile " not yet found match
" create the closing tag and insert it:
let @z = '</' . Tag . '>'
normal `z
if col('.') == 1
normal "zP
else
normal "zp
endif
endfunction " InsertCloseTag()
Liens :
Source du script : http://www.stripey.com/vim/vimrc.html
Par xeo
Dernière modification
22/03/2007 15:55