Retourner au contenu. Retourner à la navigation

 

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
Navigation
Actualités
18/12/2008 Sortie d'OpenSuse 11.1
03/12/2008 Songbird 1.0
20/10/2008 Société Générale se met au vert
15/09/2008 Sortie de la version VLC 0.9.2
23/06/2008 Opération du libre à Nantes !
Plus d'actualités...
Articles
22/05/2008 Première approche de Qmail
19/05/2008 Test de la distribution Elive 1.0 Gem
14/05/2008 GNUPG introduction à la cryptographie et utilisation de GnuPG
21/02/2008 GNU / Screen
03/09/2007 The Linux File System Encryption API
More articles
Tips
28/04/2008 Mozilla Firefox : Google Talk et Facebook Chat
22/04/2008 Sed : Rechercher du texte entre deux chaines de caractères
04/04/2008 Gérer son(ses) écran(s) avec xrandr
26/03/2008 Tips sur l'historique de vos commandes
13/02/2008 Linux-Unix Cheat Sheets
More tips
Codes
09/04/2008 Chapitre 13 - Administration DNS et DHCP
09/04/2008 Chapitre 06 - Service web avec Apache
04/04/2008 Chapitre 09 - PureFTPd
04/04/2008 Chapitre 06 - Scripting Bash
01/04/2008 Chapitre 20 - Haute Disponibilité
More codes
Courses
13/09/2006 Module 3
23/02/2006 Module 2
23/02/2006 Module 1
More courses
Formation Linux

Supinfo Training Center has the first Linux Certification. The training is 13 days and allow you to pass the LPI 101 and 102.

more info
 
 
Vous êtes ici :
Codes Autocomplétion des tags avec Vim