NeoVim everywhere on MacOS

To those of us who have embraced editing text in vim, working on anything longer than a sentence outside of vim becomes rather frustrating. This means we’ll often open up vim, write our thing in there, and paste it back into the original text control.

But there are better ways. After much trial and error — and with the help of various findings on Google — I’ve created a couple of Automator scripts to make working with vim on MacOS less chafing. The following assumes NeoVim and iTerm2, but porting to regular vim and Terminal.app should be fairly simple.

Firstly, opening files. If you’re using vim in the terminal, this requires us to open the terminal and then point vim at a file. But we should be able to launch vim with a single shortcut, and open files in it from Finder.

Create a new app in Automator, add a “Run Applescript” action, and paste the following script.

NeoVim.app

on run {input, parameters}
  set cmd to "nvim"
  if input is not {} then
    set filePath to POSIX path of input
    set cmd to "nvim \"" & filePath & "\""
  end if
    
  tell application "iTerm"
    create window with default profile
    tell the current window
      tell the current session to write text cmd
    end tell
  end tell
end run

Next up, let’s say you’re writing a long e-mail in Mail.app and would prefer to edit it in vim. For this, we have the following two scripts, created as actions in Automator.

Edit in NeoVim

This workflow takes text in and spits text out. In other words, you need to select some text to edit in vim, run the action, and the text will be replaced with whatever you saved in vim.

on run {input, parameters}
  set tempfile to do shell script "mktemp -t edit-in-vim"

  tell application "iTerm"
    create window with default profile
      tell the current window
        tell the current session
          write text "cat <<EOF > \"" & tempfile & "\"\n" & input & "\nEOF"
          write text "nvim \"" & tempfile & "\""

          repeat while name contains "Shell" or name contains "nvim"
            delay 0.5
          end repeat

          set content to do shell script "cat \"" & tempfile & "\""
        end tell

        close
      end tell
  end tell

  return content
end run

Write in NeoVim

This workflow is much like the last, but allows us to launch vim with the current context without requiring text to be selected. So if you’ve just opened a new e-mail dialogue, run this action and start writing!

on run {input, parameters}
  set tempfile to do shell script "mktemp -t write-in-vim"
  tell application "iTerm"
    create window with default profile
      tell the current window
        tell the current session
          write text "nvim \"" & tempfile & "\""

          repeat while name contains "Shell" or name contains "nvim"
            delay 0.5
          end repeat

          set content to do shell script "cat \"" & tempfile & "\""
        end tell
      close
    end tell
  end tell

  return content
end run

Bonus

These work great with the touchbar!

NeoVim on the touchbar