How to save font choice in gVim?

Solution 1:

Close. Set the font through the gui, then use the command (: to get the prompt) set gfn? to get the current font string. It should look something like this:

guifont=Mono Uralic 10

Then edit/create ~/.gvimrc and add the line:

set gfn=Mono\ Uralic\ 10

Note: You need to escape the spaces from the output (as I have above)

Solution 2:

Here's an automated approach. (I've also made the code below a plugin.)

Select the font you want to use.

Paste this into Gvim in command mode (to set up the map):

map -- :let @a=&gfn<CR>:e ~/.gvimrc<CR>Go<Esc>"apV:s/ /\\ /g<CR>Iset guifont=

Then type -- (to activate the mapping).

You should now be editing your .gvimrc with your current font set at the bottom. Save the file and open another Gvim to test that it works correctly.


What the mapping does:

  • :let @a=&gfn<CR> Copy the current font setting into our a register
  • :e ~/.gvimrc<CR> Edit our gvimrc (whether it exists or not)
  • Go<Esc> Add a new line to the end of the file
  • "ap Paste the font setting
  • V:s/ /\\ /g<CR> Escape spaces
  • Iset guifont= Put the set variable text before our setting

Solution 3:

two options here to save font choice for Gvim

  1. in $HOME/.vimrc file (this config file is for terminal Vim):
if has('gui_running')
    set guifont=Consolas\ Regular\ 12
endif
  1. make a separate config file for Gvim, $HOME/.gvimrc:
" use vim config
source $HOME/.vimrc
" set gui font
set guifont=Consolas\ Regular\ 12

Currently, I am using Vim8.0, thus the vimscript grammar is different from version 7.x