Skip to content

Instantly share code, notes, and snippets.

@brablc
Last active December 24, 2016 01:30
Show Gist options
  • Save brablc/b2f40c2f652e420484d2 to your computer and use it in GitHub Desktop.
Save brablc/b2f40c2f652e420484d2 to your computer and use it in GitHub Desktop.
Open Vim in Terminal (iTerm) from Finder - copied from http://thepugautomatic.com/2015/02/open-in-iterm-vim-from-finder/ and updated to open in the current terminal
on run {input, parameters}
set cmd to "vim -c startinsert"
if input is not in {} then
set myPath to POSIX path of input
set cmd to "vim " & quote & myPath & quote
end if
tell application "iTerm"
activate
set myTerm to (current terminal)
tell myTerm
set mySession to (make new session at the end of sessions)
tell mySession to exec command cmd
end tell
end tell
end run
@karuturi
Copy link

hi @brablc, I get the following error when I open a file using this. (OSX 10.9.5)

The action "Run AppleScript" encountered an error.
Check the actions properties and try running the workflow again.

@mstroeck
Copy link

This crashes for me if iTerm is not currently running, or it's running but has no windows open. I modified it to work for me on Mac OS X El Capitan:

on run {input, parameters}
    set cmd to "vim -c startinsert"
    if input is not in {} then
        set myPath to POSIX path of input
        set cmd to "vim " & quote & myPath & quote
    end if

    tell application "iTerm"
        activate
        set myTerm to (current terminal)
        try
            tell myTerm
                set mySession to (make new session at the end of sessions)
                tell mySession to exec command cmd
            end tell
        on error
            set myTerm to (make new terminal)
            tell myTerm
                set mySession to (make new session at the end of sessions)
                tell mySession to exec command cmd
            end tell
        end try
    end tell
end run

@thewinger
Copy link

@mstroeck Is it necessary to update something from the script?

I've tried it but automator shows me an error

screen shot 2016-11-21 at 9 10 49

@charlietran
Copy link

@chaadow
Copy link

chaadow commented Dec 23, 2016

@charlietran Thank you very much, I commented on your gist for some suggestions :)

@chaadow
Copy link

chaadow commented Dec 24, 2016

Ok guys I took the challenge as well and here is my gist : https://gist.github.com/chaadow/5e8ed49c333008eec0c3ced07451ee48
It works for El Capitan.

It handles 2 scenarios basically :

  • When Iterm is not open :
    • It doesn't create a second unnecessary window, since it knows that it's startup
    • It tells the current session to run vim with the appropriate POSIX path
  • When iTerm is open :
    • If there are no windows open ( This is the case when you have closed all windows but iTerm is still open, kinda frequent for me)
      • It creates a window with the default profile
      • It runs the vim command
    • If there is at least one window open
      • It creates a new tab so that it doesn't override the current tab of the window
      • It runs the vim command

Even though the script feels repetitive, this is the only and simplest way I found to handle most of the edge cases.
Alternatively, according to the iterm documentation, there is a is at shell prompt option, that allows to know if the current session if "free", so we can run the vim command without the need of creating a new tab each time.
However it requires installing the shell integration utiliites inside of iTerm, and this is not a "simple solution".
Cheers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment