Loading ...
Sorry, an error occurred while loading the content.

forcing a filetype based on content

Expand Messages
  • Ragica
    I ve been fiddling for a long time now, trying to get Vim to force a filetype based on the first line, even if the extension is defined as another filetype. I
    Message 1 of 2 , Jul 1 2:36 PM
    • 0 Attachment
      I've been fiddling for a long time now, trying to get Vim to force a
      filetype based on the first line, even if the extension is defined as
      another filetype. I can't seem to figure out the proper place to do
      this. Obviously myscriptsfile won't work, because vim thinks the
      filetype is known based on extension. I've fiddled with myfiletypefile
      and mysyntaxfile; but for whatever reason I can't seem to get these to
      override the extension based filetype when specific content is
      detected.

      If i listed all of the things i've tried, no one would finish reading
      this message. The only techniques that seem to work (using
      autocommands on *, for example) then don't seem to work with the rest
      of my autocommands for that file type... since the * autocommands seem
      to be called last.

      I have actually for a long time forced this behavior by merely hacking
      the syntax.vim and filetype.vim ... but i'm trying hard to reform
      myself and get my scripts cleaned up and use my* files... but just
      can't seem to figure out how to get all the behavior I want this way.
    • Benji Fisher
      ... Here is a clue. Autocommands are called in the order in which they were defined. The * autocommands are called last if they are defined last. Now, the
      Message 2 of 2 , Jul 2 4:06 AM
      • 0 Attachment
        Ragica wrote:
        >
        > I've been fiddling for a long time now, trying to get Vim to force a
        > filetype based on the first line, even if the extension is defined as
        > another filetype. I can't seem to figure out the proper place to do
        > this. Obviously myscriptsfile won't work, because vim thinks the
        > filetype is known based on extension. I've fiddled with myfiletypefile
        > and mysyntaxfile; but for whatever reason I can't seem to get these to
        > override the extension based filetype when specific content is
        > detected.
        >
        > If i listed all of the things i've tried, no one would finish reading
        > this message. The only techniques that seem to work (using
        > autocommands on *, for example) then don't seem to work with the rest
        > of my autocommands for that file type... since the * autocommands seem
        > to be called last.

        Here is a clue. Autocommands are called in the order in which they were
        defined. The * autocommands are called last if they are defined last. Now,
        the point of the my*file files is that they are :source'd after the default
        files. So the first thing you need is something like

        augroup filetype
        au! BufNewFile,BufRead {pattern}
        augroup END

        (from :help remove-filetype) in myfiletypefile. Now, 'filetype' will not be
        set, so the scripts file will be called. Put the appropriate lines in
        myscriptsfile. Remember to set the my*file variables in your vimrc, and pay
        attention to (from :help myfiletypefile)

        NOTE: Make sure that you set "myfiletypefile" before switching on file type
        detection. Thus is must be before any ":filetype on" or ":syntax on"
        command.

        Also, remember that these are variables, not options, so use :let instead of
        :set.

        > I have actually for a long time forced this behavior by merely hacking
        > the syntax.vim and filetype.vim ... but i'm trying hard to reform
        > myself and get my scripts cleaned up and use my* files... but just
        > can't seem to figure out how to get all the behavior I want this way.

        HTH --Benji Fisher
      Your message has been successfully submitted and would be delivered to recipients shortly.