I have a master .vimrc that sets up my global working environment, including special setup for both C++ and Python filetypes.
In a project folder I have an .exrc with project-specific settings for C++, but this is overriding my master .vimrc settings for Python.
How can I create an autocommand that sets these options only if it is not a python file? I don't want to wrap the project C++ settings in an autocommand because we have multiple source file types and that means a lot of duplicated code in .vimrc as well as the potential to miss a file type.
What I really want is a construct like this:
if Filetype python then
...
else
...
endif
But it would be sufficient to simply do this:
if not Filetype python then ...
if &filetype != 'python'
&filetype gets the value of the current file type.
Related
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.
I tried including the following syntax file into my vim.
I installed this plugin using Pathogen as per instruction:
git clone https://github.com/jelera/vim-javascript-syntax.git ~/.vim/bundle/vim-javascript-syntax
Then included the following in my .virc file
au FileType javascript call JavaScriptFold()
After restarting vim and opening a JavaScript file I am not able to fold using the standard zo, zc commands .. any clue ?
Check whether the fold settings have been activated:
:verbose set foldmethod?
:syntax list foldBraces
The first should yield syntax, and the syntax group should be defined.
Note that only { ... } blocks spanning multiple lines are folded by this.
For anyone else who may come across this scenario; I had the exact same problem. The fix, for me, was to edit my .vimrc by moving
syntax enable
above
au FileType javascript call JavaScriptFold()
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.
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
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.