Monday, June 5, 2023

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

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

--
--
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/20230605155052.GA11223%40phoenix.

No comments:

Post a Comment