I prefer using Vim in a terminal. I can switch to the shell with Ctrl-Z and back with fg. However it is hard to integrate a terminal command with other GUI tools, such as editing a file in Vim from Finder.

In macOS, the default Terminal app and iTerm both supports automation. It’s easy to write a script to open a new terminal window and run a command like “vim file” in it. But I want to close the window after Vim quits. A quick work around is running the following command:

vim file; exit 0

Now the problem is that if I suspend Vim via Ctrl-Z, the terminal window is closed, because shell will continue to execute the next command when the process is suspended.

After research and reading a StackOverflow answer, I wrote two scripts to launch Vim in iTerm. I also added features in iterm-vim-wrapper to edit clipboard and empty scratch file in temporary directory.

This is an example which uses the scripts to create File Action in Alfred.