I have a config file under my python project, called "logging.conf", the file looks like:
[formatters]
keys: console, logging
[formatter_console]
format: %(asctime)s | %(message)s
[formatter_logging]
format: %(message)s
etc etc etc
Tried :syntax on, nothing happened, the .conf files look very plain. Is there anyway I can turn on some syntax to make the .conf file more colorful and readable?
You can check vim.org or the Internet for a suitable syntax.
As a first approximation, this somewhat looks like DOS / Windows INI files. Vim comes with a syntax for them; try
:setf dosini
If that suits you, you can add a filetype detection rule:
:autocmd BufRead,BufNewFile logging.conf setf dosini
See :help ftdetect for details.
Your file looks plain, it is correct. ( I assume that you have already set conf as the filetype of your current buffer :set ft? to verify).
if you check your $VIMRUNTIME/syntax/conf.vim
you will see, there are three different colors will be shown in a conf file:
hi comment, lines starting with #
hi string, text wrapped by ' or "
and normal text
your current text has no comment, no quoted text. so it shows just in one color.
The file you show (python conf) is actually ini structure. try Ingo's answer.
I almost have that kind of text style but mine was the ansible hosts file, I found a lot of options here filetype.vim. I used povini or texmf for my ansible hosts file text highlights
To configure it in your .vimrc.
Grab the string before setf, so in case of provini, it is au BufNewFile,BufRead .povrayrc
Replace the last string(.povrayrc) with *.conf
The final config is au BufNewFile,BufRead *.conf for your .vimrc
The key issue here is that the syntax in your conf file is usually associated with .ini files. Setting the type (ingo's answer) fixes this issue. Alternatively you could just rename your file.
mv logging.conf logging.ini
echo "syntax on" >> ~/.vimrc
Setting .ini as the file extension maybe more desirable than having custom rules. (I do understand that this may not be possible in all cases)
Related
I'm on a system (linux) that always recognizes cpp files (*.cc) as tcl files. I don't know what file type that is, but I wanted to override it. The correct syntax highlighting is chosen if I manually do :set ft=cpp. However, I'm having troubles setting that automatically and I don't want to use the modeline option. My own .vimrc doesn't interfere (same result if I rename it).
From the vim help (:help ftplugin-override)
*ftplugin-overrule*
If a global filetype plugin does not do exactly what you want, there are three
ways to change this:
1. Add a few settings.
You must create a new filetype plugin in a directory early in
'runtimepath'. For Unix, for example you could use this file:
vim ~/.vim/ftplugin/fortran.vim
You can set those settings and mappings that you would like to add. Note
that the global plugin will be loaded after this, it may overrule the
settings that you do here. If this is the case, you need to use one of the
following two methods.
I have used this option before on another machine and that worked. I've tried
<file> .vim/ftplugin/tcl.vim
set filetype=cpp
"au BufRead,BufNewFile * set filetype=cpp
The first line correctly sets the filetype (:set ft? returns cpp), but syntax highlighting is not the same as if I said :set ft=cpp. It's still the tcl syntax highlighting. The second line does nothing.
2. Make a copy of the plugin and change it.
You must put the copy in a directory early in 'runtimepath'. For Unix, for
example, you could do this:
cp $VIMRUNTIME/ftplugin/fortran.vim ~/.vim/ftplugin/fortran.vim
Then you can edit the copied file to your liking. Since the b:did_ftplugin
variable will be set, the global plugin will not be loaded.
A disadvantage of this method is that when the distributed plugin gets
improved, you will have to copy and modify it again.
There seems to be no file in my $VIMRUNTIME directory /usr/share/vim/vim72/ftplugin/ called tcl.vim.
3. Overrule the settings after loading the global plugin.
You must create a new filetype plugin in a directory from the end of
'runtimepath'. For Unix, for example, you could use this file:
vim ~/.vim/after/ftplugin/fortran.vim
In this file you can change just those settings that you want to change.
Has the same effect as 1. Is there anything else I can try? Thanks a lot in advance.
cpp is the default filetype for *.cc and *.cpp files.
The tcl filetype is only set for *.tcl, *.tk, *.itcl, *.itk and *.jacl.
I see no reason why Vim would default to tcl when loading a *.cc file but you could check if theses files are installed:
/usr/share/vim/vim7x/ftplugin/cpp.vim
/usr/share/vim/vim7x/indent/cpp.vim
/usr/share/vim/vim7x/syntax/cpp.vim
and if the correct checks are done in:
/usr/shhare/vim/vim7x/filetype.vim
Are you the only person working on this machine? Do you use modelines?
So I am using vim (vi) to edit on command line. Whenever I code in a file that ends in .php, .pl, .cgi, .pm, etc, it matches it up with what language it is and does the proper syntax highlighting. However, I am writing some perl scripts and I am requiring some separate files with the extension ".lib". Is there a way that I could have vim interpret this as a .pl file? right now it just highlights everything in red and looks pretty bad.
:set filetype=pl, if you want this to happen all the time, add
au BufNewfile,BufRead *.lib set filetype=pl
to your .vimrc
You're probably looking for the autocmd command, which will execute some commands based on filenames (or other criteria?)
Try adding something like this to your ~/.vimrc file:
autocmd BufNewFile,BufRead *.lib set syntax=perl
I usually have to read .txt files with long lines, and at the same time edit some source file, and I like to see word wrap on the .txt files, and not in the ones that aren't.
Of course I can :set wrap and :set linebreak, but is there any way to make it automatucally, and dependent of the file extension?
There are two options that I can think of. Firstly, you can use an autocmd as suggested by Tassos:
:au BufNewFile,BufRead *.txt set wrap
See:
:help autocmd
An alternative (that is probably more applicable if you've got multiple settings as you have suggested): create a file in the after/ftplugin directory of your vim configuration folder (see below) called txt.vim and it will be sourced whenever you open a .txt file. You can put it in the plain ftplugin directory (rather than after/ftplugin), but it any built-in settings for .txt files will then not be loaded.
Put any commands you want in this file:
" This is txt.vim in the ftplugin directory
set wrap
set linebreak
See:
:help after-directory
:help ftplugin
Vim Configuration Folder
On Windows this would typically be something like:
C:\Documents and Settings\%USERNAME%\vimfiles\after\ftplugin\txt.vim
(I think), or
C:\Program Files\Vim\vimfiles\after\ftplugin\txt.vim
or even:
C:\vim\vimfiles\after\ftplugin\txt.vim
On Linux, it is:
~/.vim/after/ftplugin/txt.vim
For more info, see:
:help runtimepath
I guess :autocmd BufNewFile,BufRead *.txt set wrap should do the trick
you can do lot more with autocommand, refer here: http://www.thegeekstuff.com/2008/12/vi-and-vim-autocommand-3-steps-to-add-custom-header-to-your-file/
I have the following in my .vimrc
syntax on
filetype plugin indent on # Thanks to Jeremy
I run
vim ~/.vimrc
I get the right syntax highlighting.
I source many files in my .vimrc. My .vimrc is a like a roadmap for me where I navigate by
CTRL-W f
The problem occurs when I navigate to a file which I have sourced: no colors.
All my sourced files contain the word Vim in their PATHs.
It may be possible to use this fact in solving the problem.
How can you provide a syntax highlighting automatically for the sourced files?
Do the files in question end in ".vim"? If not, then vim's filetype detection may not be able to determine that these files contain vim-script. You can either rename the files so that they end in .vim, or add an autocommand to set the filetype appropriately.
To do the latter, you can add something like this to your .vimrc:
au! BufNewFile,BufRead PATTERN set filetype=vim
replacing "PATTERN" with a file pattern that will match the files in question.
EDIT:
See :help autocmd-patterns for how the patterns work:
The file pattern {pat} is tested for a match against the file name in one of
two ways:
1. When there is no '/' in the pattern, Vim checks for a match against only
the tail part of the file name (without its leading directory path).
2. When there is a '/' in the pattern, Vim checks for a match against the
both short file name (as you typed it) and the full file name (after
expanding it to a full path and resolving symbolic links).
In particular, note this example:
Note: To match part of a path, but not from the root directory, use a '*' as
the first character. Example: >
:autocmd BufRead */doc/*.txt set tw=78
This autocommand will for example be executed for "/tmp/doc/xx.txt" and
"/usr/home/piet/doc/yy.txt". The number of directories does not matter here.
In your case you probably want something like:
au! BufNewFile,BufRead */Vim/* set filetype=vim
To make vi consider my jQuery (.jq) files are actually javascript (.js) I did: -
Create and/or or edit your vimrc file ...
e#dev3:~$ vi ~/.vimrc
Add the following text (press i to insert) ...
if has("syntax")
syntax on
filetype on
au BufNewFile,BufRead *.jq set filetype=javascript
endif
Save the vimrc file ...
[esc]:wq[enter]
Further, to find supported filetypes look in filetype.vim ...
e#dev3:~$ sudo locate filetype.vim
/usr/share/vim/vim72/filetype.vim
e#dev3:~$ sudo grep "\.js[, ]" `locate filetype.vim`
au BufNewFile,BufRead *.js,*.javascript,*.es,*.jsx setf javascript
... the filetype is the setf arg ...
e#dev3:~$ sudo grep "\.js[, ]" `locate filetype.vim` | cut -d " " -f 4
javascript
Have fun.
What is the extension of the files you source? The extension is the usual way for Vim to detect what syntax highlighting it neds to use, and for source-able files (vimscript) it should be .vim. It sounds like that's not the case, if you only see the problem with the sourced files, and not with any others.
One obvious question is there's no line saying "syntax off" in the files you're sourcing?
It could be:
the "filetype" option
the filetype might not be auto-detected by vim
filetype on sorts the first, and the second is fixable with autocmds based on the file extensions.
I've looked at this but it wasn't too much help. Maybe I didn't read it too well.
Basically what I want is when I open a .txt file the settings:
set wrap
set linebreak
are turned on. How might I go about doing that?
Thanks in advance.
Also, I'm using XP.
My answer to that question still applies:
Put autocmd commands based on the file suffix in your ~/.vimrc
autocmd BufRead,BufNewFile *.txt set wrap linebreak
As Luc says, you might prefer to
autocmd BufRead,BufNewFile *.txt setlocal wrap linebreak
if you're likely to open txt and non-txt files at the same time.
Put this into ~/.vim/ftdetect/text.vim (this path will be slightly different on windows):
autocmd BufRead,BufNewFile *.txt setfiletype text
Then put this into ~/.vim/ftplugin/text.vim:
setlocal wrap
setlocal linebreak
It's preferable to only do the autocmd once for a filetype, and to separate it from your vimrc file.
A good solution to this is the "after" directory. You can add an rc file for anything you want in there, syntax highlighting, file types, etc. These configurations are run after all other configurations are run, so after the system configs and after your .vimrc. So you can create, on a unix type system, an file called ~/.vim/after/ftplugin/text.vim and add the two lines you want in there. They options will be set for the text file type, but not for other file types. You can have different files in each of those directories for other filetypes, such as perl.vim.
Since you are not in a unix environment, you will need to find your [runtime directory][1] by checking the [runtimepath][2] option. You would create your "after" directory and the files there.
NOTE:
The links are not working for me, probable because of the anchors:
After directories are briefly
mentioned here:
http://www.vim.org/htmldoc/usr_43.html#43.2
Runtime directories:
http://www.vim.org/htmldoc/usr_43.html#your-runtime-dir
Runtime path:
http://www.vim.org/htmldoc/options.html#'runtimepath'