Skip to content

Instantly share code, notes, and snippets.

@napcs
Last active March 19, 2024 18:13
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save napcs/2d8376e941133ccfad63e33bf1b1b60c to your computer and use it in GitHub Desktop.
Save napcs/2d8376e941133ccfad63e33bf1b1b60c to your computer and use it in GitHub Desktop.
Open file in Terminal Vim on OSX
  1. Open Automator
  2. Choose "New Document"
  3. Locate "Run AppleScript" and double-click it
  4. Paste the Applescript code into the box
  5. Save as /Applications/TerminalVim.app
  6. In Finder, select some file you want to open in Vim, e.g. a .rb file.
  7. Hit ⌘I to open the “Get Info” window.
  8. Under “Open with:”, choose TerminalVim.app. You may need to select “Other…” and then browse.
  9. Hit the “Change All…” button and confirm.

Now all .rb files in Finder will open in Vim.

on run {input, parameters}
set filename to quoted form of POSIX path of input
set cmd to "clear;cd `dirname " & filename & "`;vim " & filename
tell application "iTerm"
tell the current window
create tab with default profile
tell the current session
write text cmd
end tell
end tell
end tell
end run
@bietsch
Copy link

bietsch commented Oct 13, 2019

Works like a charm. Being on a mac with all sorts of weird characters in filenames you might want to change
"set filename to POSIX path of input"
to
"set filename to quoted form of POSIX path of input"

@normen
Copy link

normen commented Apr 25, 2020

I took the idea further, now you can edit any text snippet (e.g. in Mail, Safari etc.) in MacOS with VIM:
https://github.com/normen/vim-macos-scripts

@kunalarya
Copy link

I ran into a problem with filenames containing spaces, and found the following helped:

set filename to quoted form of POSIX path of input

@nikell28
Copy link

nikell28 commented Mar 1, 2023

This option does not bring the terminal to the foreground if the stager is on ((

@Spirarel
Copy link

I took the idea further, now you can edit any text snippet (e.g. in Mail, Safari etc.) in MacOS with VIM:
https://github.com/normen/vim-macos-scripts

These are great and work on macOS 13. Signal boosting!

@wojciech-kulik
Copy link

wojciech-kulik commented Oct 14, 2023

Updated version with bringing terminal (iTerm2 + nvim) to the foreground:

on run {input, parameters}
	set filename to quoted form of POSIX path of input
	set cmd to "clear;cd `dirname " & filename & "`;nvim " & filename
	tell application "iTerm"
		tell the current window
			create tab with default profile
			tell the current session
				write text cmd
			end tell
		end tell
	end tell
	
	tell application "System Events"
		tell process "iTerm2"
			set frontmost to true
		end tell
	end tell
end run

@amomentunfolding
Copy link

Updated version with bringing terminal (iTerm2 + nvim) to the foreground:

on run {input, parameters}
	set filename to quoted form of POSIX path of input
	set cmd to "clear;cd `dirname " & filename & "`;nvim " & filename
	tell application "iTerm"
		tell the current window
			create tab with default profile
			tell the current session
				write text cmd
			end tell
		end tell
	end tell
	
	tell application "System Events"
		tell process "iTerm2"
			set frontmost to true
		end tell
	end tell
end run

works great, cheers!

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