Why doesn't the font setting work in gvim? - vim

In Fedora15, I was originally using vim, with all my settings defined in my ~/.vimrc file. I recently installed vim-X11, and have now transitioned to gvim. I defined a ~/.gvimrc file, with the following line to set the font:
set guifont=Bitstream_Vera_Sans_Mono:h11:cANSI
However, it doesn't work. There are two strange things:
The other settings such as line number, color scheme works right, but font setting doesn't work. Instead, I need to set them in the tool bar every time to change the font: "Edit->Select Font".
Every time I select "Edit -> Startup Settings", it opens the ~/.vimrc file instead of the ~/.gvimrc file.

Your guifont is valid only on Windows. In GTK GVim, you want:
set guifont=Bitstream\ Vera\ Sans\ Mono\ 11
Also, you can use set guifont=* to bring up a system dialog to choose a font, and then look at the contents of the setting with set guifont?.
GVim loads .vimrc and then .gvimrc, so you can separate settings that apply only to GUI version.

I find use set guifont=* then set guifont? will not work, butset guifont=Bitstream\ Vera\ Sans\ Mono\ 11 nice

Related

Vim opening terminal with different profile than the default

I 'm working on MacOS Catalina and I am using vim 8.2
The only profile set up in my iterm2 is the Default one.
which has been set up to used a slightly customized solarized theme as depicted below
I am now opening vim (which has also been set up to use its counterpart solarized colorscheme)
My question is, why when I am opening the :terminal in vim, this opens in black background (I don't think there even exists a profile with such setting)
This will happen when you have Vim running with 'termguicolors' on. (You can confirm it and see where it's being enabled with :verb set tgc?.) Same as when running in a GUI (not the terminal), it will tell Vim to use 24-bit colors, and use the color scheme full RGB color specifications, rather than use the terminal color palette.
In that situations, Vim's colors get completely detached from those of the terminal where Vim is running, so Vim's built-in terminal no longer has access to the actual terminal colors.
You can set specific colors to use in Vim's built-in terminal with the g:terminal_ansi_colors variable (see :help g:terminal_ansi_colors for details.)
Alternatively, disabling 'termguicolors' should work as well. Assuming the Vim color scheme has been built in a way that will use the terminal color scheme to reproduce the same colors, it might not have other side effects in how Vim colors otherwise look.
Removing line
set termguicolors
from .vimrc fixed it

Custom Fonts in VIM

I'm somewhat new to vim and wanted to add some custom fonts.
This is the one I wanted to add: https://github.com/belluzj/fantasque-sans.
How would I tell vim to use this font since it's not part of the default font set.
Do I add this to the .vimrc?
I'm on Mac Catalina by the way.
If you're using vim in the command line: Vim uses the font of your terminal emulator so if you set up your terminal emulator (iTerm2, Terminal.app etc) to use that font it should pick it up automatically.
If you want that font to be used exclusively in vim and you're using gvim you can change the guifont option which is documented on the Vim FAQ:
"You can change the 'guifont' option to change the font used by GUI Vim. To
display the current value of this option, you can use
:set guifont?
You can add the displayed font name to the .vimrc file to use the font
across Vim sessions. For example, add the following line to the .vimrc file
to use Andale Mono font.
set guifont=Andale_Mono:h10:cANSI
For Win32, GTK and Photon version of Vim, you can use the following command
to bringup a dialog which will help you in changing the guifont:
:set guifont=*
You can also use the -font Vim command line option to specify the font used
for normal text."

Fail to set font in Macvim

I am configuring macvim on Mac OS X Mavericks. I type "set guifont=Menlo\ 14" in both ~/.vimrc and ~/.gvimrc but the editor remains displaying in courier.
I insert ":set guifont?" and it reveals "guifont=Menlo\ 14". I overviewed the content of .vimrc and .gvimrc but found no other line overriding the guifont setting.
I have checked several posts in stack overflow but cannot yet find the solution. Wish to get some help, thanks for anticipation.
The ideal way of going about doing this is by invoking the following command from vim :
set guifont=*
That will open a font selection dialog box and you can select whatever you would like there. Once done, you should then check what the option was set to by using the command you mentioned yourself :
set guifont?
Now you should put the output of that in your .gvimrc (not .vimrc) and additionally you'll need to escape the spaces like you're doing already. There might be subtle mistakes in what you're setting yourself, the font size for instance can sometimes be set in different ways.
On my version of MacVim (7.4.258) and running EL Kapitan, putting
set guifont=*
in .vimrc caused MacVim to spit out the following error (when run from the command line, starting it from the Dock caused it to just not run at all):
E665: Cannot start GUI, no valid font found
So it seems the correct way is to start MacVim, use the Edit > Font > Show Font menu, pick a font, then use
set guifont?
to find the line to add to your .vimrc
Also remember to use forward slashes to escape any spaces in the font name.
To change font, use Edit -> Font -> Show Fonts and selected a font.
Unfortunately this setting gets wiped out with each update of MacVim.
To have your font setting persisted in MacVim add this to your ~/.gvimrc
set guifont=Hack\ Nerd\ Font:h12

How to view UTF-8 Characters in Vim or gVim

I work on webpages involving non-English scripts from time to time, most of them are encoded using UTF-8.
Vim and gVim do not display those UTF-8 characters correctly.
I'm using Vim 7.3.46 on Windows 7, 64-bit, with set guifont=Monaco:h10 in _vimrc.
Is there a way to fix this?
Update: I've googled around and found set guifontwide acts as second fallback for regional languages.
I added the following lines to _vimrc and most of my problems got solved.
set enc=utf-8
set fileencoding=utf-8
set fileencodings=ucs-bom,utf8,prc
set guifont=Monaco:h11
set guifontwide=NSimsun:h12
The above NSimsun font works for Chinese, The problem is, I don't know how they got the font name to work with Vim, Courier New is mentioned as Courier_New also NSimsun is nowhere in the font directory. The font I want to use is Latha But, I don't know how to use it in the _vimrc file. set guifontwide=latha:h12 or set guifontwide=Latha:h12 doesn't work.
If I successfully set the guifontwide to latha, then my problem will be solved. How to do it?
Did you try
:set encoding=utf-8
:set fileencoding=utf-8
?
Try to reload the document using:
:e! ++enc=utf8
If that works you should maybe change the fileencodings settings in your .vimrc.
If Japanese people come here, please add the following lines to your ~/.vimrc
set encoding=utf-8
set fileencodings=iso-2022-jp,euc-jp,sjis,utf-8
set fileformats=unix,dos,mac
On Microsoft Windows, gvim wouldn't allow you to select non-monospaced fonts. Unfortunately Latha is a non-monospaced font.
There is a hack way to make it happen: Using FontForge (you can download Windows binary from http://www.geocities.jp/meir000/fontforge/) to edit the Latha.ttf and mark it as a monospaced font. Doing like this:
Load fontforge, select latha.ttf.
Menu: Element -> Font Info
Select "OS/2" from left-hand list on Font Info dialog
Select "Panose" tab
Set Proportion = Monospaced
Save new TTF version of this font, try it out!
Good luck!
this work for me and do not need change any config file
vim --cmd "set encoding=utf8" --cmd "set fileencoding=utf8" fileToOpen
In Linux, Open the VIM configuration file
$ sudo -H gedit /etc/vim/vimrc
Added following lines:
set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936
set termencoding=utf-8
set encoding=utf-8
Save and exit, and terminal command:
$ source /etc/vim/vimrc
At this time VIM will correctly display Chinese.
Is this problem solved meanwhile?
I had the problem that gvim didn't display all unicode characters (but only a subset, including the umlauts and accented characters), while :set guifont? was empty; see my question. After reading here, setting the guifont to a sensible value fixed it for me. However, I don't need characters beyond 2 bytes.
I couldn't get any other fonts I installed to show up in my Windows GVim editor, so I just switched to Lucida Console which has at least somewhat better UTF-8 support. Add this to the end of your _vimrc:
" For making everything utf-8
set enc=utf-8
set guifont=Lucida_Console:h9:cANSI
set guifontwide=Lucida_Console:h12
Now I see at least some UTF-8 characters.
On Windows gvim just select "Lucida Console" font.

vim colorschemes not changing background color

I try to apply various color schemes in vim that I have seen on the net. Whatever scheme I choose, the background remains white, even though screenshots of the applied scheme shows that the background should be colored.
In some schemes, some of the background change color, but space right of lines containing text still remains white.
I'm using Vim 7.2 on a mac. I have just started messing with non-gui applications, so everything should be pretty much as it was out of the box..
Does the overall settings for the terminal window have something to do with it?
When running macvim, everything looks ok. Its only when starting vim from the terminal things looks strange..
I have this in my .vimrc and it solved this problem for me using while using PuTTY.
set t_Co=256
set background=dark
colorscheme mustang
highlight Normal ctermbg=NONE
highlight nonText ctermbg=NONE
It's important to load the colorscheme before the ctermbg settings in .vimrc because they need to override the same ones set by the colorscheme. This also means you can't switch colorscheme while Vim is running and expect it to work.
I'm adding a second answer from me because it's very different from my first answer and may point to actual problem.
If you look at the actual website for the colorscheme here:
Molokai website
you will see a question very similar to yours. Here's answer given, which suggests trying command :set t_Co=256 in your vimrc to see if it fixes things:
"- Make sure you’re using a console terminal capable of 256 colors; not all of them do (particularly on mac). You might need to explicitly force Vim to use that by doing “set t_Co=256″ on your .vimrc file.
- The windows console is well… totally unsupported, that only does 16 colors so it’s a mess"
In linux I had export TERM=xterm-256color in my .bashrc. That caused vim to look like this (after setting set t_Co=256):
When I removed that line from my .bashrc and opened a new terminal (exec bash didn't do it) This is what I get:
Terminals are usually limited to 256 colors while GUI are only limited by color depth of your desktop environment, typically 2^32.
So even if there is lots of vim color scheme available around, implicitly they are often designed for the GUI and won't work for the terminal version.
If you look at color schemes on vim.org, there is often a mention of GUI or 256. So you have to chose which to use depending on the context.
To convert a GUI scheme to terminal you can use the following plugin :
CSApprox.
You can also use a different colorscheme depending on the context, add the following in your .vimrc:
if has("gui_running")
colorscheme [using any color you want]
else
colorscheme [using 256 colors]
endif
I think the problem could be the way the default color is changed by the colorscheme. I've looked at some colorschemes that set default merely by:
set background=light
or
set background=dark
Not sure what limitations of those are. I don't think those work in terminals.
In any case, you should be able to manually set background in a terminal by using the 'Normal' highlight. Insert it into a spot before most of the 'hi' commands in the colorscheme file and it should provide defaults they will work with. For example:
hi Normal ctermbg=White ctermfg=Black guifg=Black guibg=White
Change ctermfg (color terminal foreground) and ctermbg (color terminal background) to be whatever you want (or whatever color you were expecting to see in the colorscheme but now aren't seeing). (Remember, though, if the colorscheme already has a setting for hi Normal then this probably isn't your problem.)
For ctermbg and ctermfg you can enter color names, but I think there is only a fairly limited number:
Black
DarkBlue
DarkGreen
DarkCyan
DarkRed
DarkMagenta
Brown, DarkYellow
LightGray, LightGrey, Gray, Grey
DarkGray, DarkGrey
Blue, LightBlue
Green, LightGreen
Cyan, LightCyan
Red, LightRed
Magenta, LightMagenta
Yellow, LightYellow
White
Otherwise you should be able to use a number from 0 to 255 in place of the color name. Or this script gives rough idea, and lets you see how you could also set up to use more color names:
Vim script with color settings
Also, there are a number of scripts that help you use or convert colorschemes written for gui for use with cterm. E.g.,:
Colorscheme support for cterm
Does the overall settings for the terminal window have something to do with it?
Maybe, but I'm pretty sure a properly written Vim colorscheme will override any terminal settings you've made. At least they do for me in Windows and on Ubuntu. . .
You need to add set termguicolors to your ~/.vimrc
I tested t_Co=256 and other options, but none worked, only set termguicolors
After this you can use the command set bg=light or set bg=dark to see witch one looks better (some vim color schemes accept both options).
Here is a list of terminals that are compatible with termguicolors: https://gist.github.com/XVilka/8346728#now-supporting-true-color
I had the same problem and found out that the answer to this question is actually threefold, where fixing only two of the three isn't enough. You'll need to have:
256-color support in your terminal - Putty with default settings does have this
Vim has to recognize that the terminal is 256-color capable: "set t_Co=256" in your .vimrc will do it
The color scheme needs to have support for color terminals with ctermbg and ctermfg attributes for highlights, not just the gui*-versions. http://www.vim.org/scripts/script.php?script_id=2682 should be able to provide these automatically, and CSApprox I'm using most definitely does, but requires either +gui -compiled Vim or a recent enough Vim version (7.3 or newer).
The third one seems to be the most commonly missed requirement. I wrote a short piece on my own fumblings on this subject just this morning: http://codeandlife.com/2013/09/22/vim-colorschemes-with-putty-aka-gui-vs-xterm-color256/
Final gotcha that happened to me while trying different settings was that when the colors did work, only areas of screen with text had the proper background color. Re-checking Putty Terminal setting "Use background colour to erase screen" fixed that final issue for me.
Does the overall settings for the terminal window have something to do with it?
Yes, terminal parameters override vim parameters (at least in OSX and iTerm). For example, I have a following script in /Users/[username]/.bashrc
setBackground() {
osascript -e "tell application \"iTerm\"
set current_terminal to (current terminal)
tell current_terminal
set current_session to (current session)
tell current_session
set background color to $1
end tell
end tell
end tell"
}
vim() {
(setBackground "{65025,65025,65025}" &)
(exec vim $*)
}
The above remaps terminal vim command to execute a background color change before executing vim. Background color function is applescript (I copied the script from somewhere...). It works for iTerm. I belive that you can adapt this to work with terminal (apple product + apple script -> should work).
br,
Juha
Use this rule if you use Vim through SSH:
Add to your local .bashrc:
export TERM=xterm-256color
Remove from .bashrc any TERM definitions.
If you use same .bashrc on both (local and remote), use temporary environment variable and never set TERM globally:
alias color-ssh='TERM=xterm-256color ssh user#host'
This works for me for switching backgrounds:
colorscheme hemisu
function! g:ToggleBackground()
if &background != 'dark'
set background=dark
else
set background=light
colorscheme hemisu
endif
endfunction
nnoremap <silent> <F3> :call g:ToggleBackground()<CR>
Also try setting light background to something like ctermbg=231, so that tmux handles it better.
I have similar issue that the background color of indentation guides (nathanaelkane's vim-indent-guides) cannot be displayed in my Windows Cygwin's mintty terminal.
I solved the issue with a line Term=xterm-256color in ~/.minttyrc (equivalent to set via mintty's Options GUI: Terminal -> Type -> xterm-256color. This has the effect export TERM=xterm-256color. Without this, mintty default to TERM="xterm", which result in vim's t_Co=8 (instead of t_Co=256) and cannot show some background color.
Checklist:
echo $TERM in bash should give xterm-256color.
in vim, :set t_Co should give t_Co=256.

Resources