Executing vim command based on filetype? - vim

I'm trying to have the command
let b:match_words='<:>,<\#<=\([^/][^ \t>]*\)[^>]*\%(>\|$\):<\#<=/\1>'
run every time I open an html file. I tried putting the line
autocmd FileType html let b:match_words='<:>,<\#<=\([^/][^ \t>]*\)[^>]*\%(>\|$\):<\#<=/\1>'
in a file named html.vim in both my ftdetect and ftplugin folders and nothing happened. How do I have the command run everytime I'mm in an html file?
The command is to change the matching behavior of matchit btw.

In general, your autocmd is alright; the problem is that you're trying to redefine the b:match_words definition done in $VIMRUNTIME/ftplugin/html.vim, so the execution order becomes important.
The place for these customizations is in the after directory, i.e. ~/.vim/after/ftplugin/html.vim; just create a new file and put the :let command in there.
You can observe the sequence of sourced scripts via :scriptnames. In other cases, when you're not overriding default behavior, the :autocmd FileType is alright, but I prefer putting these (e.g. custom mappings) into ~/.vim/ftplugin/html_mymappings.vim, as it provides better separation and helps keeping your .vimrc short and understandable.
The ftdetect subdirectory is for filetype detection, i.e. inspecting file path / name / contents to determine the correct filetype. It doesn't apply here, as the filetype is html.

Related

Syntax file sourced from the plugin is overwritten by Vim

I am creating a very simple plugin to make syntax highlighting better in Vim for baan syntax (basically an improved version of syntax/baan.vim).
This is my plugin directory.
One file inside ftdetect folder; baan.vim. It looks like this.
au BufRead,BufNewFile *.bc set filetype=baan
The file inside syntax folder; baan.vim. It is almost the same as baan.vim in syntax folder within Vim. With one line of change in syntax highlighting.
My problem is when I open any files with .bc extensions, two more syntax files are called; one before my own plugin syntax file, and other one is after.
bc.vim syntax file is called already from filetyp.vim, I guess. Because filetypes I want to set syntax is with extension .bc. This is the first issue. Second one is that I set filetype to 'baan' but Vim is looking and sourcing all baan.vim files. But once baan.vim is source from myplugin, it is still sourcing the one from Vim itself. How can I solve those issues elegantly without using /after directory?
This is normal. Consider the following.
$VIMRUNTIME/filetype.vim sources ftdetect/*.vim scripts near to the end. At this point setf bc was already executed and the first FileType event was triggered and processed. BTW. This is the reason why late set ft=baan works but setf baan no more. Shouldn't be a problem though, as :syn clear is executed in $VIMRUNTIME/syntax/synload.vim automatically. And no, you can't do anything with this unless you patch/replace filetype.vim.
synload.vim intentionally sources all matching files (:runtime!). This is the reason they respect b:current_syntax variable. The first to set it wins, others step aside by executing :finish at a top. Nonetheless, they all are sourced and get into :scriptnames. Note this also means that extending existing syntax with after/syntax (while not respecting b:current_syntax) is usually more preferable.

How do i correctly set the filetype txt for *.txt files in vim

I am trying to figure out how I am supposed to set the filetext to txt for *.txt files in vim
I was tempted to add a file into the ftdetect directory with the content
autocmd BufNewFile,BufRead *.txt set ft=txt
The problem with this approach, however, is that the autocmd is also triggered when I edit robots.txt. The default of vim ($VIMRUNTIME/filetype.vim) is to detect the robots.txt file and (correctly) set the filetype to robots in that case. I don't want to change that.
Additionally, I am not sure why I have to include this autocmd line into the ftdected directory. What is the difference to just add the line into my vimrc file?
Use :setf txt instead of :set ft=txt. This way, a previously set filetype (e.g. for robots.txt) is kept. From :help new-filetype:
The files in the "ftdetect" directory are used after all the default
checks, thus they can overrule a previously detected file type. But you
can also use |:setfiletype| to keep a previously detected filetype.
The benefit of the ftdetect directory is that it provides a well-known abstraction, and allows to separate the rules (this is important when you want to publish your filetype plugin for others). You could also put this (enclosed with :augroup filetypedetect) into your ~/.vimrc, but that's not so maintainable and the precedence rules are different.

How to enable go.vim by default (automatically)?

The instructions on the Vim site says to just put the file in the /syntax folder. This works all right and well. But, for me to use the syntax I must set the following
:set syntax=go
Every single time. So, I know I am doing something wrong. I just don't know what.
Here are some things from looking around,
My HTML5 syntax set is from Rodrigo's HTML5 omnicomplete function and syntax vimball file. Though this uses some installation script to get it going.
As far as I can tell this would be my first manual adding of syntax file.
Also, my VIMRUNTIME is not set, well because there is no syntax.vim file, so from reading the documentation I see it checks for files via synload.vim
I even read the "Making Your Own Syntax Files" section, which says that same as above with the syntax=go option. Am I supposed to be detecting the .go filetype as described in new filetype section?
How can I enable syntax highlighting for GO by default?
This is for Mac Snow Leopard.
I don't think it is this complicated but I decided to leave all the different documentation I skimmed. GO and Vim say to just add the file. But it is definitely not detecting it automatically
If you are using filetype detection in your ~/.vimrc file with the following line:
filetype plugin indent on
then you can place the file in the following folder:
~/.vim/after/ftplugin/go.vim
or for windows
~/vimfiles/...
For the filetype detection to work, would would want the autocmd in a file in the ftdetect folder:
~/.vim/ftdetect/go.vim
and the contents would be:
autocmd BufNewFile,BufReadPost *.go set filetype=go
Use autocmd:
au BufRead,BufNewFile *.go setlocal filetype=go

Vim In-File Commands

I'm after a means by which I can add additional commands to a text file via vim. For example, just as you can do something like this:
# vim:syntax=foo
I'd like to do something like:
# vim:option call matchadd('Special', '\(REQUIRED\|OPTIONAL\)')
Any ideas? I know I can write a syntax file, but this is not what I'm after for now.
Vim modeline syntax (see :help modeline) is not intended to specify commands
to execute during file opening. That is exactly what autocommands is for (see
:help autocommand). What you are trying to do should be an autocommand
similar the following.
autocmd FileType foo call matchadd('Special', '\(REQUIRED\|OPTIONAL\)')
or
autocmd BufReadPost *.foo call matchadd('Special', '\(REQUIRED\|OPTIONAL\)')
(Here instead of *.foo you can use any pattern that matches path or filename
(or both) of the target file.)
If the configuration you are setting up is local to some files or a project,
and you don't want to pollute your .vimrc with those autocmds, use
localvimrc plugin. It allows you to have a "local" .vimrc file next to
your target file or project folder. Script stored in that .lvimrc is
executed when you open files in the same directory where the "local" .vimrc
is, or in its subdirectories. Autocommands shown above (or any other
configurations) can be stored in a .lvimrc file local the project. For
details about localvimrc configuration see the homepage of the plugin.
This isn't an answer to your question, but I have also searched for Truth, and this question here is the closest one to it:
Vim: How to execute selected text as vim commands
It isn't automatic, but potentially only one keypress away it's close enough. :)
My ModelineCommands plugin extends Vim's built-in modelines to execute any Ex command(s) when a file is opened. A set of configurable validators examine the commands and can verify the correctness of an optional command digest, in order to prevent the execution of potentially malicious commands from unknown sources. (That's the main reason why Vim doesn't offer this feature!) This way, you could restrict the commands to only simple :let, or have the plugin query you to confirm execution of anything that isn't signed with your own secret key.

VIM: set filetype=txt for every new file [No Name]

I tried all possible things to let vim set filetype to 'txt' to all new files I create (in a new tab) but it doesn't work.
This is p.e. what I've read on the web a few times:
au BufRead,BufNewFile *.txt setlocal ft=txt
(to put in _vimrc)
However it doesn't work.
Can anyone help me?
The following line, added to your .vimrc, will set the filetype to text if it is not already set.
autocmd BufEnter * if &filetype == "" | setlocal ft=text | endif
All files are considered plain text unless you have file-type detection turned on or explicitly set the file-type. However, Vim lets you set the file-type to any old text, so are you absolutely sure it is not working?
:set filetype=banana
:set filetype?
filetype=banana
Setting the filetype is not going to have any noticable effect unless there is a corresponding file in the ftplugin Vim directory and Vim does not ship with a txt.vim file-type file. You could, of couse, add a txt.vim here but I am not sure what this will gain you over default settings — what special behaviour would you want for text files that you would not want for the default behaviour?
(If you want syntax highlighting (whatever that may mean for text file!) then you will also have to create a txt.vim file in the syntax Vim directory.)
What effect are you trying to achieve?
It's actually way simpler than all this. Just put this as one of the first lines in your .vimrc.
set ft=txt
Whenever opening a new file, the filetype will be set to txt. But if you open a file with an known existing type it will still be overridden no problem.

Resources