autocmd does not detect *.ott files - vim

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'

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 syntax doesn't highlight in real time

I have enabled vim syntax on (in ~/.vimrc syntax on) and it works but only on files with a code in when I view them. When I create a new file with vim and write there some code - no syntax highlight. After saving this file and reopening with vim - syntax highlight works perfect.
I am using manjaro KDE.
When you open a new file without an extension (vim mynewfile) none of vim’s filetype detection mechanisms can recognize it (they all use either extensions or first-couple-of-lines heuristics, which don’t work here).
When you enter code and reopen the file, the line-checks for filetypes work, causing the syntax to be set correctly, causing highlights to apply.
You can always set syntax=mine (though set filetype=mine is better) to set it manually.
This problem shouldnt happen when you do vim some.c or similar, because the extension will force detection based on extension rules.
Vim must know how to highlight your syntax in order to actually highlight it. One way to do this, is for Vim to check the file name and sometimes inspect the contents of the file, and set the file type. The file type is then used to highlight the syntax.
To enable detection of the file type (and load plugin and indent files), add the following to your vimrc:
filetype on plugin indent
If Vim is unable to detect the file type, and you have not yet saved your file with a known extension, you can set the file type manually, like this:
:set filetype=html
Vim will then highlight the syntax of the file as HTML syntax.
More information is available in the help pages.

Vim: Auto Replace Tabs with Spaces on Save

My vim always automatically converts tabs into spaces whenever I save (:w) a Go file (*.go), which is not what I want.
My vim is cloned from https://github.com/freedombird9/vim_anywhere. Besides that, I installed vim-go with Vundle.
I looked my .vimrc and the plugin folder for possible configs, basically configs like autocmd BufWritePre and :write that could change the save behavior. But I did not find anything that suggests there's a conversion before save. I also didn't see such a setting in vim-go.

Syntax highlight not working in Janus for Vim

I am using Linux Mint 13 Maya Cinnamon 64-bit. My Vim version is 7.3 and I installed the latest version of Janus.
I found that for any files with a hash "#" in its content, the syntax highlight for the file does not work. For example,
# test
print "Hello"
The 'print' has color while I am editing the file. But when I save it and open it again the whole file loses syntax highlight.
If I deleted the first line and save, the syntax highlight comes back after I open it again.
This applies to all kinds of files such as .py, .c and .h. If there is a hash "#" character in the file, syntax highlighting does not work.
I have already tried "syntax on" but nothing changes.
I don't know Janus so this answer might not be 100% useful for you, but let's see. You could try finding out where the settings have been set. Try this:
Get current settings:
:set filetype? syntax?
Check where these have been set:
:verbose set filetype? syntax?
Execute these commands when you lost your syntax highlighting:
:syntax on
:set ft=python
:verbose set ft? syn?
Here you should see which script changed your filetype after saving. Normally, vim uses heuristics to determine the correct filetype if the file extension is ambiguous. In cases where these heuristics don't work, you usually set a global variable in your vimrc to a fixed value. In your case this would be something like:
let g:filetype_py="python"

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