vimで新しいファイルを作成するときにスケルトンコードを自動的に追加するにはどうすればよいですか

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

  •  03-07-2019
  •  | 
  •  

質問

vimを使用して新しいファイルを作成するときに、スケルトンコードを自動的に追加したいと思います。

たとえば、新しいxmlファイルを作成する場合、最初の行を追加します:

  <?xml version="1.0"?>

またはhtmlファイルを作成するときに、次を追加します:

<!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>
役に立ちましたか?

解決

スケルトンをコンテキストまたはユーザーの選択に適応させる場合は、 vim.wikia

他のヒント

.vimrcで次のようなものを得ました:

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

など、必要なものは何でも。

スケルトン/テンプレートをファイル、たとえば〜/ vim / skeleton.xmlに保存できます

次に、次を.vimrcに追加します

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

遅れて申し訳ありませんが、やる方法は、一部の人にとっては役に立つかもしれません。ファイルのファイルタイプを使用するため、従来の方法よりも短く、より動的になります。 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

Pythonスクリプトを使用した2つの例です。

.vimrcまたは.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

ファイルの読み取りまたは作成時にさまざまなフックを追加できます。

:help event

そこにあるものを読んでください。欲しいのは

:help BufNewFile

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

&quot; docs&quot;トリガーするスニペット。

複数のスニペットで動作しますが、:messagesウィンドウが表示され、面倒です。

htmlのプラグインを作成しました:

vimスクリプトの場合: http://www.vim.org/scripts/ script.php?script_id = 4845

Github: https://github.com/linuscl/vim-htmltemplate

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top