That’s it for now! Please take it for a spin and let me know if you find something I can improve on.

Have a smart configuration in your ~/.vimrc yourself? Turn it into a plugin! A good place to start (and the source of most of my VimL-fu) is learnvimscriptthehardway.steve.

Show thread

That’s it! Sourcing our plugin once more, we can switch macOS’s appearance to dark mode and have Vim automatically follow when it gains focus.

Show thread

To add our autocommand to our plugin, we’ll wrap it in an `augroup` to make sure it’s reloaded properly when we load the plugin multiple times, for whatever reason:

augroup nightfall
autocmd!
autocmd FocusGained,BufEnter * call UpdateBackground()
augroup END

Show thread

We’ll use autocommands, which fire automatically when a certain trigger happens. In our case, we’ll call our function when Vim gains focus (`FocusGained`) or when entering a buffer (`BufEnter`):

:autocmd FocusGained,BufEnter * call UpdateBackground()

Show thread

Let’s try our function. To load our file, call `:source %` (`%` is a shortcut for the current file).

Then, we can switch the background by running `:call UpdateBackground()`. It should switch to match macOS’s background mode. 🎉

Show thread

In nightfall.vim, we'll define a function:

function UpdateBackground()
if system("defaults read -g AppleInterfaceStyle") == "Dark\n"
if &bg == "light" | set bg=dark | endif
else
if &bg == "dark" | set bg=light | endif
endif
endfunction

Show thread

With our prototype done, let’s build that plugin. After settling on a name, we’ll create a directory and a file to store our plugin in.

$ mkdir -p ~/.vim/pack/plugins/start/vim-nightfall/plugin
$ nvim ~/.vim/pack/plugins/start/vim-nightfall/plugin/nightfall.vim

Show thread

Combining these, we can set our background color based on the `AppleInterfaceStyle`:

:if system("defaults read -g AppleInterfaceStyle") == "Dark\n"
: if &bg == "light" | set bg=dark | endif
:else
: if &bg == "dark" | set bg=light | endif
:endif

Show thread

We can use an if-statement to set `bg=dark` if it’s currently “light”, and vice versa:

:if &bg == "light" | set bg=dark | endif

:if &bg == "dark" | set bg=light | endif

Show thread

First things first. We need to know when to switch the background color. We’ll check `defaults read -g AppleInterfaceStyle`, which produces `Dark\n` when macOS’s dark mode is turned on.

In Vim, as a boolean:

:echom system("defaults read -g AppleInterfaceStyle") == "Dark\n"

Show thread

Alright, I’m writing and another Vim micro plugin to automatically switch between `bg=light` and `bg=dark` based on macOS’s dark mode.

Follow along for some live-micro-blogging! 🧵

twitter.com/jkreeftmeijer/stat

A way to do this would be adding an `autocmd` that calls out to `defaults read -g AppleInterfaceStyle` and updates the `bg` value if needed.

I might wrap that in a plugin if it doesn’t exist yet.

Show thread

Vim users, what’s your favorite plugin to automatically switch between `bg=dark` and `bg=light` based on macOS’s dark mode?

I’m not using MacVim, so I can’t rely on `OSAppearanceChanged`, sadly.

The bathroom has more cabinet space than we need. That’s convenient, as it allows us to not open the cabinet with the forgotten half set of dentures ever again. 😨

Show thread

Wait, does following somebody on Github mean you’ll get notifications whenever they push a commit? If that’s the case, sorry about github.com/jeffkreeftmeijer/up. 😬

@joerebelloharley They’re both bad options. I very much just wanted it to be quiet until somebody picked up, so I could do something else. 😔

Calling my ISP, they provide the option to _not_ listen to their waiting music. That sounds helpful, but if you choose it they’ll just keep you listening to a dial tone for fifteen minutes. 🤷‍♂️

We only use one of the bedrooms. One of the other ones is locked, as it’s filled with furniture that’s yet to be moved out. Same goes for the garage in the back yard, which is filled to the brim.

Show thread
Show older
Mastodon

Server run by the main developers of the project 🐘 It is not focused on any particular niche interest - everyone is welcome as long as you follow our code of conduct!