Home > Misc > Using it’s all text on Mac OS X

Using it’s all text on Mac OS X

September 15th, 2009 Leave a comment Go to comments

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:

Image 8

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!

Categories: Misc Tags: ,
  1. Kevin Guinn
    September 22nd, 2009 at 22:34 | #1

    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…

  2. September 22nd, 2009 at 22:44 | #2

    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.

  3. Kevin Guinn
    September 22nd, 2009 at 23:45 | #3

    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.)

  4. September 23rd, 2009 at 12:15 | #4

    Kevin Guinn :
    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.

    And thanks for sharing! 🙂

  5. jolleyjoe
    October 19th, 2009 at 21:26 | #5

    How would you do this with the default Terminal.app on Mac OSX? 🙂

  6. shane
    October 23rd, 2009 at 22:45 | #6

    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"

  7. John Morrissey
    January 28th, 2010 at 18:13 | #7

    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.

  8. Franklin
    September 27th, 2010 at 14:47 | #8

    I wanted to be able to open emacs in a new terminal window by just typing “emacs file” inside of the terminal in osx. It is surprisingly hard to find the answer to this issue, so I thought I would post my solution here (based on the code above). This code will open any files on the argument line and will close the terminal window on completion.

    Put the following in runEmacs.scpt
    on run this_file
    tell application “Terminal”
    do script “emacs ” & this_file & “; exit”
    end tell
    end run

    Put this in your bash shell
    emacs() {
    osascript /your/path/runOpen.scpt $*
    }

    Make sure to change the terminal preferences in shell to automatically close after a clean exit.

  9. January 25th, 2011 at 11:35 | #9

    To get this to work I had to edit the Firefox config manually because the preferences window would not let me select the mvim executable:

    1. Enter “about:config in the address bar
    2. In the filter box type ‘alltext’
    3. Find the key ‘extensions.itsalltext.editor’
    4. Set its value to ‘/usr/local/bin/mvim’ or whatever other program you prefer.

  10. RDM
    February 19th, 2013 at 06:27 | #10

    @shane

    Amazing comment and thanks so much (to authors and comments)! I was wondering if I would find a way to have just a terminal open with my text editor to use “It’s All Text”
    The only thing I did was change “emacs” to “vim” and I am completely satisfied! Looking forward to doing all my wiki editing in vim now!!!

    (edited in vim in terminal!!!)

  11. Johnathon Racecar
    April 23rd, 2013 at 23:40 | #11

    I always have mvim running — so I would like to have my firefox edits to just use this current mvim session. To do this in vim, you can use the –remote-silent input argument.

    So, I wrote a open_mvim.sh file that looks like:

    #!/bin/bash
    set -eu
    exec /usr/local/bin/mvim –remote-silent “$@”

    It works like a charm… no apple script — very little complexity. Make your own open_mvim.sh file and have your it’s alltext configuration point to it.

  1. No trackbacks yet.