Come posso aggiungere automaticamente un po 'di codice scheletro quando creo un nuovo file con vim

StackOverflow https://stackoverflow.com/questions/162617

  •  03-07-2019
  •  | 
  •  

Domanda

Quando creo un nuovo file con vim, vorrei aggiungere automaticamente del codice scheletro.

Ad esempio, durante la creazione di un nuovo file XML, vorrei aggiungere la prima riga:

  <?xml version="1.0"?>

O quando creo un file html, vorrei aggiungere:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <title></title>
  </head>
  <body>
  </body>
</html>
È stato utile?

Soluzione

Se vuoi adattare il tuo scheletro al contesto o alle scelte dell'utente, dai un'occhiata ai plug-in di espansione modello elencati in vim.wikia

Altri suggerimenti

Ho qualcosa di simile nel mio .vimrc:

au BufNewFile *.xml 0r ~/.vim/xml.skel | let IndentStyle = "xml"
au BufNewFile *.html 0r ~/.vim/html.skel | let IndentStyle = "html"

E così via, qualunque cosa ti serva.

Puoi salvare il tuo scheletro / modello in un file, ad esempio ~ / vim / skeleton.xml

Quindi aggiungi quanto segue al tuo .vimrc

augroup Xml
    au BufNewFile *.xml 0r ~/vim/skeleton.xml
augroup end

Scusate il ritardo, ma sento che il modo Lo faccio potrebbe essere utile per alcuni. Utilizza il tipo di file del file, rendendolo più breve e dinamico rispetto ai metodi più convenzionali. È stato testato solo su Vim 7.3.

if has("win32") || has ('win64')
    let $VIMHOME = $HOME."/vimfiles/"
else
    let $VIMHOME = $HOME."/.vim/"
endif

" add templates in templates/ using filetype as file name
au BufNewFile * :silent! exec ":0r ".$VIMHOME."templates/".&ft

Ecco due esempi che usano lo scripting Python.

Aggiungi qualcosa del genere nel tuo .vimrc o in un altro file proveniente dal tuo .vimrc:

augroup Xml
  au BufNewFile *.xml :python import vim
  au BufNewFile *.xml :python vim.current.buffer[0:0] = ['<?xml version="1.0"?>']
  au BufNewFile *.xml :python del vim
augroup END

fu s:InsertHtmlSkeleton()
  python import vim
  python vim.current.buffer[0:0] = ['<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">', "<html>", "<head>", "  <title></title>", "</head>", "<body>", "", "</body>", "</html>"]
  python del vim
endfu

augroup Html
  au BufNewFile *.html call <SID>InsertHtmlSkeleton()
augroup END

È possibile aggiungere vari hook quando i file vengono letti o creati. a

:help event

e leggi cosa c'è. Quello che vuoi è

:help BufNewFile

Può funzionare anche con snipmate:

augroup documentation
    au!
    au BufNewFile *.py :call ExecuteSnippet('docs')
augroup END

function! ExecuteSnippet(name)
    execute "normal! i" . a:name . "\<c-r>=TriggerSnippet()\<cr>"
endfunction

con " documenti " lo snippet da attivare.

Funziona con più frammenti ma poi appare la finestra: messaggi ed è ingombrante.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top