Streamline Opening Neovim from the Shell

Boon aka Hwee-Boon Yar February 26, 2024
Source

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]({% post_url 2024-02-17-double-click-file-in-macos-to-open-in-neovim %}).

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 ":tabe $absolute_path" 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.

See also: Fixing gx for Neovim with a Ruby Script.

Discussion in the ATmosphere

Loading comments...