Set a 'fallback' filetype in vim - 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).

Related

autocmd does not detect *.ott files

I'd like to have syntax highlighting on files with the .ott extension (using ott-vim), but I am not getting any colors when opening a file, even though filetype=ott is set. Re-setting set filetype=ott manually does enable the colors again.
Oddly enough, I also get colors on file opening when I change the extension in ftdetect/ott.vim to something other than *.ott.
Is there something special about the *.ott extension, or some clash with another file format?
au BufRead,BufNewFile *.ott set filetype=ott
" a.ott does not get highlighted
" Replace *.ott with *.ot, then a.ot gets highlighted
This is a conflict with the zipPlugin that is distributed with vim (/usr/share/vim/current/plugin/zipPlugin.vim), and which recognizes a .ott file as a zip archive. Hacky fix to remove that extension from those recognized by zipPlugin:
" ~/.vimrc
let g:zipPlugin_ext='*.zip'

Enable syntax highlight for file extension contained somewhere in filename

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.

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