Friday, July 2, 2010

Re: File types for new files

On Friday, July 02, 2010 19:56:20 Gary Johnson wrote:
> On 2010-07-02, Daniel D Jones wrote:
> > Using Gvim under Gentoo Linux, if I open a file with a .txt extension,
> > Gvim sets the filetype to text and sets the appropriate word wrap, etc.
> > that I have configured. But if I open Gvim, create a new buffer with
> > :tabnew and then save that as a file with a .txt extension, Gvim doesn't
> > set the filetype and I have to explicitly set it in order to get the
> > word wrap and other file settings I have configured. Is there a setting
> > I'm missing to have Gvim automatically recognize the file type when the
> > file is named and saved, or is that not possible absent some scripting?
>
> Vim assumes that you want to determine the file type when a file is
> opened or a new buffer is created, i.e., before you start viewing or
> editing, not when you write the file. In most cases, by the time
> you write a file you've either already set the file type or you
> don't care.

Thanks for the info, but how do you set the filetype when you create the buffer?
That's assuming you're not running Vim from a command line and so can pass the
filename when you start the program. Uh, OK, nevermind. The obvious occurred
to me when I wrote that line. I didn't realize that :tabnew took args.

> The easiest way to set the file type after writing a file is to
> simply edit it again,
>
> :e

That never occurred to me. Quicker than manually setting the filetype.

> If you really want to automate this, you could put an autocommand
> like this in your ~/.vimrc:
>
> au BufWrite * if &ft == '' | filetype detect | fi

Cool. I'll keep that archived but hopefully I can train myself to just name
the file when created.

--
"It is impossible to experience one's death objectively and still carry a
tune." - Woody Allen

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

No comments:

Post a Comment