Setting a sign to highlight only text instead of whole line in Vim - vim

When defining a :sign you can use the linehl argument to assign a highlight group for the whole line the sign is placed in.
This highlights the whole line until the end, but how I do to highlight only the text of that line?
Edit: The idea is to use Syntastic to show the errors and warnings, but I can't redefine the SyntasticStyleError sign to do what I want. It highlights all the line instead of only the text of that line.

It is not possible as far as I know. Look at the :h sign-define:
:sign define {name} {argument}...
Define a new sign or set attributes for an existing sign.
The {name} can either be a number (all digits) or a name
starting with a non-digit. Leading digits are ignored, thus
"0012", "012" and "12" are considered the same name.
About 120 different signs can be defined.
Accepted arguments:
icon={bitmap}
Define the file name where the bitmap can be found. Should be
a full path. The bitmap should fit in the place of two
characters. This is not checked. If the bitmap is too big it
will cause redraw problems. Only GTK 2 can scale the bitmap
to fit the space available.
toolkit supports ~
GTK 1 pixmap (.xpm)
GTK 2 many
Motif pixmap (.xpm)
Win32 .bmp, .ico, .cur
pixmap (.xpm) |+xpm_w32|
linehl={group}
Highlighting group used for the whole line the sign is placed
in. Most useful is defining a background color.
text={text} *E239*
Define the text that is displayed when there is no icon or the
GUI is not being used. Only printable characters are allowed
and they must occupy one or two display cells.
texthl={group}
Highlighting group used for the text item.
It does not offer you a straight-forward way to distinguish between the text background colour and the background colour of the line - in fact, you can only set the linehl parameter.
Maybe there is a hacky way to do what you want. I've stumbled upon this link you might find useful: https://sunaku.github.io/vim-256color-bce.html
Another interesting idea is explained on vim.wikia.com (link) in the Highlighting that stays after cursor moves section. It suggests using the following command:
:nnoremap <silent> <Leader>l ml:execute 'match Search /\%'.line('.').'l/'<CR>
This way you might mix it with the information you get from :sign place and replace signs with your custom highlighting method I posted above. It requires some scripting though.

Related

Syntax Highlighting with Fine Granularity

Is there a way to assign an individual character, as identified by a height and depth index, to a highlight group? Every match feature I have come across uses a regex pattern as input.
The reason I ask is because I am making a syntax coloring plugin that will make text an increasingly lighter shade of gray with increasing parenthesis depth. If vim has no such feature and another algorithm makes character-by-character highlighting unnecessary, please point me to it!
Vim has a whole set of special regular expression atoms that can specify buffer positions.
For lines, \%23l matches only in line 23. You can also use \%>23l for all lines starting from 23, and concatenate two of those with < and > to specify ranges.
For columns, there are the corresponding \%23c and \%23v. The former uses byte indices (what Vim somewhat confusingly calls "columns"), as returned by functions like col() and getpos(), the latter screen widths (from virtcol()).
By combining those atoms, you can select arbitrary blocks of text, and highlight them, e.g. with :call matchadd(...). See :help /\%l for details on the atoms.
For your plugin implementation, you may be able to get some ideas from the vim js context coloring plugin, which highlights JavaScript code according to its scope.

Multiple matches for syntax highlighting in VIM

I'm writing a syntax file to match a log format (basically column based; think syslog for a similar example), and I'm trying to set up a type of inheritance for columns.
I have two main goals with this.
First, I want to say that column 3 is the "component" field (let's say it's marked by a header; it could also be at a fixed position) and set the background to, say, Grey. I then want to say that component "foo" gets a foreground color of Red, and component "bar" gets a foreground color of Green, but they should inherit the background color of the "component" column. In this case, the field should really have two syntax matches; this also makes it easy to conceal the entire column (a la Toggling the concealed attribute for a syntax highlight in VIM)
Second, there's a field for levels; I want to set the background of the entire line for a critical level message to Red, but the foreground should be continue to be set via the normal highlighting (component, source, etc; I left off most of the other requirements).
From what I can see in the vim documentation, this doesn't seem possible. Am I missing something? Alternatively, can anyone suggest a good workaround?
Thanks
You can't (yet). For each character, Vim only uses one particular highlight group, determined by the last, "most inner" non-transparent syntax group match.
To work around this, you need to define a combined highlight group and corresponding :syntax commands. Some syntaxes (e.g. $VIMRUNTIME/syntax/html.vim, and various Wiki formats) use that for combining bold, italic and underline, but it gets tedious and repetitive after that.
There's a patch proposed that would add a combine modifier for :syntax commands; it's in Bram's Todo list.

In Vim, how to keep characters concealed even when cursor enters that line

I may have a unique situation here. I want gVim (gui version, in Linux) to keep concealed characters concealed no matter what, even when the cursor is on that line or that character gets selected. (It should be as close to if the characters never existed as possible.) Currently the concealed characters show themselves when the cursor enters that line, which causes text to jump around when scrolling and when selecting text.
We are using gView (read-only gVim) to view logs, so as to take advantage of its robust syntax highlighting. Problem is, these logs contain lots of escape characters and TTY color codes, that make reading difficult. (^[33mSomeText^[0m)
I'm using this line to hide them:
syntax match Ignore /\%o33\[[0-9]\{0,5}m/ conceal
Since the files are viewed by non-vim-experts, it looks glitchy and broken when the text un-conceals itself. (And also looks glitchy and broken if the color codes are present, and also looks glitchy and broken if the color codes are blacked-out to become invisible, but still show when selected and appear after copy/paste.)
This should be fine because these files are opened read-only in gview, with an extra set nomodifiable making it even more difficult to save the file. While it's possible to edit and attempt to save the logs, doing so is considered both an invalid thing to do, and a harmless thing to do, and requires enough Vim skills that if someone manages to edit a file they know what they're doing. The problem with being able to edit a line with concealed text does not apply.
If 'conceal' can't be configured to keep hidden text hidden no matter what, an acceptable alternative would be to replace the TTY color codes with whitespace when the file gets opened. But, this has to be done in read-only mode, and we can't have gview throwing up a save dialog on closing the window because the file has been modified by its .vimrc.
Note: I am in full control of the .vim script file sourced when these are read, but cannot control the existence of the TTY color codes or the code that opens the log files in gview. (i.e. I can't pass it through sed or anything like that.) The ideal solution is anything that can transparently nuke the color codes from within a .vimrc, but I'll hear any suggestions. The 'conceal' feature is just my most promising lead.
So, any ideas how to permanently get rid of these on file view without dialogs popping up on close?
:help conceal
When the "conceal" argument is given, the item is marked as concealable.
Whether or not it is actually concealed depends on the value of the
'conceallevel' option. The 'concealcursor' option is used to decide whether
concealable items in the current line are displayed unconcealed to be able to
edit the line.
:help concealcursor
Sets the modes in which text in the cursor line can also be concealed.
When the current mode is listed then concealing happens just like in
other lines.
n Normal mode
v Visual mode
i Insert mode
c Command line editing, for 'incsearch'
'v' applies to all lines in the Visual area, not only the cursor.
A useful value is "nc". This is used in help files. So long as you
are moving around text is concealed, but when starting to insert text
or selecting a Visual area the concealed text is displayed, so that
you can see what you are doing.
Keep in mind that the cursor position is not always where it's
displayed. E.g., when moving vertically it may change column.
Also, :help conceallevel
Determine how text with the "conceal" syntax attribute |:syn-conceal|
is shown:
Value Effect ~
0 Text is shown normally
1 Each block of concealed text is replaced with one
character. If the syntax item does not have a custom
replacement character defined (see |:syn-cchar|) the
character defined in 'listchars' is used (default is a
space).
It is highlighted with the "Conceal" highlight group.
2 Concealed text is completely hidden unless it has a
custom replacement character defined (see
|:syn-cchar|).
3 Concealed text is completely hidden.
Only one command is needed: set concealcursor=n
I might have a better idea—you can pass it through sed (using %!sed) or really do a bunch of other :substitute commands—whatever edits you need to get rid of the color codes.
When you’re done, make sure to set nomodified—this forces vim to think there haven’t been any changes!

VIM: How to change the Showbreak Highlight color without using the NonText Color-element

I noted that the 'showbreak' symbol is highlighted with the highlight "NonText" color-element. NonText is also used for the EOL Characters.
I would like to keep the highlight-color for the EOL characters but want to change it for the showbreak symbol is that possible?
Another problem is that my showbreak symbol is not displayed.
I would like to use this symbol "↳" and put it in the linenumbers column (using set cpoptions+=n). I can't find out how to display the symbol and how to put a space after the showbreak symbol (between the text and the symbol).
Can anyone help me?
I don't think you're going to get highlighting to be different than the EOL character, at least I am not aware of a way to do that.
For the second part I can help with. I was able to get "↳ " to show up in my line number column with the following settings:
let &showbreak = '↳ '
set wrap
set cpo=n
Note that there is a space after the ↳. This lines up nice until you have > 9 lines in the file. If you wanted it to line up with the last character of the number column regardless of the number of lines I'm not sure what you're going to have to do.
Edit: I've recently written a proof-of-concept function for someone on IRC that highlights the first character on a line that has been wrapped with a different highlight group. It hasn't been tested much but it seems to work. Not exactly what you're looking for but maybe it's worth a look.
:help hl-NonText makes it pretty clear that you cannot have different colors for the 'showbreak' string and other non-text strings, of which eol is a member (see :help 'listchars'):
NonText
'~' and '#' at the end of the window, characters from 'showbreak' and
other characters that do not really exist in the text (e.g., ">"
displayed when a double-wide character doesn't fit at the end of the
line).
If you're willing to accept this limitation (#elliottcable) hi! link NonText LineNr will match the 'showbreak' string to the line number colors.
If you really wanted to get clever, as a compromise you could create a mapping or command to toggle between ':set list' and ':set nolist' that would also adjust the NonText highlight setting simultaneously.
If you use :set relativenumber (added in vim 7.3), :set showbreak=↳\ \ \ will reliably keep your 'showbreak' neatly lined up since the number width will not change as you navigate through the file. (This in addition to the :set cpo+=n and :set wrap #Randy Morris mentioned in his answer.)
You'll definitely need UTF-8 for the ↳ character, since it does not appear in other encodings. I'd strongly recommend you carefully document your encoding problems, with details about how to reproduce them along with your OS, its version, and the :version output of vim, and post them as separate questions. UTF-8 should be helping you wrangle multiple languages rather than being an impediment.

Full width background color highlighting of codeblock in Vim

I have Vim documents that have codeblock regions that use 'syntax include' regions to have different syntax highlighting from the main document. For example, I have a syntax region named 'pyregion' defined that I use when I input Python code. The pyregion areas of the document are then highlighted using setup in Python's syntax file, while the area outside the region uses the syntax file of the main document.
I want to have the entire background in the Python region be shaded a different color to distinguish it from the non-Python areas of the document. I can get part of the way there by entering a background for the entire python region:
:highlight pyregion guibg=#555555
But the command above changes the background only for areas that have text characters on them, not for entire background of the region, which seems still to be governed like rest of document by the background color defined for the 'Normal' group. This is okay -- it does draw attention to the region, but it has a blotchy look because only the code characters themselves have different background, not the entire band of the region across the screen.
Is there some way to get a uniform band of a different background color across the whole region, not just portion of region where there are characters?
There's related question and answer in following link, but so far as I can tell that answer also changes background only where there is text:
Highlight Code Block Backgrounds with Vim
Thanks for any help.
I don't believe that this is possible without a ridiculous bodge (see below). If you try this:
:match Error /\%3l/
It highlights what Vim considers to be the whole of line 3. The highlighting only extends to the the end of the characters. This makes me suspect that there will be no way to highlight non-existent characters.
Stop reading now.
Purely as an exercise in the ridiculous, you could of course append a massive number of spaces to the end of every line and then remove them when you save. This would be daft, but it's the only thing I can think of.
" Add spaces to the end of every line (you could also do
" this selectively for lines that don't already have lots
" of spaces and you could do it on reading a file or whatever)
:exe '%s/$/' . repeat(' ', 1000) . '/'
" Automatically remove spaces at the end of every line when saving
:autocmd BufWritePre %s/ *$//
" Make 'A' append at the end of the non-space characters on the line
:nmap A / *$<CR>i
" Make '$' go to the end of the non-space characters on the line
:nmap $ /.\? *$<CR>
Disclaimer: don't do this.
Yes, you can do this using the sign command.
http://vimdoc.sourceforge.net/htmldoc/sign.html
sign define 1 linehl=Error
sign place 1 name=er1 line=1 file=file.txt
Will highlight the first line of file.txt all the way to the edge of the terminal.

Resources