Vim highlight every Nth line numbering? - vim

My line numberings in Vim are blue (background) and white (foreground) but this is not very clearly especially for large files.
I would like to have every fifth line numbers background in darkblue and every tenth line numbers foreground in red, so that one can distinguish between 5 and 10 lines of code easily without counting or having to focus at the line numbering.
How can I make this happen? Unfortunately I did not find any plugin doing this..

You cannot make different hl(highlighting) on the line number column. You can somehow highlight the text in the line however. But I guess this is not what you are looking for, you just want to see some flag/highlighting on the line number column. Then the sign might be the closest to your requirement.
source this codes: (I just randomly picked color, you can adjust them as you like)
hi FifGroup term=bold ctermfg=red
hi TenGroup term=bold ctermbg=darkgreen
sign define fifth texthl=FifGroup text=->
sign define tenth texthl=TenGroup text=>>
fun! PlaceLineSign()
for i in range(1+line('$'))
if i =~ '5$'
execute 'sign place '.i.' line='.i.' name=fifth buffer='.bufnr('%')
endif
if i =~ '0$' && i>0
execute 'sign place '.i.' line='.i.' name=tenth buffer='.bufnr('%')
endif
endfor
endf
fun! RemoveLineSign()
sign unplace *
endf
nnoremap <F6> <c-u>:call PlaceLineSign()<cr>
nnoremap <F7> <c-u>:call RemoveLineSign()<cr>
then you can press <f6> to display those flag, and <F7> to hide them.
Note, there is one problem with "sign", if you displayed the signs, and change the line numbers, i.e. removed/added new lines, the "sign"s won't change correspondingly. But hide and display again should go.
It looks like:

You can use my DynamicSigns plugin. It defines a SignExpression command, that works similar to the foldexpression. In your case, you could simply do:
:SignExpression v:lnum%10==0?'Line1':v:lnum%5==0?'Line2':''
Advantage of using my plugin is, it takes care of adjusting the line numbers automatically, when you add or delete lines. Note: depending on the size of your file, this might slow down Vim. But this is a problem, many sign plugins have in common, since there is no VimL API for accessing signs.

I'm also thinking about the same thing but unfortunately there's no easy way to do it (unless using sign support as other answers suggested, at the cost of slowing down).
The best close thing native to vim is LineNrAbove/Below. However, it's defined in vim source itself: https://github.com/vim/vim/blob/f3fa18468c0adc4fa645f7c394d7a6d14d3d4352/src/drawline.c#L1156-L1167; It should be easy to extend to highlight groups like every k-th relative line but it I don't think in the foreseeable future that it would be included in vim. I believe this is the only usable option because evaluating is done in the vim core which is fast than doing so at vim script.

Related

Cycling and/or highlighting through a vim selection

The feature I am thinking of is kind of inspired by a feature that I really like about sublime text.
In sublime text, if you select a sequence of characters, it automatically puts a little box around it (to distinguish it from the word that you just highlighted). For me, this is very helpful because I can see and find specific things of the code much faster.
It would be awesome to have something similar to my vim environment. It does not have to be exactly the same as the one in sublime though, but it would be awesome if it were as similar as possible plus the additional feature to easy cycling through similar words.
Currently, what I am doing is highlight the work I want and then manually typing it to the search command /. It would be much better if I can just highlight it in visual mode and then automatically highlight similar words on the current screen with a different colour from the highlighting in visual mode and then have a quick key short cut to cycling through them, if I wished to do that.
I am not sure if a there exists a plugin or something that already does that, but that would cool! Ideally, I would want to to know as many details of the commands/changes to the vimrc file, so that I have the most control over this feature and be able to customize it as I wish.
You can get the highlighting you are looking for by enabling the hlsearch option:
:set hlsearch
It will highlight every occurrence of the last search pattern and thus work after all the following commands (and their relatives):
/foo<CR>
?bar<CR>
:s/fizz/buzz/g
*
#
You can use n to jump to the next occurrence in the direction of your search and N to do the same in the opposite direction.
To highlight every occurrence of the current word "without" moving the cursor, you can simply do:
*N
or:
*``
to jump to the next occurrence and jump back immediately.
Doing the same for visually selected text is a bit trickier but still possible…
either via a lightweight plugin like visualstar or The Search Party,
or with a tiny bit of crude vimscript in your ~/.vimrc:
" this function cleans up the search pattern
function! GetVisualSelection()
let old_reg = #v
normal! gv"vy
let raw_search = #v
let #v = old_reg
return substitute(escape(raw_search, '\/.*$^~[]'), "\n", '\\n', "g")
endfunction
" map * and # in visual mode so that they do the same as *N and #N in normal mode
xnoremap * :<C-u>/<C-r>=GetVisualSelection()<CR><CR>N
xnoremap # :<C-u>?<C-r>=GetVisualSelection()<CR><CR>N
My SearchHighlighting plugin changes the * command so that it just toggles the highlighting for the current word, without the movement to the next match (for which you can press n, or pass a count). This also works in visual mode, using the selection. I find this very handy for highlighting all matches.
There's also a mode that automatically highlights the current word / selection, like what many IDEs offer.
Other plugins
If you want more permanent highlighting, separate from searching, the Mark plugin offers that.
To get an orientation about the number of matches (without highlighting them), I have the SearchPosition plugin.

How to eliminate the text "[n] lines folded"

I would like a folded line to display only "-" characters: no text at all.
I have tried defining foldtext per examples here and in help:. I was able to eliminate the first line contents, which were extremely annoying and completely illogical to my thinking, but it still displays
---"3 lines folded"-------
for example.
(For me it is visually distracting and irrelevant...partly defeats the purpose of folding for me, which is to HIDE the collapsed section, not to HIGHLIGHT it, which is what this redundant message does.)
You do not say what you tried that did not work, but with
:set foldtext='---'
I see a line of dashes (full width) in place of the fold. If you want even less distraction than that, try
:set fillchars+=fold:\ foldtext='\ '
(There are two spaces after the first backslash.)
You can use following settings in your .vimrc:
set foldtext=EmptyFoldText()
function! EmptyFoldText()
return '-'
endfunction
Works fine on my vim.

Not highlighting search in vim if it is a part of a command

In my .vimrc I've enabled highlighting the searched text (which I find to be a handy feature and wouldn't want to disable it).
set hlsearch
And, following answers to this question, I've made a mapping to be able to clear the highlight:
nmap <silent> ,/ :nohlsearch<CR>
The problem comes with commands which include search. For example, delete to next character 'x':
d/x
This will automatically highlight all the instances of 'x'. To remove this highlight I have to punch ,/ to clear it, which is quite annoying.
The question. Is it possible to enforce :nohl if the search is a part of a preceding command? Maybe, it is possible at least for a selected list of commands (say, d, y and c) before / character is hit?
d/x does not work for me as you describe. (I'm on vim 7.3 here and it can't make sense of the / following d in normal mode, so disregards the d and starts a regular / search.)
If you want to delete to the next x, then dfx or dtx are what I would use (depending on whether you want to also delete the x itself or not).
No highlighting involved.
Hope that helps.
[Following some clarification in the comments.]
I'm thinking that it should be possible to write a custom function to do what you want, and then assign a custom key sequence to call that function.
I played around a little, but am not very well versed in vim functions and couldn't make it work.
Here's what I tried:
function! g:DeleteToSearchAndNohls(term)
:normal d/a:term
:nohlsearch
endfunc
If 'x' is on the same line than the cursor, you can use dtx (meaning delete to x).

How do I increase the spacing of the line number margin in vim?

I have a problem with my colorscheme in vim:
(source: tinygrab.com)
The line numbers are too close to the code. How I can increase the width of the line numbers' right margin?
This bothered me, too; I hate having text crammed up against a block of solid color. It makes me claustrophobic.
Here's what I did as a workaround (in MacVim, but I expect the same technique will work elsewhere):
In your preferred colorscheme, set the background color of the line-number column to be the same as the background color for normal text, and the line-number foreground color to something low-contrast and unobtrusive (so it doesn't look like it's part of your normal text). I use white-on-black for normal text, and dark-grey-on-black for the line numbers:
hi LineNr guifg=#505050 guibg=Black
hi Normal guifg=White guibg=Black
Admittedly, this doesn't fix the problem so much as hide it, but it's worked well for me.
https://github.com/vim/vim/blob/master/src/screen.c#L2220
Looking at the code, it turns to be impossible (without patching the vim, obviously): vim always formats the line numbers to take up the whole available width, leaving just a single space afterwards (the "%*ld " format specifier in the code makes sure of that).
You can add the following line in your "~/.vimrc":
set nuw=6
Where "6" specfies the width of the column in terms of number of characters. Replace with a smaller or large number as needed. Only works with more recent (>=7?) versions of Vim, I think.
%left 5
in the vim terminal (% whole document, left distance code to linenrs, 0-infinity distance in monospace (= number of columns))
Here is solution 2 (ref to how to change the left margin in gvim)
Solution 1 (like %left 5 )just adds 'spaces' into the code area. I guess it does the same done by the threadstarter ...but it does not the answer the original question.
Solution 2: foldcolumn does the trick (foldcolumn is the column left from the linenumbers)
In exec mode :set foldcolumn=12
If you want to change the color too (like in the ref)
hi FoldColumn guibg=#003f3f (in the ref its FoldColumns, thats wrong).
Add the following line to you init.vim file.
autocmd VimEnter * :%left 5

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