Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

1 total

Open existing vim in iTerm

// open existing vim in iTerm
//
// experimenting with something like this
// as a modification to RunVim
//
// http://www.fastnlight.com/runvim/

tell application "iTerm"
	activate
	set _term_list to every terminal
	repeat with _term in _term_list
		tell _term
			try
				select session "Vim"
				select _term
				set _vim_tty to tty of session "Vim"
				try --check to see if vim is even running
					do shell script "ps -t " & _vim_tty & " | grep vim"
					
					try --check to see if vim is foreground
						do shell script "ps -t " & _vim_tty & " -o command,state | grep vim.*+"
					on error --vim is suspended or something
						--check for vim as job %+
						tell i term application "System Events"
							key code 6 using control down
							delay 0.1
							key code 14 using control down
							key code 32 using control down
						end tell
						tell session "Vim"
							write text "jobs | grep ]+.*vim"
						end tell
						delay 0.1
						set fulltext to text of session "Vim"
						set fulltext to quoted form of fulltext
						set lastline to do shell script "echo " & fulltext & " | tail -n 2 | head -n 1"
						set lastline to quoted form of lastline
						try --check %+
							do shell script "echo " & lastline & " | grep -v jobs"
							tell session "Vim"
								write text "fg %+"
							end tell
						on error --vim is not %+ job
							
							--check to see if vim is %- job
							tell session "Vim"
								write text "jobs | grep ]-.*vim"
							end tell
							delay 0.1
							set fulltext to text of session "Vim"
							set fulltext to quoted form of fulltext
							set lastline to do shell script "echo " & fulltext & " | tail -n 2 | head -n 1"
							set lastline to quoted form of lastline
							try --check %-
								do shell script "echo " & lastline & " | grep -v jobs"
								tell session "Vim"
									write text "fg %-"
								end tell
							on error --vim is not %- job; just pick the first one in jobs list
								
								tell session "Vim"
									write text "jobs | grep -m 1 vim"
								end tell
								delay 0.1
								set fulltext to text of session "Vim"
								set fulltext to quoted form of fulltext
								set lastline to do shell script "echo " & fulltext & " | tail -n 2 | head -n 1"
								set lastline to quoted form of lastline
								set jobnumber to do shell script "echo " & lastline & " | sed 's/[^0-9]//g'"
								tell session "Vim"
									write text "fg " & jobnumber
								end tell
								
							end try --%-
						end try --%+
						
					end try --vim foreground
					-- escape in case Vim was not in command mode
					tell i term application "System Events"
						key code 53
					end tell
					tell session "Vim"
						write text ":enew"
					end tell
				on error -- vim not running
					tell i term application "System Events"
						key code 6 using control down
						delay 0.1
						key code 14 using control down
						key code 32 using control down
					end tell
					tell session "Vim"
						write text "vim; exit"
					end tell
				end try
				exit repeat
			end try
		end tell
	end repeat
	tell _term
		try
			select session "Vim"
		on error --no existing Vim session
			launch session "Default Session"
			set name of the last session to "Vim"
			tell the last session
				write text "vim; exit"
			end tell
		end try
	end tell
end tell
1 total