简体   繁体   English

在 .vimrc 文件中设置 gvim 字体

[英]set gvim font in .vimrc file

I am using gVim 7.2 on Windows 7. I can set the gui font as Consolas 10 (font size) from the menu.我在 Windows 7 上使用 gVim 7.2。我可以从菜单中将 gui 字体设置为 Consolas 10(字体大小)。 I am trying to set this in .vimrc file like below:我试图在.vimrc文件中设置它,如下所示:

set guifont=Consolas\ 10

But it doesn't work.但它不起作用。 Does anyone know how to set this?有谁知道这个怎么设置?

I use the following (Uses Consolas size 11 on Windows, Menlo Regular size 14 on Mac OS X and Inconsolata size 12 everywhere else):我使用以下(在 Windows 上使用尺寸为 11 的 Consolas,在 Mac OS X 上使用尺寸为 14 的 Menlo 常规尺寸,在其他地方使用尺寸为 12 的 Inconsolata):

if has("gui_running")
  if has("gui_gtk2")
    set guifont=Inconsolata\ 12
  elseif has("gui_macvim")
    set guifont=Menlo\ Regular:h14
  elseif has("gui_win32")
    set guifont=Consolas:h11:cANSI
  endif
endif

Edit: And while you're at it, you could take a look at Coding Horror's Programming Fonts blog post .编辑:当你在做的时候,你可以看看 Coding Horror 的 编程字体博客文章

Edit²: Added MacVim.编辑²:添加了 MacVim。

Try setting your font from the menu and then typing尝试从菜单中设置您的字体,然后输入

:set guifont?

This should display to you the string that Vim has set this option to.这应该向您显示 Vim 已将此选项设置为的字符串。 You'll need to escape any spaces.你需要逃避任何空间。

I am trying to set this in .vimrc file like below我正在尝试在 .vimrc 文件中设置它,如下所示

For GUI specific settings use the .gvimrc instead of .vimrc , which on Windows is either $HOME\\_gvimrc or $VIM\\_gvimrc .对于 GUI 特定设置,请使用.gvimrc而不是.vimrc ,它在 Windows 上是$HOME\\_gvimrc$VIM\\_gvimrc

Check the :help .gvimrc for details.查看:help .gvimrc了解详细信息。 In essence, on start-up VIM reads the .vimrc .本质上,在启动时 VIM 读取.vimrc After that, if GUI is activated, it also reads the .gvimrc .之后,如果 GUI 被激活,它还会读取.gvimrc IOW, all VIM general settings should be kept in .vimrc , all GUI specific things in .gvimrc . IOW,所有 VIM 常规设置都应该保存在.vimrc ,所有 GUI 特定的东西都应该保存在.gvimrc (But if you do no use console VIM then you can simply forget about the .vimrc .) (但如果你不使用控制台 VIM,那么你可以简单地忘记.vimrc 。)

set guifont=Consolas\\ 10设置 guifont=Consolas\\10

The syntax is wrong.语法错误。 After :set guifont=* you can always check the proper syntax for the font using :set guifont?:set guifont=*你总是可以使用:set guifont?检查字体的正确语法:set guifont? . . VIM Windows syntax is :set guifont=Consolas:h10 . VIM Windows 语法是:set guifont=Consolas:h10 I do not see precise specification for that, though it is mentioned in the :help win32-faq .虽然在:help win32-faq提到了它,但我没有看到确切的规范。

  1. Start a graphical vim session.启动图形 vim 会话。
  2. Do :e $MYGVIMRC Enter:e $MYGVIMRC输入
  3. Use the graphical font selection dialog to select a font.使用图形字体选择对话框来选择字体。
  4. Type :set guifont= Tab Enter .输入:set guifont= Tab Enter
  5. Type G o to start a new line at the end of the file.键入G o在文件末尾开始一个新行。
  6. Type Ctrl + R followed by : .键入Ctrl + R后跟:

The command in step 6 will insert the contents of the : special register which contains the last ex-mode command used.步骤 6 中的命令将插入:特殊寄存器的内容,其中包含最后使用的 ex-mode 命令。 Here that will be the command from step 4, which has the properly formatted font name thanks to the tab completion of the value previously set using the GUI dialog.这将是步骤 4 中的命令,由于先前使用 GUI 对话框设置的值的制表符完成,该命令具有格式正确的字体名称。

For Windows do the following:对于 Windows,请执行以下操作:

  1. Note down the font name and font size from the "Edit-Select Font..." menu of "gvim.exec".从“gvim.exec”的“Edit-Select Font...”菜单中记下字体名称和字体大小。
  2. Then do :e $MYGVIMRC然后做:e $MYGVIMRC
  3. Search for "guifont" string and change it to set guifont=<font name as noted>:h<font size>搜索“guifont”字符串并将其更改为set guifont=<font name as noted>:h<font size>
  4. Save the file and quit.保存文件并退出。
  5. Next time when you execute gvim.exec, you will see the effect.下次执行gvim.exec,就会看到效果。

Although this is an old thread I thought that I would add a comment as I have come across it whilst trying to solve a similar issue;虽然这是一个旧线程,但我认为我会添加评论,因为我在尝试解决类似问题时遇到了它; this might help anyone else who may find themselves here:这可能会帮助其他可能在这里找到自己的人:

The backslash character is used to ignore the next character;反斜杠字符用于忽略下一个字符; once added to the font name in my gvimrc it worked;一旦添加到我的 gvimrc 中的字体名称,它就起作用了; I am on a GNU/Linux machine which does not like spaces.我在一台不喜欢空格的 GNU/Linux 机器上。 I suspect that the initial post was an error due to the back slash being used on a windows machine.我怀疑最初的帖子是一个错误,因为在 Windows 机器上使用了反斜杠。

In example:例如:

:set guifont?  ## From gvim command, would give the following:

set guifont=DejaVu Sans Mono for Powerline 11 set guifont=DejaVu Sans Mono for Powerline 11

Where as I needed to add this line to the gvimrc file for it to be read:因为我需要将此行添加到 gvimrc 文件以供读取:

set guifont=DejaVu\ Sans\ Mono\ for\ Powerline\ 11

我不得不结束

:set guifont=Courier:h10:cANSI

Ubuntu 14.04 LTS Ubuntu 14.04 LTS

:/$ cd etc/vim/
:/etc/vim$ sudo gvim gvimrc

After if - endif block, typeif - endif块之后,键入

set guifont=Neep\ 10

save the file (:wq!).保存文件 (:wq!)。 Here "Neep" (your choice) is the font style and "10" is respect size of the font.这里“Neep”(您的选择)是字体样式,“10”是字体大小。 Then build the font - cache again.然后再次构建字体 - 缓存。

:/etc/vim$ fc-cache -f -v

Your desired font will set to gvim.您想要的字体将设置为 gvim。

When I try:当我尝试:

set guifont=Consolas:h16

I get: Warning: Font "Consolas" reports bad fixed pitch metrics我得到: Warning: Font "Consolas" reports bad fixed pitch metrics

and the following is work, and don't show the waring.以下是工作,不要显示警告。

autocmd vimenter * GuiFont! Consolas:h16

by the way, if you want to use the mouse wheel to control the font-size, then you can add:顺便说一句,如果你想使用鼠标滚轮来控制字体大小,那么你可以添加:

function! AdjustFontSize(amount)
    let s:font_size = s:font_size + a:amount
    :execute "GuiFont! Consolas:h" . s:font_size
endfunction

noremap <C-ScrollWheelUp> :call AdjustFontSize(1)<CR>
noremap <C-ScrollWheelDown> :call AdjustFontSize(-1)<CR>

and if you want to pick the font, you can set如果你想选择字体,你可以设置

set guifont=*

will bring up a font requester, where you can pick the font you want.将调出一个字体请求器,您可以在其中选择您想要的字体。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM