Streamline Opening Neovim from the Shell
The mvim
script (included with MacVim) is useful for launching the MacVim app from the shell. Sure, you can run Neovim from the shell with nvim
. but I want it to launch my instance of Neovim running in Alacritty.
This requires running nvim with --listen
as described in the linked post above.
I use fish shell so I have defined this in ~/.config/fish/functions/v.fish
:
function v
if count $argv > /dev/null
set -l filename $argv[1]
# Expand the filename to its absolute path
set -l absolute_path (realpath $filename)
#echo "Arguments provided: $argv"
#echo "Absolute path: $absolute_path"
nvim --server ~/nvim-server.pipe --remote-send "<ESC>:tabe $absolute_path<CR>"`
open -a Alacritty
else
set tempfile (mktemp)
#echo "file: $tempfile"
ansifilter > $tempfile
nvim --server ~/nvim-server.pipe --remote-send "<ESC>:tabe $tempfile<CR>"`
open -a Alacritty
end
end
v
accepts 2 types of input:
Filename as an arg
It lets me open a file in Neovim:
v some-file-name-to-open
STDIN automatically written to a temporary file and opening that file
Pipe some contents to Neovim:
cat some-file-with-contents | v
Pipe some contents to Neovim again:
echo "hello world" | v
Piping is useful because I can do:
$ curl https://dog.ceo/api/breeds/image/random | json_pp | v
and get the output:
{
"message" : "https://images.dog.ceo/breeds/mountain-bernese/n02107683_4907.jpg",
"status" : "success"
}
This would be similar to piping it to nvim -
, but again, using the v
script handles STDIN automatically (so you don’t need -
) and it writes the contents to a temporary file and open it in my preferred Neovim instance.