The default behavior in Vim is to syntax highlight special characters such as Numbers, booleans, etc. However when working with comments I find this to be a major distraction since Vim is highlighting them by default. I'd like to keep any syntax coloring in a comment to be the same all across the board. (in my case, light gray)
What is the colorscheme or vimrc setting to maintain a a consistent comment color?
Also unlet c_comment_strings does not resolve the issue.
This is not about particular color scheme. This is about syntax matching rule that allows to find strings/numbers even inside comments. So it depends upon a language.
For C/C++ it is governed by existence of c_comment_strings variable (see also :h c.vim). And it is not a true default. That variable either intentionally defined in your vimrc or it comes from standard defaults.vim. In latter case you need to unlet it.
For other languages refer to Vim docs on a particular syntax.
Related
I'm having problems with auto indentation in vim while programming in lisp.
My .vimrc had the following settings for tabs:
set noexpandtab
set autoindent
set softtabstop=0
set shiftwidth=8
set tabstop=8
When I type (if <enter> in insert-mode, a new line is created with an indentation of two spaces.
None of my settings say anything about two spaces, so why don't I get a tab?
What setting can I use to change the indentation while in insert-mode?
Thanks in advance!
Update:
Thanks for the answers, the settings are not overwritten.
It has to do with the "default lisp indenting".
In the :help lisp it says something about changing the p flag in
cpoptions. This is what it says in the help for the cpoptions flags:
p - Vi compatible Lisp indenting. When not present, a slightly better algorithm is used.
Setting it does change the indent to one space instead of two spaces.
Still not sure how to change this to something else though.
Looks like this is two-space indentation is a hard coded behavior of :set lisp mode which ignores shiftwidth.
We can trace this to the source code which contains a hard-coded amount += 2; increment statement.
It's that way for a good reason; it has come to be the predominant way of writing Lisp.
As I wrote this answer, I peeked at samples of source code the following projects (all Lisp or Lisp-like languages):
Steel Bank Common Lisp (SBCL);
Clozure Common Lisp (CCL);
GNU Emacs;
Clojure;
GNU Guile;
Racket;
and GNU CLISP.
I did not spot a single example of anything but two-space indentation! With two-space indentation, you are in good/large company. You might as well get used to that; even if you somehow get Vim to follow your way, if you upstream anything into anyone's Lisp project, you will likely have to reformat.
Now, I have seen Lisp code using four-space indentation, like in very old code bases and some books.
By the way, sometimes you see if indented like this:
(if (condition)
(then)
(else))
This may happen where indentation is four spaces, but I'm referring situations when this is alignment, and not four space indentation. This formatting is, of course, standard for function arguments, but controversial for operators such as if. Some people like it that way in their code bases. For instance, this institution's randomly found coding style guide recommends this way of writing if, while also recommending two-space indentation.
Vim will do the above if you remove if from lispwords.
The :set lispwords=... parameter contains a comma-separated list of identifiers which follow operator-like indentation, meaning two spaces rather than function-like alignment: second and third lines align with argument. By default, if is found in lispwords.
Lisp mode also applies two space indentation to a function (i.e. form not listed in lispwords, if there is no argument):
(gobbledygook 1 2
2 3)
(gobbledygook
1)
That's also "canonical". Sometimes this comes in handy if you're trying to conform to some maximum line length; a function call running past the limit can sometimes be made to fit by moving all its arguments down, and just giving them two space indentation.
Each filetype can have its own settings in vim. So your .vimrc values can be overwritten in filetype plugins. To learn the current values of the settings for lisp filetype open the lisp file and run the following commands in vim command line:
:set noexpandtab?
:set autoindent?
:set softtabstop?
:set shiftwidth?
:set tabstop?
If they are different from the ones in .vimrc, then your values are overwritten in some plugin.
In order to overwrite them again with your custom values, create the ~/.vim/after/ftplugin/lisp.vim file with the required values:
set noexpandtab
set autoindent
set softtabstop=0
set shiftwidth=8
set tabstop=8
I have tried well over 15 different colorschemes for vim. I have made all of the correct settings for full color support in terminal. This 'problem' persists in both terminal and gvim. Perhaps it is not a bug and simply the design of the colorschemes themselves, but only one colorscheme I have tried actually has decent highlight support. For example:
In this python class all of the colorschemes will only highlight a couple of things.
wombat only highlights comments and the if.
molokai is extremly dissapointing but at least gets the ints
All of the themes I try are similar to these two except 'Crayon' which compares like so:
Most of the vim color-scheme github pages show previews with highlight support compared to crayon. My question is what might be causing this problem? or is this just the design of the themes themselves?
Syntax highlighting is the combination of two things:
syntax definition, provided by syntax scripts typically found in syntax/ or after/syntax/,
highlighting definition, provided by colorschemes typically found in colors/.
The former defines syntax groups, the latter defines how those groups look.
But those almost never come in pairs so there's no guarantee whatsoever that every possible syntax group is properly handled by every possible colorscheme.
If a colorscheme you like doesn't handle some of the syntax groups you expect it to handle, open an issue or (better) patch it.
I have tried highlighting specific words by adding them in .vimrc, colorscheme file and also in syntax.vim(I changed one at a time, not altogether).
syn match mygroupwords '\c\<\(-word1\|-word2\)'
hi def link mygroupwords colo_words12
hi colo_words12 guifg=red1 gui=bold guibg=white
But somehow it seems to be getting overwritten by default syntax highlighting
i need to highlight keywords irrespective of color-scheme or file-type which have following words-
Ex; -word1 , -word2
Any suggestions?
explanation of your failed attempts
A colorscheme just provides color definitions for predefined highlight groups; that's the wrong place for actual syntax matches! ~/.vimrc is the first configuration that is read; if a filetype is detected and a corresponding syntax script is loaded, that will override your syntax definition.
syntax extensions
If your desired highlightings are extensions to an existing syntax, you can place :syntax match commands in a syntax script in the after directory. For example, to extend Python syntax, put it in ~/.vim/after/syntax/python.vim. That may still fail if the original syntax obscures the match; sometimes, this can be solved via containedin=...
independent marks
If your highlightings are independent of syntax and filetype, there's a different built-in mechanism: :match (and :2match, and additional variants via :call matchadd(...)):
:match mygroupwords /\c\<\(-word1\|-word2\)/
This goes on top of (and is independent of) syntax highlighting. However, it is local to the current window. So, if you put this into your .vimrc, it will only affect the first window (but any files viewed in there). To apply this globally to window splits and tab pages, you have to use :autocmds. It's not trivial to get this totally right. If you need such a full solution, have a look at my Mark plugin; this supports multiple colors, allows presetting with :[N]Mark /{pattern}/ (similar to :match), and highlights in all windows.
I would like to be able to format comments in code somewhat:
# **TODO**
#
# *Don't forget*
I want markdown syntax to be highlighted a bit but only in comments, add cterm=bold for \*\*(.*)\*\* or something.
Is there a way to do it in Vim?
To do this properly, you'd have to :syntax include the markdown syntax into each individual syntax (that has comments that you're interested in), and have it containedin= the corresponding syntax group for comments. That's doable, but requires some effort (depending on how many and which syntaxes to modify).
It's quicker to use a plugin like my SyntaxRange plugin to change the syntax of those comment blocks to markdown (manually, and on demand). With a corresponding mapping, this can be set up quite easily (but it's not automatic as the first alternative).
I want to used different colorschemes for different filetypes and I added the following code in my .vimrc
function SetColorScheme ()
if &filetype != "vo_base"
colorscheme desertEx
endif
endfunction
au WinEnter * call SetColorScheme()
This works fine with one issue.
If I open a .otl file, say todo.otl (vo_base), and then open another file, say example.xml, using :sp the colorscheme desertEx does not get applied to the second window (the one having example.xml).
If I use BufEnter instead of WinEnter than desertEx gets applied to both the windows.
Is there a way to make sure that when I open a window with :sp the above function (a) runs, and (b) runs only for that particular window and not all the windows in the current session.
No there's no way to do that. There can be only one active colorscheme at the same time in vim.
Something that comes to mind is to create a color scheme that directly points to the low-level syntax groups in the Vim syntax files.
Take for instance c.vim for the C programming language. You will find for instance syntax hightlighting groups such as: cStatement, cLabel, cConditional, cType.. etc.
Take python.vim and you will find pythonDefStatement, pythonFunction, pythonConditional, etc..
So, if you want to use different color schemes for C code and python, you would copy the two original color schemes to ~/.vim/colors/mycolorscheme.vim and edit them to point to the low-level syntax groups instead of the generic high level groups such as Comment, Constant, Error, Identifier, etc. that are found in many available color schemes.
Note that you would probably want keep a default stanza of 'highlight' statements on top of these other two to take care of syntax highlighting for files that contain neither C nor python code.
To clarify, you could edit the celebrated 'Hello World' code and issue the following from the Vim command line:
:hi cInclude ctermfg=cyan guifg=cyan
You have not changed color schemes, other files displayed in other windows or tabs are unaffected, and yet the '#include' is now displayed in a different color.
Unless you absolutely need the feature, I would advise against it, because it pretty much breaks Vim's syntax highlighting. Besides, it will require significant work to convert the existing ':hi' statements comprised in the original color schemes because there are usually many low-level syntax highlighting groups.
A somewhat better approach might be to link the language-specific low level groups to high-level groups that are also specific to each language. This would help keep the custom color scheme file reasonably small, but requires additional inventory work.