Vim-script question

29 views
Skip to first unread message

Klaus Jantzen

unread,
Dec 10, 2020, 12:01:36 PM12/10/20
to 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

unread,
Dec 10, 2020, 12:24:34 PM12/10/20
to 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

unread,
Dec 10, 2020, 12:42:58 PM12/10/20
to 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

unread,
Dec 10, 2020, 1:18:41 PM12/10/20
to 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

unread,
Dec 10, 2020, 8:06:35 PM12/10/20
to 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

unread,
Dec 13, 2020, 5:54:29 AM12/13/20
to '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.

Reply all
Reply to author
Forward
0 new messages