Skip to content

Instantly share code, notes, and snippets.

@sdondley
Last active August 2, 2023 19:18
Show Gist options
  • Save sdondley/57972e3a507f7ec85d908f03fd2b6823 to your computer and use it in GitHub Desktop.
Save sdondley/57972e3a507f7ec85d908f03fd2b6823 to your computer and use it in GitHub Desktop.
Resarting/reloading neovim without quitting (sort of)
credit: chatgpt came up with this recipe for me
**The problem:** Modern package managers like lazy vim require you to restart neovim before any plugin changes can take effect. Doing `:wq, up arrow, enter` to reload neovim gets old quick when you are developing.
**Solution:**
First, write a bash script that automatically restarts vim if it exits with a specific exit code. Start all your vim sessions with this script:
```
#!/bin/bash
while true; do
/usr/bin/nvim "$@" # change path to real nvim binary as necessary
if [ $? -ne 1 ]; then
break
fi
done
```
You can drop this script into your ~/bin and call it nvim. Just make sure to make it executable and that ~/bin before the path the to real nvim binary in `$PATH`
Next, write a simple keymap that quits neovim and sends the exit code back the script that started neovim.
vim.api.nvim_set_keymap('n', '<leader>rr', ':lua os.exit(1)<CR>', { noremap = true, silent = true })
If you set everything up right, you'll see a brief flash after run the keymap above and the magically the file will reopen for you. For the best experience, make sure you use plugins that save your last cursor position when you closed so you can pick up right where you left off.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment