VIM Highlight the whole current line - vim

What is the best way to highlight the whole current line in VIM? In some IDEs I am able to set some sort of 20% opacity of the line I am editing, this is great to find where I am rather quickly.

To highlight the current line use
:set cursorline
To highlight the current column use
:set cursorcolumn
The visual appearance of this can be modified. Have a look around :help hl-CursorLine or hl-CursorColumn.

See the Vim wiki on this subject for the complete story.
I especially found the following bit handy (when working with more than one window).
autocmd WinEnter * setlocal cursorline
autocmd WinLeave * setlocal nocursorline
Only highlight the current line in the active window.

Shift + V puts you in visual mode, highlighting the entire line.

Related

Highlighting the 70th character of a current line in Vim

I like to keep a strict 70 character margin whenever possible. To help with this, I want to configure vim so that the 70th character of the current line is highlighted. I understand that
set cursorline
can be used to highlight the current line. I, however, would like just the
very end of the line (the 70th character) to be highlighted. How would I go about accomplishing this?
Edit: cursorcolumn isn't what I'm looking for. I just want a single character (the 70th one on the current line).
Edit 2: perhaps a picture will help.
You can use colorcolumn to set a "right margin" bar.
This did not exist before Vim 7.3, so it is wisest to only enable it if the feature is available.
if exists('&colorcolumn')
set colorcolumn=70
endif
I prefer that this only be shown in insert mode, so I use this:
if exists('&colorcolumn')
autocmd InsertEnter * set colorcolumn=80
autocmd InsertLeave * set colorcolumn=""
endif
That will set the option when you switch to insert mode, and turn it off when you leave insert mode.
:call matchadd('Todo', '\%70c')
and if you don't want to count one tab as a single character, but you want to take into account all the spaces it takes:
:call matchadd('Todo', '\%70v')
You can use any other highlight group (for example to change color) listed by :hi instead of Todo.
:autocmd CursorMoved * exe 'match IncSearch /\%70v\%' . line(".") . 'l./'
The highlight color will be determined by your color scheme.
You can change IncSearch to any of the highlight groups, which can be found by typing:
:hi
If you are using VIM 7.3 you can set the color of a certain column with:
set colorcolumn=70

Vim, split windows breaks length highlight

I set this up in my .vimrc to highlight a line over 80 characters.
highlight OverLength ctermbg=darkred ctermfg=darkred guibg=darkred
match OverLength /\%>80v.\+/
Now then, if I open (one buffer) a file with lines that are too long, they'll highlight. Fine, but if I open a different file and then use NERDTree to open a vsplit of the original file, the long line will no longer highlight. What am I doing wrong?
Edit: I'm using gvim on GNU/Linux.
Edit: After you open the original file in a split long lines will not highlight FOR ANY FILE until you restart vim and open the file in a single buffer.
The :match command only applies to the current window. So any :split won't inherit the highlighting. This is documented under :help :match:
Define a pattern to highlight in the current window.
You can automatically install the matching for any window via :autocmd:
autocmd VimEnter,WinEnter * match OverLength /\%>80v.\+/
But I'd recommend to switch to the 'colorcolumn' setting, which is available in Vim 7.3+; this only highlights a single column (and also in shorter lines), but you can simply :set colorcolumn=81 in your ~/.vimrc and it applies to all windows, and you get to keep the :match highlighting for other uses.

How do I change the background color of current buffer or pane in vim?

Imagine I'm coding, and I have different split panes open. What settings should I pass into vimrc to change the background color as I switch from one buffer/pane to another?
I have tried:
autocmd BufEnter * highlight Normal ctermbg=black
autocmd BufLeave * highlight Normal ctermbg=white
I would like to add that I am sure that I've got 256 colors enabled
Actually, there is a way to get this effect. See the answer by #blueyed to this related question: vim - dim inactive split panes. He provides the script below and when placed in my .vimrc it does dim the background of inactive windows. In effect, it makes their background the same colour specified for the colorcolumn (the vertical line indicating your desired text width).
" Dim inactive windows using 'colorcolumn' setting
" This tends to slow down redrawing, but is very useful.
" Based on https://groups.google.com/d/msg/vim_use/IJU-Vk-QLJE/xz4hjPjCRBUJ
" XXX: this will only work with lines containing text (i.e. not '~')
" from
if exists('+colorcolumn')
function! s:DimInactiveWindows()
for i in range(1, tabpagewinnr(tabpagenr(), '$'))
let l:range = ""
if i != winnr()
if &wrap
" HACK: when wrapping lines is enabled, we use the maximum number
" of columns getting highlighted. This might get calculated by
" looking for the longest visible line and using a multiple of
" winwidth().
let l:width=256 " max
else
let l:width=winwidth(i)
endif
let l:range = join(range(1, l:width), ',')
endif
call setwinvar(i, '&colorcolumn', l:range)
endfor
endfunction
augroup DimInactiveWindows
au!
au WinEnter * call s:DimInactiveWindows()
au WinEnter * set cursorline
au WinLeave * set nocursorline
augroup END
endif
You can't. The :highlight groups are global; i.e. when you have multiple window :splits, all window backgrounds will be colored by the same Normal highlight group.
The only differentiation between active and non-active windows is the (blinking) cursor and the differently highlighted status line (i.e. StatusLine vs. StatusLineNC). (You can add other differences, e.g. by only turning on 'cursorline' in the current buffer (see my CursorLineCurrentWindow plugin.))
One of the design goals of Vim is to work equally well in a primitive, low-color console as in the GUI GVIM. When you have only 16 colors available, a distinction by background color is likely to clash with the syntax highlighting. I guess that is the reason why Vim hasn't and will not have this functionality.
Basically what Kent said above will work -- surprisingly well at that. The only limitation is that you can only dim the foreground color. Copy this into vimrc, and evoke :call ToggleDimInactiveWin().
let opt_DimInactiveWin=0
hi Inactive ctermfg=235
fun! ToggleDimInactiveWin()
if g:opt_DimInactiveWin
autocmd! DimWindows
windo syntax clear Inactive
else
windo syntax region Inactive start='^' end='$'
syntax clear Inactive
augroup DimWindows
autocmd BufEnter * syntax clear Inactive
autocmd BufLeave * syntax region Inactive start='^' end='$'
augroup end
en
let g:opt_DimInactiveWin=!g:opt_DimInactiveWin
endfun
A few things I had to look up in writing this:
(1) windo conveniently executes a command across all windows
(2) augroup defines autocommand groups, which can be cleared with autocmd! group
For Neovim users, there is the winhighlight option. The help file provides the following example:
Example: show a different color for non-current windows:
set winhighlight=Normal:MyNormal,NormalNC:MyNormalNC
Personally, I use my statusline to let me know this. I use the WinEnter and WinLeave autocmds to switch to an inactive status line (grayed out) when leaving and an active statusline (bright colors) when entering. The split panes you mention are windows in vim. This also works because :help statusline tells us that its setting is global or local to a window, so you can use :setlocal statusline=... or let &l:statusline=... to only apply to the current window.
Your method won't work because a) BufEnter and BufLeave aren't necessarily the events that you want and b) highlight groups are global, so changing Normal's definition changes it for every window.

vim: Highlight quickfix selected line with color different than 'Search'

What is the highlight group for the currently selected line in the quickfix window?
The selected line in the quickfix window uses Search for highlighting. I'd like to continue using yellow for Search highlighting, but use blue for quickfix selected line.
Ingo Karkat's answer is right. It's indeed hard-coded in vim code.
I have created a patch - QuickFixCurrentLine.patch for vim8.
Patch is long enough to be posted here. Plus, it has mix of tabs and spaces. So, providing a link-only-answer.
EDIT:
The patch has got upstreamed in the latest vim code.
The name of the highlight has been changed to quickfixline instead of quickfixcurrentline.
The currently selected quickfix item is hard-coded to Search. You'd have to change the Vim source code and recompile to change this.
I see only limited ways to work around this with Vimscript. You could try to override the highlighting for the current line via :match / matchadd() (it has higher priority), but it would only cover the length of the text, not the entire line the original highlighting. Also, I think the currently selected item cannot be easily queried from Vim, so you'd have to hook into the quickfix-local <CR> mapping to update it, and stop using :cnext etc. to move to different errors.
:highlight BlueLine guibg=Blue
:autocmd BufReadPost quickfix match BlueLine /\%1l/
:autocmd BufReadPost quickfix nnoremap <buffer> <CR> :execute 'match BlueLine /\%' . line('.') . 'l/'<CR><CR>

Vim: Different line-number color in the different modes

I've just started using Vim and I find myself being confused on which mode I'm currently in. I was wondering if it's possible to change the line number color (which I have displayed by default) in the different modes (command, visual, and insert) so that I can easily identify it.
Vim has an option called showmode that will put a message on the last line if you are not in normal mode. See :help 'showmode' for more info.
If you really want to change the background of the number column while in insert mode, you could do something like the following:
autocmd InsertEnter * highlight LineNr ctermbg=red guibg=red
autocmd InsertLeave * highlight LineNr ctermbg=black guibg=black
Note that this method will break if you are bad and use C-c to exit insert mode instead of <Esc> or <C-[.
Relevant :help tags: autocmd, autocmd-events, highlight, highlight-groups

Resources