How to use different colorscheme and syntax highlighting in vim?
Colorscheme and syntax highlighting are complementary in Vim, so you typically use one of each.
The colorscheme will define what color is applied to each type of element (such as Keyword, Identifier, String, Number, etc.)
While the syntax highlighting (for a specific filetype, typically a programming language) will define how to identify those elements for that language (for example, with a list of reserved words or regular expressions to delimit the specific items.)
Suppose I want to use [papercolor] colorscheme.
I copied the PaperColor.vim
file into .vim/colors
and made my .vimrc
: [...]
That's one possible way to install it, but I really recommend using a plugin manager instead! Read on.
Now, I want to use this syntax highlighting for haskell files:
So here you'll see more the value of a plugin manager! Since technically you're supposed to copy all the *.vim
files from every subdirectory into the corresponding subdirectory of ~/.vim
, but that quickly becomes pretty messy if you want to install many plugins. (Manually unpacking them means all the files would mix together!)
I really recommend using vim-plug, since it works great on Vim 7, Vim 8, Neovim, etc. Vim 8 and Neovim support "native" plugins but you need to manually handle the docs, so you benefit from using vim-plug on them too.
See the README file for installation instructions (you just need to download one file), then update your ~/.vimrc
to list the two plugins you're interested on:
call plug#begin('~/.vim/plugged')
Plug 'NLKNguyen/papercolor-theme'
Plug 'raichoo/haskell-vim'
call plug#end()
Then start Vim and run the :PlugInstall
command. Done!
If you use a plugin manager, you don't need to care about the other details... But in case you want to understand how it works, here it goes:
There are two syntax highlighting files. Which one am I supposed to use, and where do it put them?
They are for different filetype
s and they're loaded when a specific filetype
is set.
Do I put it in ./vim/syntax
and vim auto-loads all files in ./vim/syntax
folder?
Yes, Vim will load a file named xyz.vim
whenever filetype
is set to xyz.
So if you open a buffer, type some text and then use a command such as :setf haskell
it will load haskell.vim
from ~/.vim/syntax
.
(See :help 'filetype'
and :help :setf
for more details.)
It seems like to load haskell.vim
automatically. But doesn't load cabal.vim
.
That will be used for files of type cabal
, so using :setf cabal
will use that syntax highlighting configuration. (i don't know much about Haskell and Cabal but I imagine they're related files, with somewhat different syntax and distinct file extensions.)
Wondering if it only loads haskell.vim
when I open *.hs
files? I'm trying to make it like that.
The default Vim configuration already ships commands that will set up the correct filetype (haskell, cabal) for common extensions such as *.hs
.
The haskell-vim plugin also ships a ftdetect/haskell.vim
script that configures it for additional extensions, with the following commands:
au BufRead,BufNewFile *.hsc set filetype=haskell
au BufRead,BufNewFile *.bpk set filetype=haskell
au BufRead,BufNewFile *.hsig set filetype=haskell
As you can see, that's how plugins attach syntax (and indent, key mappings, etc.) for specific file types.
Can vim load multiple syntax files at once?
Yes, but that's quite unusual...
Here's an example from :help 'filetype'
:
When a dot appears in the value then this separates two filetype
names. Example:
/* vim: set filetype=c.doxygen : */
This will use the "c" filetype first, then the "doxygen" filetype.