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
Related
I want to change the filetype based on file extension in vim.
I have the following code in the my .vimrc
autocmd BufNew,BufNewFile,BufRead *.txt,*.text,*.md,*.markdown setlocal ft=markdown
But when I open a file with the extention .md file, the filetype is not changed. I run :set ft command and it shows the output as filetype=modula2.
Am I doing anything wrong?
Edit:
I started to debug by renaming my old .vimrc file and created a new one with just this line. It was working properly. Then I replaced my old .vimrc file and everything seems to be working fine. Guess it was because of some issues in some addon which I am using.
But accepting ZyX's answer, since it thought me an alternate way to do this.
I created a ~/vim/ftdetect/markdown.vim file with this line
autocmd BufNewFile,BufRead *.md,*.mkdn,*.markdown :set filetype=markdown
Reading the docs for filetype, setfiletype only sets if filetype is unset. So you need to use set for an unconditional change to a filetype.
Wondering whether this line goes before or after filetype … on. In the former case you should try putting it (your autocommand) after this line. Better if you put it into ~/.vim/ftdetect/markdown.vim and use setfiletype markdown instead of setlocal ft=markdown:
augroup filetypedetect
autocmd BufNew,BufNewFile,BufRead *.txt,*.text,*.md,*.markdown :setfiletype markdown
augroup END
: it is the default way of doing such things. ~/.vim must go before /usr/share/vim/* paths in 'runtimepath' option in this case (it does by default).
I was able to get syntax highlighting for alternate file extensions by creating renamed copies of the target syntax file in the Vim\vim74\syntax directory.
To make *.md open as a .markdown:
copy markdown.vim md.vim
or paste a copy of markdown.vim to the syntax folder, then rename the copy md.vim.
(Running vim74 on win7)
When I open some bash script files with vim it sometimes identifies them as conf files, that's okay, I can just correct that by setting the filetype to sh with :setf sh.
That great, except I've noticed that this doesn't fix things entirely:
Notice that shopt is properly highlighted on the left, but not on the right, where I manually set the filetype to sh.
This means that when a file is identified as bash or sh by vim, it sets the filetype to sh but then does some extra steps that I'm not doing when I set the filetype manually.
Any one know what that might be, and how I could fix it?
vim already recognizes many file types by default. Most of them work by file extensions, but in a case like this, vim will also analyze the content of the file to guess the correct type.
vim sets the filetype for specific file names like .bashrc, .tcshrc, etc. automatically. But a file with a .sh extension will be recognized as either csh, ksh or bash script. To determine what kind of script this is exactly, vim reads the first line of the file to look at the #! line.
If the first line contains the word bash, the file is identified as a bash script. Usually you see #!/bin/bash if the script is meant to be executed directly, for some other shell configuration file you should use the file extensions .bash.
The help in vim explains this as well at :help ft-bash-syntax. You can also use let g:is_bash=1 in your .vimrc to make bash syntax highlighting the default for all files with filetype=sh. If you want to look at the details, this is implemented in $VIMRUNTIME/filetype.vim.
It turns out that syntax/sh.vim includes specific highlighting for Korn, Bash and sh, you just have to tell it which you're using. This is done with b:is_kornshell, b:is_bash and b:is_sh respectively.
Depending on the situation I figure I'll use the following:
ftdetect/bash.vim:
au BufRead,BufNewFile *bash* let g:is_bash=1
au BufRead,BufNewFile *bash* setf sh
Modeline:
# vim:let g:is_bash=1:set filetype=sh:
Key Mapping
nmap <silent> <leader>b :let g:is_bash=1<cr> :setf sh<cr>
Similar to Peter Coulton's solution and documented as well as an alternative in the section "new-filetype" of the "filetype" Vim help the ~/.vim/filetype.vim file could contain the following code:
if exists("did_load_filetypes")
finish
endif
augroup filetypedetect
au! BufRead,BufNewFile *bash* let b:is_bash = 1 | setfiletype sh
augroup END
This approach has the following implications:
There is one ~/.vim/filetype.vim file instead of one for each file type under the ~/.vim/ftdetect directory.
The b:is_bash variable is set local to the buffer as opposed to global by referring to it as g:is_bash.
Try viewing the effective syntax setting
:windo echo b:current_syntax
(I kind of expect the first window to say bash, and the second to say sh...?)
Also try mucking with the synatx synchronisation:
:windo syn sync fromstart
:windo syn sync minlines=300
In general
:he syn-sync
for more information
PS.
A long shot, but some other highlighting might be interfering:
:windo se #/=''
:match none
:2match none
:3match none
In Vim, I want to use a different colorscheme for each file type.
e.g. I want to use desert256 colorscheme for Python & JavaScript files, and use jellybeans colorscheme for HTML & CSS files.
I've tried putting the following code in my .vimrc, but the colorscheme change happens only when changing buffers for the first time.
i.e. If I open a new Python file, Python's colorscheme is used, and when I open a new CSS *buffer*, indeed the colorscheme changes to CSS's colorscheme. However, Changing back to Python's buffer does not change the colorscheme back.
I've used autocmd WinEnter to try and make this rule happen when changing windows (and buffers), but it doesn't help:
autocmd WinEnter,FileType python,javascript colorscheme desert256
autocmd WinEnter,FileType *,html,css colorscheme jellybeans " This includes default filetype colorscheme.
How can I fix this? In addition, a bonus would be to not change a colorscheme when not needed - i.e. Changing from a Python to a JavaScript buffer won't change the colorscheme to "itself".
EDIT:
If anyone's interested, here is my .vimrc repo in github.com. I'll update it with the solution I find here once given.
I've been looking for the same thing. This inside your .vimrc works reasonably well although not perfect.
autocmd BufEnter * colorscheme default
autocmd BufEnter *.php colorscheme Tomorrow-Night
autocmd BufEnter *.py colorscheme Tomorrow
(Note if you're looking for a good dark color theme Tomorrow-Night looks pretty good. Very similar to theme used on Code Academy.)
What you want are filetype plugins, rather than the autocmds. Run help: ftplugin in vim for more info.
From the vim help page:
A filetype plugin is like a global plugin, except that it sets options and
defines mappings for the current buffer only.
In order to use filetype plugins, first put the line filetype plugin on in your vimrc. Then create the folder ftplugin in your vim folder (on unix it's ~/.vim/, I'm not familiar with windows). Then create a script file for each file type you want to customize. These files must be named a specific way. From the vim help page:
The generic names for the filetype plugins are:
ftplugin/filetype.vim
ftplugin/filetype_name.vim
ftplugin/filetype/name.vim
So, for example, if I wanted to create a script for a python file, I would have three options:
Create a file named python.vim in ftplugin
Create a file named python_whatever.vim in ftplugin
Create a file named whatever.vim in ftplugin/python
This script will then be loaded anytime I open a file that vim recognizes as a python file.
So, in order to accomplish what you want:
Create a file named filetype.vim in the ftplugin directory for every filetype you want.
In each of these files, add the line colorscheme name_of_colorscheme
Add filetype plugin on to your vimrc.
In order to set a default colorscheme, just set it in your vimrc file. If I remember correctly, filetype plugins are loaded after your vimrc.
Edit:
The OP indicated that he had a good reason to avoid using the ftplugin directory. After a bit more diggin, I found this script. It can be placed in the global vimrc and seems intended to solve the same problem as the OP.
I have a hack you may like. It is far from perfect, and it doesn't use a .vimrc, but it works for me. It requires you to type a different command to edit different files. It works using the -c parameter when you call gvim. This argument allows you to run vim commands after loading the file. Add this to your ~/.bashrc ( I guess you are using bash ) :
alias gpy="gvim -c 'colorscheme desert'"
alias gcs="gvim -c 'colorscheme jellybeans'"
Hope this helps
Use BufWinEnter instead of WinEnter, like this:
autocmd BufWinEnter,FileType javascript colorscheme jellybeans
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'