Switching Fonts#

Nvim does not natively support setting a specific font. I still wanted
a custom font for my .md files, so I made a autocmd that checks if nvim enters a markdown file and if so changes the Kitty font.

 
Create 2 files in kitty: ~/.config/kitty:

 
font_default.conf:

include ./kitty.conf

font_family JetBrainsMono Nerd Font Mono
font_size 13

font_markdown.conf:

include ./kitty.conf

font_family Garamond Mono
font_size 13

 
and in your kitty.conf just append at the end:

# BEGIN_KITTY_FONTS
font_family      family="Droid Sans Mono"
bold_font        auto
italic_font      auto
bold_italic_font auto
# END_KITTY_FONTS
font_size 13

 
This autocmd handles the switch:

-- ------------------------------------
-- --- Kitty Font Switch for .md --- --
-- ------------------------------------
local default_font_conf = "~/.config/kitty/font_default.conf"
local markdown_font_conf = "~/.config/kitty/font_markdown.conf"

local function switch_kitty_font()
  local current_ft = vim.bo.filetype
  local buf_name = vim.api.nvim_buf_get_name(0)

  -- 1. to .md font
  if current_ft == "markdown" then
    local cmd = string.format("kitty @ load-config %s", markdown_font_conf)
    vim.schedule(function() vim.fn.system(cmd) end)
    return
  end

  -- exeptions
  if current_ft == "prose_summary" or buf_name:find("ProseAnalyzer Summary") or current_ft == "" then
    return 
  end

  -- 3. Liste standard font
  local force_default_ft = {
    ["lua"] = true,
    ["sh"] = true,
    ["python"] = true,
    ["conf"] = true,
    ["kitty"] = true,
    ["help"] = true,
  }

  -- 4. set back 
  if force_default_ft[current_ft] then
    local cmd = string.format("kitty @ load-config %s", default_font_conf)
    vim.schedule(function() vim.fn.system(cmd) end)
  end
end

local font_switcher_group = vim.api.nvim_create_augroup("KittyFontSwitcher", { clear = true })

-- toggle switch
vim.api.nvim_create_autocmd({ "FileType", "BufWinEnter" }, {
  pattern = "*",
  group = font_switcher_group,
  callback = function()
    switch_kitty_font()
  end,
})

-- back to default on quit
vim.api.nvim_create_autocmd("VimLeave", {
  group = font_switcher_group,
  callback = function()
    local cmd = string.format("kitty @ load-config %s", default_font_conf)
    vim.fn.system(cmd)
  end,
})