如何在 Vim 中设置默认字体大小?

我正在尝试用 Vim 配置 GUI 的默认设置。我已经在网上做了研究,但我找到的所有解决方案和尝试没有工作。

下面是我尝试过的一些方法(在. vimrc 文件中) :

set guifont = Monaco:h20
set guifont=Monospace 20

其实我不在乎摩纳哥字体。

167424 次浏览

For the first one remove the spaces. Whitespace matters for the set command.

set guifont=Monaco:h20

For the second one it should be (the h specifies the height)

set guifont=Monospace:h20

My recommendation for setting the font is to do (if your version supports it)

set guifont=*

This will pop up a menu that allows you to select the font. After selecting the font, type

set guifont?

To show what the current guifont is set to. After that copy that line into your vimrc or gvimrc. If there are spaces in the font add a \ to escape the space.

set guifont=Monospace\ 20

Try a \<Space> before 12, like so:

:set guifont=Monospace\ 12

Add Regular to syntax and use gfn:

set gfn= Monospace\ Regular:h13

The other answers are what you asked about, but in case it’s useful to anyone else, here’s how to set the font conditionally from the screen DPI (Windows only):

set guifont=default
if has('windows')
"get dpi, strip out utf-16 garbage and new lines
"system() converts 0x00 to 0x01 for 'platform independence'
"should return something like 'PixelsPerXLogicalInch=192'
"get the part from the = to the end of the line (eg '=192') and strip
"the first character
"and convert to a number
let dpi = str2nr(strpart(matchstr(substitute(
\system('wmic desktopmonitor get PixelsPerXLogicalInch /value'),
\'\%x01\|\%x0a\|\%x0a\|\%xff\|\%xfe', '', 'g'),
\'=.*$'), 1))
if dpi > 100
set guifont=high_dpi_font
endif
endif

I cross over the same problem I put the following code in the folder ~/.gvimrc and it works.

set guifont=Monaco:h20

You may find useful this plugin I made to simplify setting guifont in a portable way: https://github.com/awvalenti/vim-simple-guifont. Your vimrc goes like this and it handles all the OS specific stuff:

silent! call simple_guifont#Set(
['Cascadia Code PL', 'JetBrains Mono', 'Hack'], 'Consolas', 14)

set guifont=Lucida\ Console:h10

In Ubuntu 22, for gvim, setting the "Ubuntu Mono" font with size 11 will be this in .vimrc:

set guifont=Ubuntu\ Mono\ 11