Alternate indentation display in Vim - vim

I came across this question:
https://softwareengineering.stackexchange.com/questions/87077/how-can-a-code-editor-effectively-hint-at-code-nesting-level-without-using-inde
and thought that Vim might be able to do something similar as well with a plugin.
I believe the indentation level could be indicated with the a sign (icon in GUI, text with highlighting in term). The piece I'm not sure about is displaying the lines without indentation. Does anyone know, is it possible and/or how you would do it?
This question is not whether or not displaying indentation levels in this manner is desirable; but how to accomplish it in Vim.

You could use the conceal feature that is new in 7.3.
Here is a function that does roughly what the article is describing (for spaces; accounting for tabs instead would be a fairly trivial addition):
function! IndentationHeatMap()
set conceallevel=1
for i in range(1,9)
let indentation = repeat(" ", &sts * i)
exe 'syntax match NonText "^' . indentation . '" conceal cchar=' . i
endfor
endfunction
A solution closer to what you are requesting might use conceal to hide all leading whitespace with
syntax match NonText "^\s\+" conceal
and then use signs to provide the indicators based on custom calculations.
Note: NonText in these syntax commands is an arbitrary highlight group.

Take a look at these plugins: Indent Guides and IndentHL
Both have screenshots.

Related

Render boundary space in Vim (just like VSCode)

I am pretty noob at Vim still, pardon my ignorance
"Boundary" whitespace is any whitespace except single spaces between words, paraphrasing from VSCode docs:
I want to mimic this same behavior in Vim (ideally with no plugins).
An example of how this feature looks in VSCode:
There aren't many resources that I found on this topic. There is Render space if it have multi space in Vim, which addresses this exact same feature (previous example image shown is from that question), but the provided solution does not work exactly as expected.
This is an example of how boundary whitespaces look after trying out that stackoverflow solution:
While the '·' character is shown when there are 2 or more spaces in a row, which is desired, they are also shown highlighted, which is not what I expected
Moreover, if I place the cursor on a line that contains consecutive spaces, they disappear (or are highlighted into invisibility, whatever is happening that renders them invisible)
Excuse my newbie comments in my .vimrc; the rest of the file is commented out
This is another example, in VSCode, of the desired behavior:
Am I missing something from that previous question? Is the highlighting of whitespaces normal or a different issue on its own? Thanks in advance!
The feature you are looking for can be enabled with :help 'list' and customized with :help 'listchars':
The result above can be obtained by adding the following lines to your vimrc:
set list
set listchars=eol:↵,lead:·
If your color scheme makes those characters too visible, you can override its NonText and SpecialKey highlight groups (mentioned in :help 'listchars') to make them more bearable. For example:
augroup MyColors
autocmd!
autocmd ColorScheme * hi NonText ctermfg=235 | hi SpecialKey ctermfg=235
augroup END
colorscheme foobar
Note that listchars was more limited at the time the linked question was answered so, while it was more or less valid at the time, it is no longer necessary to use the conceal feature for this.

Inconsistent vim highlighting throughout various syntax

I'm building a color scheme from scratch and as I came past the syntax group I noticed a lot of inconsistency overall.
hi Comment ctermfg=129 ctermbg=129 cterm=italic
hi Boolean ctermfg=3 ctermbg=none cterm=bold
The comments are basically ignored as they should be both purple and italic, instead they're both a different color and not italic. The booleans as well both show a different color but they do appear to be bold.
hi String ctermfg=10 ctermbg=none cterm=italic
The string on the other hand has no trouble whatsoever.
I used a Javascript and Python file for testing here but it messes up everywhere (CSS, HTML, Rust, C, Shell).
I'm using rxvt-unicode and have no trouble assigning the purple color to the string.
Kind of unsure on how to proceed here. What could be the problem?
First ensure that you are looking at the correct line, i.e., that code is using the highlight group that you think it should. You could use the following mapping from the vim tips:
map <F10> :echo "hi<" . synIDattr(synID(line("."),col("."),1),"name") . '> trans<'
\ . synIDattr(synID(line("."),col("."),0),"name") . "> lo<"
\ . synIDattr(synIDtrans(synID(line("."),col("."),1)),"name") . ">"<CR>
By hitting F10 it will display the highlight group off the word under the cursor.
If the problem remains you could:
switch the terminal to check if it the problem is specific to urxvt
ensure that no plugin/setting is interfering by following the procedure on Vim FAQ 2.5 - "I have a "xyz" (some) problem with Vim. How do I determine it is a problem with my setup or with Vim? / Have I found a bug in Vim?"
check the Vim FAQ on syntax highlighting
You may also be interested on ColorSchemeEditor plugin:
This plugin provides a GUI tool which simplifies creating/editing Vim
colorscheme files. It consists of a Vim plugin as well as a Python
program, and utilizes Vim's command server interface |clientserver|
for communications.

remove the highlight for the current parenthesis but keep it for the matching pair

When I work in a fast paced with vim, one of the recurring annoyance is highlighting of {} or {} or []. When the parenthesis or brackets are adjacent to each other, it takes me a second (yes a whole second!) to figure out where the focus is. I do not like having to rely on the blinking and I still want the matching paren/bracket to be highlighted. So how can I remove just the highlighting for the current paren/bracket?
:hi MatchParen ctermbg=black guibg=black
show match is ok, but the highlight was driving me nutz. The above turns the highight black so you don't see it when editor has a black background
:NoMatchParen
type :help matchparen for more help on this .
this functionnality is really useful for me, so maybe you can look at the doc a little and find a way to hightlight the matching pair in a different color (or just underline it for example)
What you are looking is
set noshowmatch
to be set in vimrc

How to create a syntax rule that can be contained anywhere?

The following snippet from my .vimrc highlights superfluous whitespace at line ends in a shade of gray:
autocmd Syntax * syntax match MySpace /\s\+$/
autocmd ColorScheme * highlight MySpace ctermbg=238
But this does not work when this whitespace is already matched by a syntax group. For example, trailing whitespace in various types of comments is not marked.
The manual talks about the contains=ALL option for syntax groups, but there seems to be no analogous containedin=ALL. Can I emulate it in any way? The only method I could come up would be to list all relevant syntax groups in the containedin= option of MySpace, and that's clearly tedious and not at all elegant.
Don't know how to do this with Syntax, but you can use the listchars options to highlight trailing spaces.
From my .vimrc:
" List chars
set listchars="" " Reset the listchars
set listchars+=tab:\|\ " show tabs as "|"
set listchars+=nbsp:· " show non-breaking spaces as "·"
set listchars+=trail:· " show trailing spaces as "·"
set listchars+=precedes:«
set listchars+=extends:»
You should use the :match command (or matchadd()), as described in the Vim Tips Wiki article about this particular topic.
If you like a ready-to-use solution for this, you can also try out my ShowTrailingWhitespace plugin, or one of the alternatives listed on the plugin page.

How do I make vim syntax highlight a whole line?

I'd like to have vim highlight entire lines that match certain patterns. I can get all the text in a line to highlight (by doing syn match MyMatch "^.*text-to-match.*$"), but it always stops at the end of the text. I'd like to to continue to the end of the term, like highlighting CursorLine.
I've tried replacing $ with a \n^, hoping that would wrap it around. No change. (I didn't actually expect this would work, but there's no harm in trying.) I also tried adjusting the syn-pattern-offset (which I read about here: http://vimdoc.sourceforge.net/htmldoc/syntax.html#:syn-pattern). Long story short, adding he=he-5 will highlight 5 fewer characters, but he=he+5 doesn't show any extra characters because there aren't characters to highlight.
This is my first attempt at making a vim syntax and I'm relatively new to vim. Please be gentle and include explanations.
Thanks!
(edit: Forgot to include, this is a multiline highlight. That probably increases the complexity a bit.)
It's not very adaptive as the filename (buffer) and line to full row highlight needs to be explicitly identified, but apparently the sign command can be used:
It is possible to highlight an entire
line using the :sign mechanism.
An example can be found at :help
sign-commands
In a nutshell:
:sign define wholeline linehl=ErrorMsg
:sign place 1 name=wholeline line=123 file=thisfile.txt
Obviously, you should pick a higlight
group that changes the color of the
background for the linehl argument.
source: Erik Falor, vim mailing list
From the documentation on syn-pattern:
The highlighted area will never be
outside of the matched text.
I'd count myself surprised if you got this to work, but then again, Vim is always full of surprises.
could also try
:set cursorline
:set cursorcolumn
change colors like this:
:hi cursorline
:hi cursorcolumn
using the usual term=, ctermfg=, ctermbg= etc
see this answer
VIM Highlight the whole current line

Resources