add skeleton to new file based on filetype, not extension

14 views
Skip to first unread message

NBaH

unread,
Jun 5, 2023, 8:58:47 AM6/5/23
to vim...@googlegroups.com

hello,

I'm trying to add skeleton to new files having a filetype (eg python)

here's what  I've tried :

autocmd BufNewFile if &filetype == "python" 0r ~/.vim/python.skel | normal G | let IndentStyle = "python" | endif

with no luck when I open a new file like this :

: bel new | set filetype=python

I know skeleton works , because I have this line that adds it when open a named new file

au BufNewFile *.py 0r ~/.vim/python.skel | normal G | let IndentStyle = "python"


PS: i noticed, when I open a new file with the command above, that adding startinsert at the end causes loading to be much longer and may corrupt dipslay.

this  doesn't work very well:

: bel new | set filetype=python | startinsert

this works well:

: bel new | startinsert | set filetype=python

Gary Johnson

unread,
Jun 5, 2023, 10:54:40 AM6/5/23
to vim...@googlegroups.com
On 2023-06-05, NBaH wrote:
> hello,
>
> I'm trying to add skeleton to new files having a filetype (eg python)
>
> here's what  I've tried :
>
> autocmd BufNewFile if &filetype == "python" 0r ~/.vim/python.skel | normal G |
> let IndentStyle = "python" | endif

One problem with the above is that the autocommand is missing the
pattern argument, the filename pattern following "BufNewFile".
Another is that there should be a "|" between the if condition and
the following command. You also need to be sure to define this
autocommand _after_ you enable filetype detection. This should work
(one line):

autocmd BufNewFile * if &filetype == "python" | 0r ~/.vim/python.skel | normal G | let IndentStyle = "python" | endif

The reason that it must be defined after filetype detection is
enabled is so that the filetype will be defined before your
autocommand is executed.

> with no luck when I open a new file like this :
>
> : bel new | set filetype=python

In addition to the problems with the BufNewFile autocommand, no file
name argument has been given to that :new command, so the BufNewFile
autocommand is not triggered.

HTH,
Gary

NBaH

unread,
Jun 5, 2023, 6:26:41 PM6/5/23
to vim...@googlegroups.com
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.


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


Gary Johnson

unread,
Jun 5, 2023, 8:02:03 PM6/5/23
to vim...@googlegroups.com
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

NBaH

unread,
Jun 5, 2023, 9:12:10 PM6/5/23
to vim...@googlegroups.com
Le 06/06/2023 à 02:58, Gary Johnson a écrit :
if line("$") == 1 && empty(getline(1))

that's nice

if last line is the first one, and  first line is empty

thank you.

Reply all
Reply to author
Forward
0 new messages