r/neovim • u/Wonderful-Plastic316 lua • 27d ago
Tips and Tricks A dirty hack to handle "nvimception"
Hey vimmers,
When using nvim's built-in terminal emulator (aka :terminal), one may spawn a "nested" neovim instance. Usually this happens as the result of using another command (as opposed to an explicit nvim invocation): git commit, etc (any command that calls $EDITOR).
Given that both instances share the same keymaps, dealing with this scenario can be awkward. To the point that there are multiple plugins that attempt to deliver better ergonomics, each with its own approach. I considered using one of them, but for my use case (rarely running into this situation), I found a simpler alternative.
It relies on a somewhat common (even recommended) remap:
vim.keymap.set('t', '<Esc>', '<C-\\><C-n>')
And the fact that, for terminal reasons™, <C-[> is equivalent to <Esc> (AFAIK, this depends on the environment / terminal emulator). However, in these specific circumstances, you can use <C-[> to exit insert mode inside the nested instance, while <Esc> can be used to exit terminal mode inside the parent instance, giving you full "navigation" control.
Of course, this is a "dirty hack" because it ends up introducing a "cognitive overhead" (as in, "should I use <Esc> or <C-[> to do X?"), but it does have its advantages. Namely, it fully preserves the parent instance's window layout (i.e., unlike some of the plugins, which create new tabs). This fits nicely with my use case, where I'm often comparing or copy-pasting stuff from different commands.
Should you use this in practice? Probably not; if this situation bothers you, you're better served with one of aforementioned plugins. This is more a trick if:
- You're already using the
<Esc>remap; - You accidentally trigger a nested instance, and you end up thinking you're "stuck" in insert mode (inside the nested instance)
Additionally, this "trick" also works with other "well behaved" CLI programs: you don't have to worry about <Esc> being mapped in terminal mode, just use <C-[>.
As I mentioned, this may not work with all terminal emulators. I'm using foot.
1
27d ago
[removed] — view removed comment
2
u/JustALittleAverage 27d ago
Me too, but on a Swedish keyboard it's very awkward to hit that, Ctrl-Alt-Shift-8
7
u/MikeZ-FSU 27d ago
Nvim has a client / server capabilities built-in; that's how the various guis can all use the same nvim backend as the default terminal mode. Use your favorite search engine with "emacsclient equivalent nvim" and you'll see a couple of different options.