Skip to content
MeanderingProgrammer edited this page Nov 6, 2024 · 29 revisions

Fonts

Preface

I am no expert on font rendering and some of the details below are likely oversimplified. If you find anything to be inaccurate or misleading please let me know!

The goal is to provide a quick reference to all neovim users, though more likely newer ones, to solve issues related to missing or bad looking icons in terminals. This is an early version and will be updated based on feedback.

If you just want a way to unblock yourself and already know all you want to about font rendering then focus on the Troubleshooting section. Otherwise I encourage you to checkout some of the other sections as well.

Troubleshooting

In order to use the tables below you need to know 3 pieces of information:

  1. What problem you are facing with icons. Possible values are:
    • Missing �: Icons are either clearly wrong or missing and being shown as a �.
    • Too Small: Icons are rendered smaller than you want and are hard to see.
    • Too Large: Icons are rendered larger than you want and look squished.
  2. The terminal emulator you are using for neovim. Currently we have info for:
  3. The type of Font you are using. Possible values are:
    • Nerd Default: This is if you're using a standard Nerd Font, meaning the name ends with "Nerd Font". For example "JetBrainsMono Nerd Font".
    • Nerd Mono: Similar to above, but the monospaced symbol variant, meaning the name ends with "Nerd Font Mono". For example "JetBrainsMono Nerd Font Mono", notice the second "Mono", that is the important part.
    • Base: This is the case if you're not not using any kind of Nerd Font, true if no other option is. Probably the case if you have not configured a font.

If your specific problem is not covered by these please reach out!

Once you have all that you can get to the solution. Use the table associated with your problem. Examples:

Problems

Missing �

Terminal / Font Base Nerd Default Nerd Mono
WezTerm Always Find Always Find Always Find
kitty Always Find Always Find Always Find
Alacritty Font -> Any Nerd Font -> New Nerd Font -> New Nerd

Legend:

  • ✅ = Potential Fix
  • ❌ = No Fix
  • ❓ = Impossible

Too Small

Terminal / Font Base Nerd Default Nerd Mono
WezTerm Wez Glyphs Grow Font -> Base Font -> Base
kitty Symbol Map Default Font -> Base Font -> Base
Alacritty Never Find Size Limit Font -> Nerd Default

Legend:

  • ✅ = Potential Fix
  • ❌ = No Fix
  • ❓ = Impossible

Too Large

Terminal / Font Base Nerd Default Nerd Mono
WezTerm Wez Glyphs Shrink Font -> Base Font -> Base
kitty Symbol Map Mono Font -> Base Font -> Base
Alacritty Never Find Font -> Nerd Mono Size Limit

Legend:

  • ✅ = Potential Fix
  • ❌ = No Fix
  • ❓ = Impossible

Solutions

Wez Glyphs

There are 2 levers we can pull to grow or shrink icons.

The first lever is through allow_square_glyphs_to_overflow_width.

To grow icons you'll need to allow them to overflow their cells:

config.allow_square_glyphs_to_overflow_width = 'WhenFollowedBySpace'
  • This is the default value so this is more of a sanity check that you did not disable this, but you can also set it explicitly if you like.

To shrink icons you'll need to prevent them from overflowing their cells:

config.allow_square_glyphs_to_overflow_width = 'Never'

At this point you can update the config, save, and check the size of the icons to see if you're happy with it. If you are then nothing else is needed.

Otherwise use the second lever through font_with_fallback scale.

In the example below you can replace the <scale> placeholder with a value > 1 (like 1.10) to grow icons or a value < 1 (like 0.90) to shrink icons.

config.font = wezterm.font_with_fallback({
    { family = '<your_current_font>' },
    { family = 'Symbols Nerd Font Mono', scale = <scale> },
})

Symbol Map

Set the font for the symbol_map described here to either a Default Nerd Font or a Mono Nerd Font as specified.

Font Base

Change terminal emulator font to any Base font. This means not a Nerd Font so does not contain the string "Nerd Font", even Comic Sans works. You can install a new font if you like but that is not necessary. The goal is to have the terminal emulator resolve icons for you using font fallback rather than using the icons that are part of the preferred Nerd Font.

Font Any Nerd

Change terminal emulator font to any Nerd Font. Follow these steps which fill install multiple variants of your chosen Nerd Font. When picking a variant choose any of them.

Font New Nerd

Change terminal emulator font to a new Nerd Font. Follow these steps but make sure the font you install is different from the one you are currently using. This is to make it likely that the new font will cover icons the previous one did not. When picking a variant choose any of them.

Font Nerd Default

Change terminal emulator font to a Default Nerd Font. Follow these steps which fill install multiple variants of your chosen Nerd Font. When picking a variant make sure to select the font that ends with "Nerd Font".

Font Nerd Mono

Change terminal emulator font to a Mono Nerd Font. Follow these steps which fill install multiple variants of your chosen Nerd Font. When picking a variant make sure to select the font that ends with "Nerd Font Mono".

Always Find

Some terminal emulators, particularly newer ones, have the ability to search through more than just your default font. If a particular icon is missing it will trigger font fallback which will search through system fonts as well as fonts that come bundled with the terminal emulator by the author. This should prevent any icons from being missing, so if you encounter this let me know!

Never Find

If a terminal emulator does not support font fallback and you are not using a Nerd Font of some sort it is unexpected any symbol renders in any way other than �, so if you encounter this let me know!

Size Limit

When Nerd Fonts are created from their Base Fonts the authors create multiple variants. We've talked about these before as Nerd and Nerd Mono. The difference between these is that the Nerd variant has double width icons which some terminal emulators can take advantage of when space allows. Nerd Mono on the other hand has single width icons which makes the entire font (base + icons) monospaced.

As a result if you're already using the Nerd variant you cannot get bigger icons. Similarly if you're using the Nerd Mono variant you cannot get smaller icons. If you find this to not be the case and their is a way around this let me know!

Install a Nerd Font

  1. Download a Nerd Font: A popular one is JetBrainsMono Nerd Font but any of them should work, pick the one you like. You can do this using:
    • homebrew
    • Your favorite package manager, instructions vary, please Google / LLM
    • website
  2. Install the Font: This depends on your OS and could involve moving a file to a specific directory, running some command, or is already handled by your package manager like with homebrew. You just need the font to be visible, Google / LLM should help here.
  3. Use the Font: Set the font as your terminal emulator's font. How depends on the terminal emulator and could be under a settings panel or done through a configuration file. A search for " font config" should get you there.
    • After installing the font you'll have multiple variants available, such as JetBrainsMono Nerd Font and JetBrainsMono Nerd Font Mono. Make sure to use the correct one if specified, otherwise the former is usually a safe choice.

Background

This section covers some of the high level details about why we use icons and why they don't always work out of the box.

So you've downloaded neovim and picked up a few plugins, nice! Then you open up your new file tree from neo-tree.nvim or take a look at your status line from lualine.nvim or an awesome markdown renderer :) and you notice the dreaded �, what's going on?

Well, like most people, up until now, you've only ever needed your terminal to display letters, numbers, and the basic symbols you find on your keyboard. So your font, which likely only implements these basic characters was never a problem. Now you want to display complex glyphs / icons, leading to this problem. When your font doesn't implement a particular icon, your terminal emulator (WezTerm, kitty, Alacritty, iTerm2, etc.) doesn't know what to do, so as a last resort will show you the �. At this point might have some questions, it's a good time to browse the FAQs to see if they're covered before moving on.

Font Fallback

WIP: WILL PROBABLY REWRITE

How text is rendered by your terminal emulator depends on the 2 things you would expect, the font and the terminal emulator.

This means the same font used by different terminal emulators will look different. Think of this as the font providing the raw data and the terminal emulator using the data to create an image. We'll cover these 2 sides separately.

Fonts

When we talk about fonts that includes many niche icons we're typically talking about "Patched Nerd Fonts".

These fonts are generated by taking some existing Base Font, like JetBrains Mono, and filling all the "gaps" with a set of common icons. This creates a new font with a name like JetBrainsMono Nerd Font, this kind of naming convention is used for most Patched Nerd Fonts. These will typically come with different variants of the same font like a version with monospaced icons (by default the icons are wider) which will be called JetBrainsMono Nerd Font Mono.

You may come across information saying you should not use these fonts and they're "bad practice" or "harmful". These are opinions with some legitimate reasons behind them. My stance, that you will need to read further to understand, is that if you can avoid using a Patched Nerd Font then you should not use one. However, there are situations where there are no other options and they are the only solution.

You may be wondering, well if these fonts are "bad" then what do I use, there doesn't even seem to be an alternative. This is where we get into the terminal emulator side.

Terminal Emulators

In the most basic case you set a font for your terminal emulator, it looks up some UTF-8 to get information on how to render a character and if it's missing then you get some default output like �. If this is the case for your terminal emulator then you need to use a Patched Nerd Font to get icons, there is no alternative.

However many terminal emulators, especially more recent ones, can take advantage of multiple fonts using what is called font fallback. Essentially you set a preferred font, and if an icon is missing, rather than giving up, the terminal emulator will go through some process to look at other available fonts and find one that does know how to render the icon. Available fonts can mean fonts on your system but also fonts that are bundled with the terminal emulator directly by the author. For these terminal emulators they will often come with a "symbols only" font which is just the icons and none of the standard letters / numbers, instead those are provided by your preferred font, which can now be anything, including Comic Sans. How the fallback works depends on the terminal emulator's implementation.

Terminal Font Fallback
WezTerm
kitty
Alacritty

If you are using a supported terminal emulator then in theory you should never see a �. You should also avoid using Patched Nerd Fonts and instead use the Base Font directly and allow the fallback to handle symbols. The reason to not use a patched font is that, as far as I know, letting the terminal emulator handles this leads to more consistent behaviors, i.e. icons all being pulled from a single font.

FAQs

Why doesn't my font have all the icons?

This is a very niche problem that most fonts don't need to solve. These missing icons are most likely what are referred to as Nerd Font Icons, and there are over 10,000 of them.

The idea of every font shipping with implementations for all these icons is crazy if you think about it. Who out there using Comic Sans in Word is trying to inline the GitHub logo as text? If font authors went through the trouble of making unique icons it would be an insane amount of work for a small portion of users. If they shared the same icons and kept the font part unique all fonts would get much bigger. Maybe not much of a problem now but still wasteful and extra effort.

GUI applications do not come with the same set of limitations as a terminal. They can render an image no problem. Terminals on the other hand are a fixed size grid where we can paint some pixels in each cell. We've essentially taken some UTF-8 encodings and mapped them to icons using the power fonts.

But I didn't add any icons?

Well maybe not directly, but some plugin you're using did. Somewhere in the code for the plugin you'll find the UTF-8 encoded character that represents the icon your font is missing.

Why do we need fancy icons, this is a terminal?

You're technically correct, we do it for the aesthetic :P I'll add that icons, when used effectively, can give you a lot of information at a glance. With file trees for instance they will tell you what's a folder and the type of each file, once you know what you're looking at. It's likely a bit of both and it's totally reasonable to want things to look nice in an environment you spend hours a day in.

The purist may disagree, and the purist is free to use something else or write it themselves.

Can I use different font sizes for different parts of the screen?

No, this is not possible in any terminal emulator that I am aware of. I'm not really sure where the fundamental limitation is. Terminals have been grids since their inception, but in theory this is not mandatory. Though I do not believe there are any ANSI sequences that exist that would allow text size information to be communicated to the terminal emulator. Maybe one day ANSI sequences will be added, terminals will be updated, and neovim will add support, or maybe there's more challenges involved, or maybe it's just not that important. In either case for now this doesn't exist.

Clone this wiki locally