Monday, June 5, 2023

Re: add skeleton to new file based on filetype, not extension

On 2023-06-06, NBaH wrote:
> Le 05/06/2023 à 17:50, Gary Johnson a écrit :
>
> no file name argument has been given to that :new command, so the BufNewFile
> autocommand is not triggered.
>
> this is the most important part of the reason why it doesn't work. :)
>
> thank you for correcting autocommand.

You're welcome. I'm glad I could help.

> do you have any (simple) idea about how to add skeleton to unnamed file having
> filetype as python ?

I would hook the FileType autocommand event instead of the
BufNewFile event, then test for whether the buffer is for a new or
existing file before reading the template.

I don't know what the best way is to determine whether or not
a buffer is empty, but this is a method I've used.

autocmd FileType python if line("$") == 1 && empty(getline(1))
\ | 0r ~/.vim/python.skel
\ | normal G
\ | let IndentStyle = "python"
\ | endif

That will work whether you open a new file like this:

$ vim newfile.py

or without a name like this:

$ vim
:set filetype=python

or this:

$ vim somefile.c
:new
:set filetype=python

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 vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20230606005812.GA7287%40phoenix.

No comments:

Post a Comment