Using it’s all text on Mac OS X
Introduction
As a system administrator, I dislike to edit text in Firefox (or any other webbrowser), for example to write a new post on this blog, or edit some contents in drupal. This morning, I was looking for a Firefox extension allowing to use an external editor (in my case vim, for sure) to edit contents of textarea. This extension is pretty simple, you only need to configure which command to run to edit the file.
However there is a big trouble with Mac OS X, indeed, I was not able to find a command line to open a new terminal tab and lauching vim with the file to edit. Indeed, something like:
% open -a iTerm /usr/bin/vim
is working fine, however, it’s not possible to do something like:
% open -a iTerm /usr/bin/vim -- /tmp/file_to_edit
That’s very inconvenient, isn’t it! So, the only way I found to achieve that is to write an AppleScript to open a new iTerm, using vim profile (which runs vim as startup), and then open the file by sending text to vim from the AppleScript. But, once gain, I run out of luck. Indeed, the filename computed by it’s all text contains spaces, and vim expect escaped spaces. So, I look for a way to replace string in AppleScript, but… guess what? it’s seem very difficult (because I need to create another file..). So the only simple solution I found is to create a shell script that create a symbolic link in /tmp to the file to edit.. Here we go!
Configuration
iTerm
Open your bookmarks manager, and create a new bookmark like
that:

Open a terminal, create the directory ~/bin, create the file
editfile with the following contents (replace bbonfils
with your login name):
#!/bin/zsh extension=${1:e} link="/tmp/firefox."$$".${extension}" ln -s "$1" $link /Users/bbonfils/bin/editfile.scpt $link
Create a new file named editfile.scpt with the following contents:
#!/usr/bin/osascript
on run argv
tell application "iTerm"
activate
make new terminal
tell the last terminal
launch session "vim"
tell the last session
write text "^[[:e " & item 1 of argv
end tell
end tell
end tell
end run
Ensure both are executable chmod +x, and then configure it’s all text
to use the first script to open file (/User/bbonfils/bin/editfile), and
now it should work!
The last word
Note I can’t remove the link in the script, since all of them are executed in background,
if you add a rm in the think, the link will be removed few seconds after you start vim,
so your textarea contents won’t be updated.
And yes, I know, it’s ugly, if you have a better way to achieve that, please post it in comments!
I am using my method to enter this comment, so it is definitely working:
(Adding all steps, even those I consider somewhat obvious)
1. Download the MacVim port from http://code.google.com/p/macvim/
2. extract the *.tbz
3. put the MacVim.app where you desire (e.g. /Applications)
4. put the mvim script where you desire (e.g /usr/bin) [helps to use a terminal here so you can verify ownership and permissions are sane]
5. recommended: symlink gvim to mvim if you are a multi-platform type
6. configure It’s All Text to use mvim (e.g. /usr/bin/mvim) as its editor.
7. party on…
Note, not quite as cool, but I use gvim with IAT! on my Linux and Windows installs, so at least I have a consistent experience…
Hello Kevin,
thanks for your reply! Indeed your method is working, however, since I use vim (inside iTerm+sceren) every days, I was looking for an exactly similar way. Following your method, I need to use ITerm+screen+vim (to edit local files) and MacVim (to edit textarea), which is an another application, meaning different environment, behavior, etc.
Fair enough. Different needs, different methods. But, since your blog was the among the only sources of info on this topic, I wanted to share an alternative.
Technically, I suppose you could swap out the “normal” vim for a link to /Applications/MacVim.app/Contents/MacOS/Vim and get a unified set of preferences, etc. regardless of if you were using console mode or “graphical” mode. (This would more closely resemble what I actually get on my Linux systems.)
And thanks for sharing!
How would you do this with the default Terminal.app on Mac OSX?
Thanks for info on this. I have been looking forever
on a way to do it. I wrote this article with it as well.
Here is the way I did it. Btw, no need for symlinks
because found away around from another article.
# is your username
$ mkdir /Users/somename/bin
$ create the two files below
$ chmod 755 /Users/somename/bin/emacs-terminal.scpt
$ chmod 755 /Users/somename/bin/editfile
$ Then edit prefs of ItsAllText and add the path:
/Users/somename/bin/editfile
# It will not be selectable, only .app files are.
Below are the files:
#!/usr/bin/osascript
# open Apple Script Editor and and save to this path:
# /Users/somename/bin/emacs-terminal.scpt
on run argv
tell application "Terminal"
set filename to item 1 of argv
set filename to do shell script ¬
"perl -e \"print quotemeta ('" & POSIX path of filename & "');\""
do script "emacs " & filename & "; exit"
end tell
end run
#!/bin/sh
# open your faviorite editor and save the file to:
# emacs /Users/somename/bin/editfile
# http://blog.asyd.net/2009/09/using-its-all-text-on-mac-os-x/
# http://www.macosxhints.com/article.php?story=20040617170055379
if [ -z $1 ]
then
echo "No filename was given as args."
exit;
fi
logger "ITS ALL TEXT: $1"
linkbase="/tmp/firefox-$$"
extension=${1##*.}
link=$linkbase.$extension
#ln -s "$1" $link
#exec /usr/bin/osascript /Users/sbradley/bin/emacs-terminal.scpt $link
exec /usr/bin/osascript /Users/sbradley/bin/emacs-terminal.scpt "$1"
It’s been a while since your original post so FWIW, I wrote this version:
http://horde.net/~jwm/software/misc/iterm-editor
which is self-contained (one file), honors standard shell EDITOR/VISUAL environment variables, and doesn’t require a separate iTerm profile.