When working at my desk I work on a 24” monitor and I like to use a pretty big font size when there is enough space for it. But often I also work on my laptop, where there is much less screen real estate. I used to change the font size manually with ⌘ and the +/– keys. Yesterday I decided to automate things by letting Vim do the heavy lifting.
I found a small piece of AppleScript that outputs the dimensions of the currently available screens. I decided to check the number of vertical pixels, as they always increase when you attach an external monitor due tue the fact that most monitors have screen ratios higher than 16:9. The script switches font sizes if there are more than 900 vertical pixels, meaning a resolution higher than the native resolution of all 13” or smaller MacBooks.
osascript command returns something similar to
0, 0, 3200, 1200. The first two arguments are the distances from the edge of the screen (left and top side), the third and fourth arguments are the horizontal and vertical width of the screen. I’m only interested in the fourth argument, so I use
cut to select it.
Performance wise this is pretty fast. I timed the command to take about ~0,01s on average, which is more than fast enough as the check is only performed when you start Vim. That is also a point I would like to improve on in the future. It would be awesome if it could detect screen switching automatically. But for now I am more than happy with the solution I came up with.
To put it all together in Vim add the following code to your
~/.vimrc. Change the font and sizes to your desire:
1 2 3 4 5 6 7 8 9