0

I'm using neovide (a graphical interface for nvim), so I lost ctrl+[-+] to change the font size. I'm trying to rewrite it in my lua config but I'm pretty new to lua so I'm struggling to implement the function. Here's my code :

vim.g.font_size = 11
vim.o.guifont='FantasqueSansMono Nerd Font Mono:h'..vim.g.font_size
vim.keymap.set('n', '<C-->',
    ':let g:font_size = (g:font_size - 1)<CR>:let &guifont = \'FantasqueSansMono Nerd Font Mono:h'..vim.g.font_size..'\'<CR>')

What I'm trying to do, is to have a variable font_size that I can increment or decrement, then use the new value to update the size of the font. The first two lines work perfectly, they always set the correct font size when launching a new instance of neovide. The keymap on the other hand will decrement / increment font_size like intended but the second command of the mapping will always use the value written at line 1.

For instance, if I start neovide and press ctrl+-, the status line will show :let &guifont = 'FantasqueSansMono Nerd Font Mono:h11', but if I enter the following command : echo g:font_size, I get 10. Is there a way to fix this problem ? (or a more elegant way to fix it haha)

1 Answers1

0

This line:

':let g:font_size = (g:font_size - 1)<CR>:let &guifont = \'Consolas:h'..vim.g.font_size..'\'<CR>'

Is building a string that uses the current version of vim.g.font_size at the time the string is built. That ends up being this string:

':let g:font_size = (g:font_size - 1)<CR>:let &guifont = \'Consolas:h11\'<CR>'

So your mapping looks like this:

vim.keymap.set('n', '<C-->', ':let g:font_size = (g:font_size - 1)<CR>:let &guifont = \'Consolas:h11\'<CR>'

See what's happening now?

Side note, Lua supports several string delimeters ("x", 'x', or [[x]]), so you don't have to escape your internal strings if you use a different outer delimeter, like this:

":let g:font_size = (g:font_size - 1)<CR>:let &guifont = 'Consolas:h11'<CR>"

The binding yoiu really want is this:

vim.keymap.set('n', '<C-->', ":let g:font_size = (g:font_size - 1)<CR>:let &guifont = 'Consolas:h'.g:font_size<CR>"

But if you really want the binding to be Lua, you'd dot his:

vim.keymap.set('n', '<C-->', ":luado vim.g.font_size = vim.g.font_size - 1; vim.o.guifont='Consolas:h'..vim.g.font_size<CR>")
Mud
  • 28,277
  • 11
  • 59
  • 92