Cursor-highlighting in quickfix-window: QuickFixCmdPre overriding QuickfixCmdPost? - vim

I would like my quickfix-window to have some highlighting for the current line of the cursor.
After some research I found that I could configure the general appearance of the current line using set cursorline and highlight CursorLine term=bold cterm=bold guibg=Grey40.
Now, I only want that when I'm in the quickfix-window though. So I started to wrap these 2 lines inside a function and called that function with a autocommand:
au QuickFixCmdPre * call EnableSearchHighlighting()
So far, so good. Since I still have the highlighting activated after I searched at least once, I needed to disable the effect again. And here is where I am stuck...
I wrote another function to just set nocursorline and call this one on the QuickFixCmdPost-Event. But for some reason this broke everything. Now I won't get the highlighting anymore, not even in the quickfix-window.
It feels like the Post-Event overrides the Pre-Event.
I'm not sure what else to try here.
Maybe anyone can help me out or even has another approach to the highlighting in the first place?
Here is the full code as it is in my .vimrc right now:
function EnableSearchHighlighting()
set cursorline
highlight CursorLine term=bold cterm=bold guibg=Grey40
endfunction
function DisableSearchHighlighting()
set nocursorline
endfunction
au QuickFixCmdPre * call EnableSearchHighlighting()
au QuickFixCmdPost * call DisableSearchHighlighting()
Thanks for reading. :)

Your approach has many problems, but the main one (and the reason it doesn't work) is that both QuickFixCmdPre and QuickFixCmdPost are run for each quickfix command before you get to switch to the error window.
Add this to a file ftplugin/qf.vim:
setlocal cursorline
Then add the highlight definition to your vimrc, outside any function or autocmd:
highlight CursorLine term=bold cterm=bold guibg=Grey40

Related

Vim Highlight Setting Local

I want to ask if it is possible to define highlighting local to a buffer/window, as well as with any other vim option. So far I've this small group in my .vimrc, to "highlight" the active window. Therefore only the active window gets the cursorline.
augroup CursorLine
autocmd!
autocmd WinEnter * set cursorline
autocmd WinLeave * set nocursorline
augroup END
Now I want to do something similar with the background. A pure black for inactive windows and a really dark grey for the active one. My approach is this:
augroup BackgroundSwitch
autocmd!
autocmd WinEnter * highlight Normal ctermbg=235 ctermfg=15 guibg=#ff0000 guifg=#FFFFFF cterm=NONE gui=NONE
autocmd WinLeave * highlight Normal ctermbg=16 ctermfg=15 guibg=#ff0000 guifg=#FFFFFF cterm=NONE gui=NONE
augroup END
Unfortunately this always make all window backgrounds like the active one. I guess WinLeave is thrown before WinEnter, so it is only rly shortly visible (not for me). An setting a highlight instead of an options, affects always all windows in this Vim instance.
So is there the possibility, that this highlights are only set locally, so I get the wished behaviour?
Thanks!
Changing the highlight is always global and can't be changed only for
a scope.
The Diminactive provide a hack for this. But this plugin lacks in the following points:
The "background" is only changed in lines, which contains text (new line char included).
Background highlights like seach results in inactive windows are invisble (the whole result) and lead to ugly gaps within the text.
Makes the redraw theoretically slower, which sum up with all other plugins u might have run in background to process your buffers (not recognized on my computer).

Number pane not being highlighted in vim dracula theme

I am using dracula theme for vim and am not able to get the number pane, that is, the side panel which contains the line numbers, to be displayed in a sort of translucent manner. The preview image shows that it's possible.
How the terminal should look like
(source: draculatheme.com)
How it actually looks
To fix this issue, I think I need to configure some attributes accordingly, but being a beginner, I don't know which ones, therefore any help and guidance would be appreciated.
As a reference, these are my dotvim files.
The background of the line numbers column is set in the colorscheme to NONE for color terminals and #282a36 for GUIs:
hi LineNr ctermfg=60 ctermbg=NONE cterm=NONE guifg=#6272a4 guibg=#282a36 gui=NONE
From there you have three options:
Enable the 'termguicolors' option so that Vim uses the gui* attributes instead of the cterm* attributes.
This is how the screenshot was taken but it will only work in select terminal emulators.
See :help 'termguicolors'.
Edit the colorscheme directly:
hi LineNr ctermfg=60 ctermbg=242 cterm=NONE guifg=#6272a4 guibg=#282a36 gui=NONE
I've chosen 242 arbitrarily but you can choose whatever color you want in this chart
Override your colorscheme in your vimrc:
function! MyHighlights() abort
hi LineNr ctermfg=60 ctermbg=242 cterm=NONE guifg=#6272a4 guibg=#282a36 gui=NONE
endfunction
augroup MyColors
autocmd!
autocmd ColorScheme * call MyHighlights()
augroup END
colorscheme dracula

How to underline (rather than highlight) the current line in vim?

I believe it's possible to get an underline under the current line, rather than a highlight.
This adds the highlight in my .vimrc:
set cursorline
This is what I've tried adding to get an underline:
:highlight CursorLine gui=underline cterm=underline
But it appears to make no difference.
I'm using vim 7.4.629 on Centos 6.7 through putty, if that helps.
try :hi clear CursorLine to clear the current cusorline hl, then :hi CursorLine gui=underline cterm=underline
The color of underline is same as your ctermfg or guifg. You can either live with your "colorful" underline, or add cterm/guifg to make the underlined text and the underline same color.
:set cursorline
was the best solution for me, you can combine it with removing the cursorLine as the answer above mentions
For me it works best to highlight the cursor line only in Insert mode. To do so, I use the action snippet to activate cursor underlining (full line) whenever Insert mode is activated and later deactivating it upon leaving the mode.
au InsertEnter * set cul
au InsertLeave * set nocul

Why isn't this vimrc command working for me? I'm trying to highlight long lines that exceed 80 characters

So I found this solution on StackOverflow here: Vim 80 column layout concerns
If I type a long line in my file, I would like the characters that exceed a limit of 80 characters to be highlighted. A lot of people seem to think that this solution works fine, but I have it in my vimrc file and it behaves as though nothing has changed at all. My long lines do not get highlighted.
highlight OverLength ctermbg=red ctermfg=white guibg=#592929
match OverLength /\%81v.\+/
For reference, here is my entire .vimrc, which isn't that long:
" You'll need to add the following to your ~/.vimrc so that pathogen will be loaded
" properly. Filetype detection must be off when you run the commands so its best to
" You'll need to add the following to your ~/.vimrc so that pathogen will be loaded
" execute them first:
"filetype off
call pathogen#runtime_append_all_bundles()
call pathogen#helptags()
"filetype on
syntax on
let mapleader = ","
let g:CommandTMaxHeight=25
imap ii <Esc>
map <S-Enter> O<Esc>
map <CR> o<Esc>
set guioptions-=T
set guioptions-=r
set hlsearch
highlight OverLength ctermbg=red ctermfg=white guibg=#592929
match OverLength /\%79v.\+/
set nocompatible
set ruler
set number
set shellcmdflag=-ic
set list
set expandtab
set tabstop=4
set softtabstop=4
nmap <C-k> ddkP
nmap <C-j> ddp
vmap <C-k> xkP`[V`]
vmap <C-j> xp`[V`]
au! BufWritePost vimrc source %
colorscheme vividchalk
From: https://stackoverflow.com/a/10993757/1701170
augroup vimrc_autocmds
autocmd BufEnter * highlight OverLength ctermbg=darkgrey guibg=#111111
autocmd BufEnter * match OverLength /\%75v.*/
augroup END
This works for me whereas the solution I had before does not. I'm not exactly sure why, but another commenter on the page offers a hint when he remarks, regarding the solution without opening and closing augroup lines, "This only works for the first file you open in any given buffer".
Now if someone could explain why the additional opening and closing lines solve that first-file-in-any-given-buffer problem, and why that problem exists in the first place, then I would feel enlightened.
The following seems to work for me:
highlight OverLength ctermbg=darkred ctermfg=white guibg=#FFD9D9
match OverLength /\%79v.*/
it's bascially the same thing that you have with just \+ changed to * and a different color. For some reason though changing your line didn't work for me. Perhaps there's something wrong with one of the characters?
Anyway - copied from this post: https://stackoverflow.com/a/395326/680238
I think it's the one I used when working on my vimrc, but as a side note I might add I've dropped the idea of highlighting those overlength lines and settled for having vertical line on 81st column. You may want to try this and see which one you like better:
" Highlight first oversize line
set colorcolumn=81
In my opinion it's much cleaner to use textwidth and colorcolumn. Look them up in the help for specifics on their function.
set textwidth=80
set colorcolumn=+1
This only highlights the point at which 80 columns is exceeded so may not meet your goals but in my opinion makes things easier to read if you're often loading up files from sources that do not obey your restrictions.
The highlighting is handled by the ColorColumn highlight group.
You can also just set colorcolumn to 81 and don't need to set textwidth but I use textwidth for a few other things also so have it set anyway, and this allows me to change textwidth and get the coloring at the max width regardless.
I know this is a super old post, but I recently found my OverLength highlighting stopped working (Vim 8.1). my highlight code was originally:
augroup vimrc_autocmds
autocmd BufEnter * highlight OverLength ctermbg=darkred ctermfg=white guibg=#FFD9D9
autocmd filetype BufEnter * match OverLength /\%>79v.\+/
augroup END
I removed filetype from the 3rd line and it started working again. I don't know which version changed this behaviour, but having filetype in there definitely worked for me previously.
This is pretty much the answer from Jonomono, just clarifying in case anyone else runs into a similar issue :)

Highlighting the current line number in vim

Is there a way to highlight only the current line number (in the left hand coloumn) in vim, without highlighting the background of the current line? Ideally, I would Like to make the current line number bold.
There are two groups that determine highlighting of line displayed when &cursorline option is active: CursorLine and CursorLineNR. First is used to highlight the whole line, second for the line number. So to achieve what you want you must
Clear the highlighting of CursorLine: just hi clear CursorLine after any :colorscheme and set background= call.
hi clear CursorLine
augroup CLClear
autocmd! ColorScheme * hi clear CursorLine
augroup END
Set the highlighting of CursorLineNR if it is not set in your colorscheme:
hi CursorLineNR cterm=bold
augroup CLNRSet
autocmd! ColorScheme * hi CursorLineNR cterm=bold
augroup END
(better to check whether it is already set in the colorscheme, maybe it will look better in that case).
You can join both autocommands in one of course.
CursorLineNR has been added relatively recently around version 7.3.488.
You want to look at
:se cursorline
and perhaps even/also
:se cursorcolumn
This is what worked for me:
highlight CursorLine cterm=NONE ctermbg=NONE ctermfg=NONE guibg=NONE guifg=NONE
set cursorline
I'm just using this in my .vimrc after having set the color scheme. Of course you could also set a specific background color but setting all of them to NONE just highlights the line number (i.e. makes it brighter).
I guess you could just use :hi CursorLine cterm=NONE but I just wanted to make sure I'm making everything transparent (gvim included).
With CursorLineNR I was then able to set the foreground and background colors of the highlighted number.
I'm only writing this because for me it worked without any autocommands and it may be what most people require.
You can set cursorline to highlight only the numbers
'cursorlineopt' 'culopt' string (default: "number,line")
local to window
{not available when compiled without the +syntax
feature}
Comma separated list of settings for how 'cursorline' is displayed.
Valid values:
"line" Highlight the text line of the cursor with
CursorLine hl-CursorLine.
"screenline" Highlight only the screen line of the cursor with
CursorLine hl-CursorLine.
"number" Highlight the line number of the cursor with
CursorLineNr hl-CursorLineNr.
And to override your colorscheme use autocmd
So, the following works:
set cursorline
set cursorlineopt=number
autocmd ColorScheme * highlight CursorLineNr cterm=bold term=bold gui=bold
This worked for me to highlight the line number and not the rest of the line:
highlight CursorLineNr cterm=NONE ctermbg=15 ctermfg=8 gui=NONE guibg=#ffffff guifg=#d70000
The help for highlight-groups does not mention an exclusive "number of current line" syntax group, so the official answer might be no.
You may want to take a look in the cursorline option which highlights the entire line, if it helps.

Resources