Vim-script question

۲۹ بازدید
رفتن به اولین پیام خوانده‌نشده

Klaus Jantzen

خوانده‌نشده،
۲۰ آذر ۱۳۹۹، ۱۲:۰۱:۳۶۱۳۹۹/۹/۲۰
به vim...@googlegroups.com
Hi,

I want to include in my .vimrc an autocommand that writes some new lines
into an empty file e.g. an zsh script.

So I wrote

au  BufNewFile *.zsh :a "#! /usr/bin/zsh"

but that does not do what I want (it does not do anything).

So my question: How can I write with the autocommand one or more lines
into an empty buffer?

Thanks for your help.

--

K.D.J.

Tony Mechelynck

خوانده‌نشده،
۲۰ آذر ۱۳۹۹، ۱۲:۲۴:۳۴۱۳۹۹/۹/۲۰
به vim_use
:append is for several lines of text and (IIUC) needs an end-of-text
line. What you want here is :put

For instance:

:au BufNewFile *.zsh 0put ="#! /usr/bin/zsh"

See
:help :put
:help "=

Best regards,
Tony.

Gary Johnson

خوانده‌نشده،
۲۰ آذر ۱۳۹۹، ۱۲:۴۲:۵۸۱۳۹۹/۹/۲۰
به vim...@googlegroups.com
The :a command does not take an argument, it starts inserting lines
that are typed after :a is executed. The following should do what
you want.

au BufNewFile *.zsh a
au BufNewFile *.zsh #!/usr/bin/zsh

Another command you could use is :put, e.g.,

au BufNewFile *.zsh 0put ='#!/usr/bin/zsh'

Regards,
Gary

aro...@vex.net

خوانده‌نشده،
۲۰ آذر ۱۳۹۹، ۱۳:۱۸:۴۱۱۳۹۹/۹/۲۰
به vim...@googlegroups.com

> I want to include in my .vimrc an autocommand that writes some new lines
> into an empty file e.g. an zsh script.
>

A possible alternative is to use a "here" document containing whatever
boilerplate you want, in a shell script that then opens vim on the
resulting file.

Charles Campbell

خوانده‌نشده،
۲۰ آذر ۱۳۹۹، ۲۰:۰۶:۳۵۱۳۹۹/۹/۲۰
به vim...@googlegroups.com
Another alternative: in your $HOME/.vim/filetype.vim :

    au BufNewFile *.zsh  :0r $STUB/stub.zsh

You'll have to set up a STUB environment variable a path to a directory
you set up.
Then, put stub.zsh in there with whatever you'd like to start your zsh
file with.

Repeat for whatever other filetypes you want.

Regards,
Chip Campbell

Klaus Jantzen

خوانده‌نشده،
۲۳ آذر ۱۳۹۹، ۵:۵۴:۲۹۱۳۹۹/۹/۲۳
به 'Klaus Jantzen' via vim_use
Hi,

thank you very much for your very helpful information. Now I have a much
better understanding of vimscript.

The following shows what I finally wrote

===
function NewZshScript()
    let ts = " generated on " . strftime('%Y-%m-%d at %H:%M:%S')
    let inf = expand("%") . ts
    0put ='#! /usr/bin/zsh'
    put ='#'
    put ='# ' . inf
    put ='#'
    put ='pn=$1'
    put ='pn=${pn##*/}'
    put ='#'
endfunction
au BufNewFile *.zsh call NewZshScript()

===

First I tried 'append' but I never get that working with the line
continuations. Besides the function gives me more flexibility.

Here doc or stubs I will try when I have a bigger template.

Thank you.

--

K.D.J.

پاسخ به همه
پاسخ به نویسنده
فرستادن
0 پیام جدید