vim - show whitespace for selection only - vim

Is it possible to show whitespace characters (space, tabs, newlines) only in visual mode, specifically in the selected text?
Or is there already a nice plugin that does the job?

Depending on how the highlighting is set up for visual mode, it is possible to set the foreground color of your white space characters to the background color of the window.
This is a sample Normal highlight group (taken from my own colorscheme) which defines the background color of the whole window:
hi Normal ctermbg=235 ctermfg=250 guibg=#262626 guifg=#bcbcbc cterm=NONE gui=NONE
The values we want are 235 for color terminals and #262626 for the GUI.
This is a sample Visual highlight group. It's only one foreground color on one background color:
hi Visual ctermbg=110 ctermfg=235 guibg=#8fafd7 guifg=#262626 cterm=NONE gui=NONE
The colors themselves don't really matter.
This is how we could set up the SpecialKey highlight group, using the values above:
hi SpecialKey ctermbg=NONE ctermfg=235 guibg=NONE guifg=#262626 cterm=NONE gui=NONE
And this is how it should look:
You'll need to edit your colorscheme for this to work across the board or to add this to your vimrc:
augroup colors
autocmd!
autocmd ColorScheme * hi SpecialKey ctermfg=235 guifg=#262626
autocmd ColorScheme * hi NonText ctermfg=235 guifg=#262626
augroup END
Of course, the colors above are are just an example; YMMV.

The "list" option in Vim will show whitespace characters:
List mode: Show tabs as CTRL-I is displayed, display $ after end of
line. Useful to see the difference between tabs and spaces and for
trailing blanks.
See:
:help list
That being said...
This isn't exactly what you want, but Tim Pope's unimpaired plugin provides a very easy way to toggle off the "list" option (among many other options).
When the
So, if you need to take a quick glance at those whitespace characters you can toggle them on/off with:
col
I consider unimpaired an essential, very light-weight plugin (basically just a set of mappings)

It is not possible, to permantly switch the display of the visual selection to enable the list mode, while the rest of the buffer does not have list set.
A workaround however is, after visually selecting some lines, press : so the command line looks like this: :'<,'> and then just use the :list command (which can be shortened to :l). So using :'<,'>l will echo the selected range of lines as if the list setting has been turned on. This however works only linewise, so it is not possible to only print the block selected region.
See the help at :h :l.

Related

Set visibility and/or color of listchars or conceal cchar in vim when cursorline is highlighting a line

I know that I can use set list listchars=... to show NonText and SpecialKey characters in order to highlight different kinds of, well non-text characters. In general this is straight forward and works. But it has a visual side effect which disturbes me - while "hovering" the cursor over a line when the cursorline is set to on listchars are highlighted in a different color. The problem was already mentioned in this thread and there does not seem to be a solution to work around the behaviour.
As a consquence I started to play with the conceal feature, which gives me mixed results. set concealcursor=n||v||i||c lets me control in which modes concealed characters are visible. I want to be able to see concealed characters always so I set up the following:
function! ConcealNonText()
set conceallevel=1
set concealcursor=nvic
syntax match NonText / / conceal cchar=ยท
endfunction
augroup ConcealNonText
autocmd!
autocmd VimEnter * call ConcealNonText()
augroup END
As a result it highlights all spaces which are alway visible no matter which mode I'm currently in. But again there's a disturbing side effect - when I mark lines in visual mode or highlight a line with my cursorline the highlighting area gets interrupted where concealed characters are found. Just see the screenshots:
Visual mode with selected area:
Normal mode with cursorline:
So I thought let's try set concealcursor=ic. This results in uninterrupted highlighted areas, but then the characters are hidden.
So is there a somewhere and somehow a solution around to the thread I mentioned in the beginning of this post or is there any way to make the cursorline and visual select areas uninterrupted when using set concealcursor=nvic?
I believe you have 'cursorline' set. The CursorLine highlight group defines the highlights for the same. Either you set nocursorline, (which can speed line movements) or change the CursorLine highlight groups fg colors.

Vim command line color setting

Is there a way to set different colors for the command line color and the "Normal" text color in vim. When I use
:hi Normal guifg=orange
the command line and the normal text color become orange. I would like the command line at the bottom of the gui to be a different color however.
Nowadays NeoVim has the feature. MsgArea color group. e.g.
:hi MsgArea guifg=#03ff13
No, not that I know of.
Not without messing with Vim's source.
The command line is under Normal highlighting group.
This can be done with autocommands:
hi Normal=white guifg=white
au CmdLineEnter * hi Normal ctermfg=cyan guifg=cyan
au CmdLineLeave * hi Normal ctermfg=white guifg=white
Surprisingly to me, this only affects the CmdLine, not everything else.
I had expected all the normal text to change color immediately when entering the CmdLine. Without the CmdLineLeave, the change to the normal text everywhere happens, but only after getting out of the command line; which explains why it does what we want.
Note that I am using vim from a terminal, so I am using ctermfg instead of guifg; but I anticipate that it will work the same way with a gui version of vim.
You may also want to highlight the ModeMsg.
That can be done via new wincolor option that was introduced in Vim 8.2:
set wincolor=NormalAlt
autocmd WinEnter set wincolor=NormalAlt
hi Normal guifg=#CED1CF guibg=#000000 gui=NONE
hi NormalAlt guifg=#CED1CF guibg=#1B1D21 gui=NONE

Vim split bar styling

How do I style or remove the grey bar that appears on window split?
You'll need to edit your colorscheme. The highlight group is VertSplit.
If you want to get rid of the pipe characters you can set ctermbg and ctermfg to the same value.
Additional information on styling vertical split borders*:
*Taken from ib. answer below.
In order to get rid of | characters, one should rather change vertical separator using:
:set fillchars+=vert:\
(note the significant whitespace after the '\' character)
highlight VertSplit cterm=NONE
I kind of tweak it a little to make it look like there is no splitbar with something like this
hi LineNr guibg=bg
set foldcolumn=2
hi foldcolumn guibg=bg
hi VertSplit guibg=bg guifg=bg
Note that I'm using gvim so I'm using guibg to change the background guifg to change the foreground with the same color of my vim's background, if you're not using a gui and you're on terminal vim you should change the guibg and guifg parameters for ctermbg and ctermfg
If you add a comment with " after the whitespace, you don't have a trailing white space. I don't like these, so my vim config shows them (vim-airline plugin) and I remove them regularly.
:set fillchars+=vert:\ "White space at the end

In Vim can you stop the color change of white space characters with 'set cursorline' on?

In this Vim screenshot you can see that when moving the cursor over a line it changes the normal color of the whitespace characters (shown on the left) from grey to black. Can I stop this and leave them showing grey always, regardless of cursor position?
I've tried setting these in the colour scheme but no luck:
hi SpecialKey guibg=bg guifg=#CCCCCC gui=none
hi NonText guibg=bg guifg=#CCCCCC gui=none
You can use :match to highlight the tabs.
:match NonText '^\s\+'
That seems to override the cursor line. It would be better of course to use matchadd() but it seems to be overriden by the cursor line. There might be a way to make it work
Following lines in .vimrc fixed the problem for me.
au VimEnter * call matchadd('SpecialKey', '^\s\+', -1)
au VimEnter * call matchadd('SpecialKey', '\s\+$', -1)
It overrides other styles application for tabs and trailing spaces inside a cursor line.
Yes you can. From :help listchars (at the end):
The "NonText" highlighting will be used for "eol", "extends" and
"precedes". "SpecialKey" for "nbsp", "tab" and "trail".
With this knowledge you can modify your color scheme accordingly or add a call to highlight in your vimrc.
I believe you have 'cursorline' set. The CursorLine highlight group defines the highlights for the same. Either you set nocursorline, (which can speed line movements) or change the CursorLine highlight groups fg colors.

Load different colorscheme when using vimdiff

How to load a different colorscheme when doing vimdiff.
I want this because the my current colorscheme does not show some diffs properly in vimdiff, For. eg some diff is shown with same fg/bg color. This makes it very hard to understand the diff. So every time i do a vimdiff i have to do :colorscheme some_other_scheme
Can this be done in .vimrc file?
I don't know why vim uses so many colors to highlight with, it doesn't really help you figure out what's going on.
I modified my colorscheme to only use one color to highlight (with another to show where theres a difference within a line) and it made all the difference.
Before
After
I did this by adding the following to the end of my colorscheme file (~/.vim/colors/mycolorscheme.vim).
highlight DiffAdd cterm=bold ctermfg=10 ctermbg=17 gui=none guifg=bg guibg=Red
highlight DiffDelete cterm=bold ctermfg=10 ctermbg=17 gui=none guifg=bg guibg=Red
highlight DiffChange cterm=bold ctermfg=10 ctermbg=17 gui=none guifg=bg guibg=Red
highlight DiffText cterm=bold ctermfg=10 ctermbg=88 gui=none guifg=bg guibg=Red
cterm - sets the style
ctermfg - set the text color
ctermbg - set the highlighting
DiffAdd - line was added
DiffDelete - line was removed
DiffChange - part of the line was changed (highlights the whole line)
DiffText - the exact part of the line that changed
I used this link as a reference for the color numbers.
Note: I didn't set the gui options because I use a different colorscheme for macvim/gvim
If you're calling vimdiff from the command-line, put the following in your .vimrc:
if &diff
colorscheme some_other_scheme
endif
If you're using vimdiff from within vim, you'd either have to override the commands you use to start/stop it (e.g. diffthis, diffoff) using :cnoreabbr (there's also a plugin) or use an autocommand:
au FilterWritePre * if &diff | colorscheme xyz | endif
FilterWritePre is called before filtering through an external program (the diff utility) and the &diff-option is set by vim when it's going into diff-mode (among others, see :help diff)
I'm not sure which autocommand to use to return to the original colorscheme though.
To answer my own question:
if &diff
colorscheme evening
endif
molokai:
github:
The two themes github and molokai are equally beautiful.
curl -fLo ~/.vim/colors/molokai.vim --create-dirs https://raw.githubusercontent.com/tomasr/molokai/master/colors/molokai.vim
curl -fLo ~/.vim/colors/github.vim --create-dirs https://raw.githubusercontent.com/endel/vim-github-colorscheme/master/colors/github.vim
Put the following code in your ~/.vimrc, you can choose github or molokai (a line starting with a " is a comment):
if &diff
" colorscheme github
colorscheme molokai
endif
I found the easiest way was to paste this one-liner into my ~/.vimrc file:
" Fix the difficult-to-read default setting for diff text highlighting. The
" bang (!) is required since we are overwriting the DiffText setting. The highlighting
" for "Todo" also looks nice (yellow) if you don't like the "MatchParen" colors.
highlight! link DiffText MatchParen
If you are encountering unreadable color schemes (not just ugly, but unreadable like white text on pink background), an easy fix may be to use 16 colors instead of 256 colors. Then you don't have to mess with the color schemes.
The reason is that the default vimdiff color scheme assigns DiffChange bg as "LightMagenta", which gets mapped to a very light pink in 256 colors. That is unreadable with white text. With 16 colors, the "LightMagenta" is mapped to a bold magenta, which white text shows up much better on.
You can give a quick test by doing something like this:
vimdiff <file1> <file2>
:set t_Co? " print current setting (256 by default)
:highlight " print highlighting scheme
:set t_Co=16 " set to 16 colors
:highlight " print highlighting scheme
256-color screenshot
16-color screenshot
As you can see, the 16 colors is much more readable, without changing the color scheme.
To make this permanent, you can add set t_Co=16 to your .vimrc
For people that use the very excellent Solarized theme there's an option that turns on high visibility for diff mode:
" ~/vim.rc
" Set high visibility for diff mode
let g:solarized_diffmode="high"
"normal"
"high"
"low"
my current colorscheme does not show some diffs properly in vimdiff, For. eg some diff is shown with same fg/bg color
Actually, I've found that the main culprit for same fg/bg color is because of conflict between code syntax highlighting and diff colorscheme. You can try to change the diff colorscheme, but it may be a game of whack-a-mole when you open different file types (with different code syntax highlighting).
A sure solution is to disable the syntax highlighting in vimdiff. You can either type:
:syntax off
Or if you want to automatically do this every time, then add this to the end of your ~/.vimrc:
if &diff
syntax off
endif
Another approach is to fix that color scheme.
As far as I know, there are usually four highlight groups relative to diff'ing: DiffAdd, DiffChange, DiffDelete, and DiffText. If you don't want to be bothered about the syntax or tweaking the colors to your liking, you could probably copy your default color scheme under another name to ~/.vim/colors (create the directory if it doesn't exist) and copy paste the corresponding :hi commands from your alternative color scheme to the end of your new custom color scheme, optionnally commenting out any other diff-related statements therein.
And if the result is an obvious improvement, send an email to the maintainer of your color scheme with your changes and ask him to look into the problem. There's a good chance that he will thank you for your interest and that he will fix his color scheme so that other users will also benefit..
/etc/vim/vimrc or ~/.vimrc:
If using a dark background within the editing area and syntax highlighting turn on this option as well set background=dark
To expand on #dean and some other answers here, add this to your .vimrc:
if &diff
" colorscheme evening
highlight DiffAdd cterm=bold ctermfg=10 ctermbg=17 gui=none guifg=bg guibg=Red
highlight DiffDelete cterm=bold ctermfg=10 ctermbg=17 gui=none guifg=bg guibg=Red
highlight DiffChange cterm=bold ctermfg=10 ctermbg=17 gui=none guifg=bg guibg=Red
highlight DiffText cterm=bold ctermfg=10 ctermbg=88 gui=none guifg=bg guibg=Red
endif
I use the following when using vimdiff from within vim:
au BufEnter,BufNew * if &diff | syntax off | else | syntax on | endif
The part with else statement is important because that's how you go back to your previous config after you are done with diff'ing. So you can replace syntax off and syntax on with respective colorscheme commands. This autocmd handles changing a setting and reverting it when quitting vimdiff (I use Gdiff to be precise).
None of the solutions were working for me. When I used the if &diff check, it was only working if I resourced my config after opening the diff (:Gdiff from fugitive.vim plugin). It wasn't opening automatically. Moreover, after quitting the diff pane, I had to resource to get back my original color scheme.
Hence, I ended up creating custom maps that would activate the required color scheme.
map ,m :colorscheme molokai<CR>
map ,c :colorscheme PaperColor<CR>
map ,g :colorscheme gruvbox<CR>
So far, this is the most promising solution I found, even though it's a bit of a hack and I would've liked it if the color scheme changed automatically.
However, this way, I can apply any color scheme quickly at my leisure irrespective of whether I am in a diff window or not.
The slate colorscheme which comes standard with most vim installations works fine for me. FWIW, I work with a dark background. Thus I simply add the following to my .vimrc:
if &diff
colorscheme slate
endif

Resources