Enable syntax highlight for file extension contained somewhere in filename - vim

How can I overwrite default vim file extension to syntax rules to work if filename contains .extension anywhere in filename?
For file named file.html.jinja2 I want vim to use HTML syntax.
I just want to avoid making multiple au for each filetype.

You could use an autocommand with * wildcards, for example:
autocmd BufRead,BufNewFile,BufWinEnter *.html* setf html
The BufRead,BufNewFile,BufWinEnter events ensure that the filetype is detected if the file is loaded or created. You may need to add more events, depending on your editing workflow.

Related

Set a 'fallback' filetype in vim

In vim, it seems like if a filetype isn't recognized, it falls back to text / utf-8 [unix. Here is an example from a text file I have:
I'm not sure if 'text' is the filetype that vim detects or if 'text' just means 'no filetype detected'. Either way, I would like to have files that are opened with an undetectable filetype (including txt) files as set ft=markdown. How could this be done? Additionally, is it possible to save the filetype that has been manually entered for a file? For example, let's say for a particular txt file I change it to set ft=rst, can I save that somehow, such as in the 'viminfo' file?
I'm not sure if 'text' is the filetype that vim detects or if 'text' just means 'no filetype detected'.
"Text" is a filetype. It only doesn't do much by default, but anyway... "Not detected" is an empty value of filetype.
I would like to have files that are opened with an undetectable filetype (including txt) files as set ft=markdown. How could this be done?
I'm not sure if it's a good idea to have so many "markdowns", but, in principle, you can try to write some :h ftdetect script which does something like setf FALLBACK markdown.
Considering *.txt you can either reassign filetype with ftdetect too (need to be careful not to break "help" and such), or maybe replace it on ftplugin level by creating ~/.vim/ftplugin/text.vim which forcefully does runtime! ftplugin/markdown.vim. The choice is yours.
Additionally, is it possible to save the filetype that has been manually entered for a file? For example, let's say for a particular txt file I change it to set ft=rst, can I save that somehow, such as in the 'viminfo' file?
As "filetype" is an option, it can be saved into "session" file (depending on 'ssop' value), not to "viminfo". But I'd rather suggest to put it directly into a file instead (:h modeline).

Vim filetype settings not loading when creating a file

I have defined several .vim files in the ftplugin directory for some of the languages I work with. When opening files that already exist, the settings in these files load correctly. However, when I am creating a new file (i.e. by running vim filename.py) the corresponding settings are not loaded until after I save the file (so that it exists on disk), close vim, and re-open it.
Is there a way to get vim to load filetype setting from ftplugin for files that vim is creating?
A filetype like Python is determined based on its file extension (cp. $VIMRUNTIME/filetype.vim):
au BufNewFile,BufRead *.py,*.pyw setf python
With BufNewFile, the filetype is applied to newly created files, too. This only doesn't work for filetypes that are detected by inspecting the buffer contents (e.g. a shebang like #!/bin/bash in the first line), not based on a file pattern. For these, it is indeed necessary to save (and re-edit) the file, or to :setf manually.

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.

Executing vim command based on filetype?

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.

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