On Sat, May 26, 2012 at 03:50:24PM EDT, Gary Johnson wrote: > On 2012-05-25, Chris Jones wrote: > > > There are things I find a little unclear regarding autocommands but > > assuming the file extension is something like ‘*.mkdwn’ shouldn't it be > > possible to achieve this automatically when creating the file by placing > > something like this in a file named ~/.vim/ftdetect/markdown.vim: > > > > | autocmd BufNewFile,BufRead *.mkdwn set filetype=markdown > > > > Wouldn't this cause ‘:w myfile.mkdwn’ to automatically enable the > > markdown foldexpr and create the folds in one pass? > > No. BufNewFile will set the filetype to markdown when starting to > edit a new file with a name ending in .mkdwn;
That's also how I understood it, yet.. I tested with a markdown file and BufNewFile and BufRead were the only two events defined to Vim. Non-customized Vim 7.3, no plugins, etc. This is what I see in filetype.vim: " Markdown au BufNewFile,BufRead *.markdown,*.mdown,*.mkd,*.mkdn,README.md setf markdown I started a Vim session by typing ‘vim’ at the prompt and I entered some markdown in the [No Name] buffer. At this point ‘set ft?’ said ‘filetype=’.. Now as soon as I had written the file to disk as ‘sample.mdown’ or .mkd.. .mkdn.. etc., ft was correctly set: ‘filetype=markdown’. so is it the BufNewFile or the BufRead event that causes the filetype to be set? Between the two, I would tend to think it is BufNewFile, not because the file is written to disk, but rather because the Vim buffer now has a name. I don't know if is meant to work this way (the doc would appear to suggest otherwise) but I suspect that the BufNewFile event is also triggered when you issue a ‘:w’ command against a scratch buffer. [Mind you, this is linux, not MacVim though I don't see why it would make any difference since this appears to be an event that is purely internal to Vim.] Anyway, this is what I understood the OP was doing: creating a scratch buffer, entering code, and saving the file via a ‘:w xxxxx.markdown’ command. But where I do agree, is that if it is acceptable for the OP to create his files by giving them a ‘xxxxx.markdown’ file name on the initial ‘:new’ (:vne.. :tabe.. etc.) command he will have the benefit of immediately having the Vim ‘filetype’ option automatically set and both syntax highlighting and automatic folding will be available as long as there exist corresponding definitions (syntax file with or without syntax folding, folding script.. etc.). In other words, as soon as he starts typing, keywords, headings, etc. will be recognized and highlighted accordingly. This obviously cannot happen as long as you are entering your code in a [No Name], (ft=) buffer. But I don't see how this is going to solve his problem if the BufNewFile event is not triggered in the first place. > BufRead will set it when opening an existing file whose name ends with > .mkdwn. To have ":w myfile.mkdwn" set the filetype, you would need to > use the BufWrite autocommand event. That's not normally done because > one usually wants any setting dependent on the file type executed when > starting to edit the file, not when the editing is finished. I didn't go into into it but that was my point: it is not ‘normally’ done and yet what the OP reports as not working does work everywhere else for similarly defined file types (if you look at filetype.vim). > The ways I usually set the filetype on new files is to open them by > name > > $ vim foo.c What I don't like all that much about this approach is that on a multi-user system especially, and since the file is not actually created in the file system, a file with the same name might be created before you actually write foo.c to disk. And then you get one of those confusing messages... > or > > :split foo.c > > or if I'm already in an empty buffer or the file doesn't have an > extension (e.g., a shell script), by executing > > :set ft=c I just did a quick test with ‘:tabe’ (no file name), obviously the filetype was ''. I entered some vimscript & as soon as I wrote the file under f.vim, syntax highlighting came on. To _Eric W._ What happens when you do as Gary suggests: start Vim via a ‘vim newfile.mkd’..? CJ -- AHH! The neurotic monkeys are after me! -- 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