DasGenie: !Scrap
« "Our Leader"? | Main | SubEthaEdit Conference Kit »

Samstag, 27. November 2004

see command line tool xtreme

A wonderful thing about SubEthaEdit 2.1.1 is the see command line tool.
If your living a lot in the terminal then the first thing you might want to do is set the EDITOR environment variable to 'see -rw', in bash this would be done by adding a

export EDITOR='see -rw'
to your ~/.bash_profile. Now everytime a standard unix tool wants you to edit something, the file is opened in SubEthaEdit and the tool will wait until you close the window before it will proceed. -w, --wait is for the waiting, -r, --resume will bring the Terminal.app to in front again after you close the window.
Another interesting switch is the -j, --job-description option. What it does is it puts the following string in the corresponding see window title. E.g. in my ~/.subversion/config there is:
[helpers]
editor-cmd = see -wr -j "SVN CheckIn"
so my svn checkins look like this:
wc/trunk/svn-commit.tmp [SVN Check In]

Now let's go to the more elaborate things. I bet many of you know and love the find command, especially combined with the xargs command it can be extremely useful. A typical example for find is finding and deleting files you don't like. E.g. the dreadful Thumbs.db you get from your windows users when you get images from them. So a simple:
find . -name "Thumbs.db" -delete
gets rid of them. Thats all quite nice, if you know exactly what your searching and doing, cause otherwise you either delete accidently, or get prompted on every file. So what if you want to delete many files but maybe not all of them? You use see to have a semi-automatic workflow.
find . -name "*.jpg" | see -r -t "Delete Files" | tr '\n' '\0' | xargs -0 rm
Now you can have a look at all the filenames, delete the filenames you wish to keep out of the document, close it, and deleting begins. Be cautious, when piping out, closing the window does not cancel the outer action. To cancel the deletion you have to control-c in the corresponding terminal. The extra | tr '\n' '\0' | xargs -0 ensures that spaces as well as quotes in filenames work
I used this cause I wanted to delete all thumbnails out of a directory structure. Problem was the thumbnails were named uniquely so you could tell it was a thumbnail by its name, but no simple regex would match all variations. Another way of achieving this would have been to pipe the find into see, blockedit an rm in every line and save it as an script.
Another similar one, measure the size of a hand selected number of photoshop files in the current directory and below:
find . -name "*.psd" | see -r -t "Count" | tr '\n' '\0' | xargs -0 du -hc
Or a more programming oriented variant, measure the lines of code of your project, removing third party stuff by hand:
find . -name "*.[hmc]" | see -r -t "Lines Of Code" | tr '\n' '\0' | xargs -0 wc -l
I think you get the idea...
And now going into recursion... kind of. Open a selection of txt-files in see:
find . -name "*.txt" | see -r -t "Open in SubEthaEdit" | tr '\n' '\0' | xargs -0 see
BunkBlog even used the piping to do remote editing via ssh. nice.

So what do you do with the command line tool?
21:56 - Samstag, 27. November 2004
Comments

Funny you're blogging this, cause I wrote a helper script aptly named `rsee` that somewhat non-interactively lets you retrieve files from a remote machine via SSH, and open them in SEE.

Current version can be found at http://www.alurio.org/svn/alurio/tools/rsee :-)

It's kinda buggy but hey, i'm a n00b, and it works well enough for my needs for now.

Posted by: Soeren Kuklau at 27.11.04 22:15

These are some nice tricks, but they got me thinking.

I already have a function ff () { find . -iname "*$@*"; }, as well as a number of other list generating functions (like url extraction using lynx). What I want see to do is *filter* my content.

After a little playing around, I've defined a new function see_filter () { see -r -t "$1" | tr '\n' '\0' | xargs -0 $@; } and also a corresponding alias -g seef = '| see_filter' (zsh feature).

Now I can type ff jpg seef rm, and that corresponds to your jpeg removing command.

But it gets better. I can define function geturls () { lynx -dump "$1" | awk '{print $2}' | egrep "$2" } (that one requires lynx to be installed) and then type geturls 'http://...' seef wget for some quick page leeching.

Thanks for the great idea!

Posted by: aether at 11.12.04 11:25