Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Use GUI editors for $VISUAL editing UNIX
While I'm quite at home in vi, these days I tend to keep it as my lowest common denominator tool. I can be sure it will be on any *nix machine, so I remember enough to do basic editing. I'm sure I've forgotten more vi commands than I remember. But on my own Mac, there are times when years of unix habits die hard, and I could be editing the file in a GUI editor, but it was easier to fire up vi -- like when viewing with less and hitting v. So why not set $VISUAL to a GUI app like TextWrangler?

Because it's not quite that straight forward. Firstly, you need to use open -a TextWrangler, but that returns immediately, throwing you back into less. Then you also have to allow for the way that less (and others) call $VISUAL. So instead, I set $VISUAL to point to a script, ~/bin/tw, which contains the following:
#!/bin/sh
for FILE in  "$@"  ; 
do
  if [ -f "$FILE" ] ; then
    open -a TextWrangler "$FILE" ;
  fi
done
while (ps -auxc | grep -v grep | grep -q TextWrangler)
do
  sleep 1 ;
done
Rather than mess with the $LESSEDIT variable, I just ignore files that don't exist, so that the line number passed by less is ignored (unless you like naming files something like "+16"). Now from less, when I hit v, TextWrangler opens with my file. As a bonus if I really want to get back to less, I can use Control-C to return to less with the editor still open.

[robg adds: I haven't tested this one...]
    •    
  • Currently 3.80 / 5
  You rated: 4 / 5 (5 votes cast)
 
[5,822 views]  

Use GUI editors for $VISUAL editing | 6 comments | Create New Account
Click here to return to the 'Use GUI editors for $VISUAL editing' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Use GUI editors for $VISUAL editing
Authored by: foilpan on Feb 11, '05 11:00:01AM

how about passing your text to see, SubEthaEdit's command line tool? i use this approach all the time.

does TextWrangler have similar functionality? BBEdit does, i believe.



[ Reply to This | # ]
Use GUI editors for $VISUAL editing
Authored by: durin on Feb 11, '05 11:18:24AM

Uh....look at the edit command line tool. It can optionally wait until the document you're editing is closed, you don't have to quit TW to be done.

from the manpage:
EDIT(1) TextWrangler Command Line Reference EDIT(1)



NAME
edit - (TextWrangler) Whip some ASCII.

.............
-w, --wait
Wait until the file is closed in TextWrangler. Normally, the edit
tool exits immediately after the file arguments are opened in Tex-
tWrangler. The -w option allows the edit tool to be used as an
external editor for Unix tools that use the EDITOR global environ-
ment variable. To make this work using tcsh, add the following
line to your .cshrc (or .tcshrc) file:

setenv EDITOR "edit -w"

Some tools (notably crontab), will not work correctly if your EDI-
TOR variable consists of multiple terms. You can work around this
by creating a simple shell script that calls edit -w, and then
using the shell script as your EDITOR. For example:

#!/bin/sh
edit -w "$@"

---
Go not to the elves for council, they will say both no and yes



[ Reply to This | # ]
Use GUI editors for $VISUAL editing
Authored by: u2mr2os2 on Feb 12, '05 11:28:45AM

TextWrangler (v2.0 at least) handles the "+linenumber" command line option, so I just set my VISUAL variable to "edit -w", and it works just fine. Unfortunately, SubEthaEdit does not handle the "+linenumber" option.



[ Reply to This | # ]
Use GUI editors for $VISUAL editing
Authored by: AJB on Feb 12, '05 04:17:10PM

Hint submitter here.

Thanks for the tips about the edit command line tool. I missed it, since I'v only just downloaded it, and obviously not read the documentation cover to cover!

Your suggestions of "edit -w" won't help for other apps however.

The main point in submitting this hint, is that it can be adapted to your GUI tool of choice. I happened to pick TextWrangler because and I wanted to try it out.



[ Reply to This | # ]
Use GUI editors for $VISUAL editing
Authored by: gshenaut on Feb 13, '05 08:31:57PM
I'm concerned about the wait loop using ps(1). First, I think you might want not to include the flags -a and -u, because -a includes commands being run by all users, and you probably just want your commands, and because the extra text produced by -u doesn't seem relevant to this task. Second, use of ps and grep seems like overkill. You could use something like this to get the pid of the process
ps xc | grep "Program Name" | head -1 | read thePid junque
and then in your loop, use
while kill -0 $thePid 2>/dev/null ; do
...
This would seem to be less resource intensive. Or, a simple loop using
while killall -0 "Program Name" 2>/dev/null ; do
...
would be an improvement, but killall(1) uses more resources than kill(1). Just a thought. It's too bad there isn't a way to make GUI programs synchronous.

Greg Shenaut

[ Reply to This | # ]

A more general approach
Authored by: lee1 on Feb 14, '05 11:05:03PM

You've gone to a certain effort to solve the problem that you caused by using "open -a" rather than simply invoking the executable file in the bundle. For example, if you wanted to use TextEdit as your editor, you could define VISUAL to be


/Applications/TextEdit.app/Contents/MacOS/TextEdit

When a program shells out to this a new TextEdit process will start, which you quit when you are done, just as you are used to from older unix days. It just won't understand the line number argument. I happen to be using "see", as another poster mentioned too, which opens a SubEthaEdit window, but I used to do it the way described above.



[ Reply to This | # ]