Scripting tmux with Shortcuts
I’ve been using Vim (and now neovim) for as long as I can remember. It hasn’t been my main development environment for a while, unfortunately, but it’s my go-to editor for many text processing tasks that I have encountered. 95% of the time I use vim in the terminal along with tmux.
The Problem
On macOS, I have a second GUI version of vim installed (MacVim/VimR) that serves one purpose: it allows me to drag files onto the Dock icon and quickly open them in the editor. I find this approach much faster and more convenient than going through the terminal (especially if my current work happens outside the terminal). Luckily, both vim installations share the same configuration files and plugins. There hasn’t been much overhead in keeping two versions around.
However, recently VimR has been a bit slow adopting newer versions of neovim. Homebrew on the other hand (where I get the terminal neovim from), is usually quick with supplying updates. Using two different vim releases hasn’t caused any major issues yet.
However, there are some potential issues with this approach: It’s probably just a matter of time until plug-in and configuration issues will pop up. In fact, I’ve already seen some treesitter issues that I believe are caused by the version mismatch. Also, I don’t want the convenience of drag&drop potentially getting in the way of using new neovim features.
The Solution
How can I keep the convenience and speed of dragging and dropping a file on a Dock icon while only having neovim available on the terminal?
There are probably many ways to accomplish this but one solution that worked well for me is the combination of Apple’s Shortcuts app and tmux. The Shortcuts app has a whole “ecosystem” of scripting tools, including running shell commands. On the other side, the tmux server can easily be scripted and be told to create a new window and open the passed in file in neovim.
It turns out to be super easy to build a shortcut that does exactly what I need. It can even bring the Terminal app in the foreground.
Step-by-step
- Create a new Shortcut
- Add the “File” action
- Right click on the “File” parameter and select “Shortcut input”. This allows the shortcut to receive a file that’s been dragged onto the shortcut
- Add a “Run Shell Script” action
- Change the command to
tmux new-window nvim ''
. This tells tmux to create a new window that runs nvim. - Between the single quotes, right click and select “Insert Variable” > “File”. In the popup, select “File Path”
- Remove the “File” variable in the “Input” parameter
- Add the “Open App” action and select “Terminal” as the “App” parameter
- Name your shortcut and close it
- In the Gallery, right click on your shortcut and select “Add to Dock”
That’s it! If this is your first shortcut using the “Run Shell Script”, you also need to enable “Allow Running Scripts” in the Shortcuts Settings. This is how your shortcut should look like:
Any file you drag onto the shortcut Dock icon will open in neovim inside the Terminal app. 🥳
It is important to note that the tmux server must be running in the background when the shortcut is executed. This is usually the case as soon as you create the first session. The Terminal app does not have to run for this shortcut to work.
Enjoy!