index.md (3800B)
1 --- 2 title: "2024, Day 9: Figuring out a new $EDITOR" 3 description: "A problem statement and some notes" 4 date: 2024-12-10T15:00:23-08:00 5 draft: false 6 --- 7 8 I’m writing this on December 10, but I’m considering this my update for day 9 9; the notes I’m sharing here mostly come from yesterday. 10 11 ## The problem 12 13 I’ve been growing more comfortable using Neovim’s own built-in terminal 14 emulator, usually in its own tab or a split pane. Remembering to get into, 15 and how to get out of, “terminal mode” took some getting used to, but now 16 that I’m there, I’m finding it more convenient than running everything 17 inside [tmux](https://github.com/tmux/tmux/wiki/Getting-Started)[^tmux]. 18 19 But I don’t like what happens when I run a command that launches my 20 editor---such as `git commit`---from a Neovim terminal: I wind up with a new 21 Neovim session inside my Neovim session. My desired behavior would be to 22 either open a new tab page in the same session with the document to be 23 edited, or open a buffer in the same window that returns to the terminal 24 when closed. 25 26 ## Toward a solution 27 28 I don’t have all the details ironed out yet, but running the following 29 command does what I want when run from Neovim’s teriminal: 30 31 ```bash 32 nvim --server $NVIM --remote-tab {files} 33 ``` 34 35 This crashes when it’s called without a file, but this is close to my 36 desired behavior for no file: 37 38 ```bash 39 nvim --server $NVIM --remote-send "<C-\><C-n>:tabnew<CR>" 40 ``` 41 42 This doesn’t apply to `git commit`, etc., but it’s worth noting. 43 44 Inside a Neovim terminal session, the `NVIM` environment variable is set to 45 the path of a socket; it is unset otherwise. 46 47 There’s a program, `nvr`, provided by 48 [neovim-remote](https://github.com/mhinz/neovim-remote), which simplifies 49 a lot of this. I’m hesitant to use that because it’s a `pip` install with 50 a bunch of dependencies, and I hate putting whole applications on my system 51 that way. That may be an idiosyncratic and unfair hangup, but it’s _my_ 52 #DecemberAdventure. 53 54 I’ve confirmed that I can’t define a function in my `.zshrc` and use that 55 for an `EDITOR`, so I’m probably looking at a shell script. I should 56 probably avoid the temptation to try to make it handle every corner case, 57 and write a script that covers 99% of my use cases, which look like this: 58 59 - Invoked outside of a Neovim terminal with zero or one files specified 60 - Invoked inside a Neovim terminal with zero or one files specified 61 62 Being able to handle multiple files, capturing other command line options, 63 or dealing with other obscure things that Vim (etc.) can do (Ex commands?) 64 probably don’t need to be dealt with by my simple shell script EDITOR 65 replacement. 66 67 I think the following behavior would be a perfectly reasonable MVP: 68 69 - If `NVIM` is set and the script was called with zero arguments, run: \ 70 `nvim --server $NVIM --remote-send "<C-\><C-n>:tabnew<CR>"` 71 - If `NVIM` is set and the script was called with one argument that does 72 not begin with `-`, run: \ 73 `nvim --server $NVIM --remote-tab <arg>` 74 - In all other cases, just call `nvim` with whatever arguments were passed 75 to the script 76 77 Now I just need to code it up and test it with `git commit` specifically. 78 I _suspect_ that I’m going to have to deal with whether or not the command 79 returns immediately, but I haven’t gotten that far in my testing yet. 80 81 [^tmux]: tmux is great, but do you start _every_ Vim session inside of 82 it---even on your local machine? If not, what do you do when you realize 83 “oh I need to do something in the terminal real quick”? I’m guessing 84 that most folks open a new tab or window in the desktop’s terminal 85 emulator here (becasue that’s what I did) but it’s nice to stay entirely 86 in one application (IMHO) and have Vim keybindings for _everything_.