2

I want to create a shell script that will create a pre-formatted file and open it in vim.

Specifically, I want to create a script called newperl that will allow me to type newperl [filename] and generate a file (using cat, echo, >, etc.) that is pre-formatted with

#!/usr/bin/perl
use 5.014;

etc.

I know how to do all the formatting etc. My problem is passing the [filename] which I type in after newperl to the rest of the script as an argument. How do I do that?

4

2 に答える 2

5

シェルスクリプトを書く必要はありません。Vim には、新しいファイルを開くときに既存のファイルを読み込むためのフックが既に用意されています。

まず、perl テンプレートを便利な場所に保存します。、など~/.vim/new_file_templatesを含むディレクトリを保持しています。skeleton.plskeleton.py

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

autocmd BufNewFile *.pl 0r ~/.vim/new_file_templates/skeleton.pl

vimこれは、名前が「*.pl」に一致する新しいファイルを開くときに、新しいファイルskeleton.plの最初の行の内容を読み込むことを意味します。

あとは入力するだけ

vim newperl.pl

テンプレートとして「skeleton.pl」を使用して、「newperl.pl」という名前の新しいファイルを開きます。

于 2012-08-14T14:35:26.950 に答える
4

変数$1には、スクリプトの呼び出し後に渡された単語/引用符付き文字列が自動的に含まれます。

于 2012-08-14T14:33:56.023 に答える