Vim-script question

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Vim-script question

Klaus Jantzen
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.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/5d00aa7b-4aeb-a012-5c31-9af20823a2d1%40mailbox.org.
Reply | Threaded
Open this post in threaded view
|

Re: Vim-script question

Tony Mechelynck
On Thu, Dec 10, 2020 at 6:01 PM 'Klaus Jantzen' via vim_use
<[hidden email]> wrote:

>
> 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.

: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.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/CAJkCKXsHRAcCjYXN30YCjD_-80MOqkV2xhQPpk4gapaXvHg3wQ%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: Vim-script question

Gary Johnson-4
In reply to this post by Klaus Jantzen
On 2020-12-10, 'Klaus Jantzen' via vim_use wrote:

> 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.

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

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20201210174220.GA15174%40phoenix.
Reply | Threaded
Open this post in threaded view
|

Re: Vim-script question

arocker
In reply to this post by Klaus Jantzen

> 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.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/a573586d91b82e45938cbf82d8b775d3.squirrel%40webmail.vybenetworks.com.
Reply | Threaded
Open this post in threaded view
|

Re: Vim-script question

Charles Campbell-2
[hidden email] wrote:
>> 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.
>
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

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/78eb93bd-4092-a77c-f268-baf42ded1c5e%40drchip.org.
Reply | Threaded
Open this post in threaded view
|

Re: Vim-script question

Klaus Jantzen
In reply to this post by Klaus Jantzen
On 12/10/20 4:03 PM, 'Klaus Jantzen' via vim_use wrote:

> 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.
>
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.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/17c00fdf-f576-1114-e987-a846ad94081d%40mailbox.org.