February 17, 2005The old emacs/vi debateI'm a fan of both vi and emacs (and Eclipse too), which I still use on a daily basis. I've been using emacs for more than fifteen years so I know it pretty much inside and out and I fall back to it to edit anything that is not Java and more than a few dozen lines. This posting about vi reminded me that in terms of macros, emacs still has a formidable edge over vi (and pretty much any editor I can think of, actually). But instead of empty words, here is a concrete task I had to do recently. I have a bunch of HTML files named 100.html, 101.html... 199.html. They all contain something that looks like this: <span class="number"> I want to extract the content of the span tags and put them into a file in a canonical format (eventually a .ddl file for insertion in a database, but it doesn't really matter). Emacs allowed me to accomplish this task within a few minutes with macros. Can you think of another tool that will let you do that? (without writing a script, which takes more than a few minutes anyway). Posted by cedric at February 17, 2005 09:12 AM Comments
Code, post code ! :) Posted by: at February 17, 2005 09:52 AMI usually use perl on the command line (ie: perl -pi.bak -e 's/foo/bar/') for things like that. Although you are probably right, I'd end up scripting something that complicated. I tried hard to like Vi, but having to hit the Esc key all the time is an ergonomic nightmare; especially on a Kinesis Contour keyboard. I tried hard to like Emacs... It always took to long to load on 1990's hardware, and I have a bad habit of quitting an editor when I'm not using it. I also cannot stand LISP, and got sick of logging onto the #emacs IRC channel and begging for help everytime I wanted to change my config. Posted by: James A. Hillyerd at February 17, 2005 09:57 AMThere is no code, that's the point! I did it all by recording a macro in emacs and repeating it a hundred times. I'll post the solution soon along with an explanation of why there are cases where macros are more powerful than even the coolest script language... XSLT? Anyone? Posted by: RhesusJesus at February 17, 2005 10:39 AMVi works swell on a Kinesis Contour keyboard after you switch Caps Lock and Esc. I do not remember the last time I voluntarily used Caps Lock ;) Posted by: at February 17, 2005 10:48 AMYou can do that easily with jEdit. The nice thing is that macro recording creates a BeanShell script. So if you need to do stuff that cannot be recorded (like looping the macro until some condition matches), you can easily add it to the script. You get fast macro recording and easy macro customization. Real programmers know that the Caps Lock key should be remapped to Control, not Esc! :) Actually, before Eclipse I used to spend all day inside Emacs, but now I keep Eclipse open all day, and Emacs takes too long to open. I know about GNUserv, but for "quick edits" I now use Vim. Posted by: Daniel Serodio at February 17, 2005 11:46 AMHere is the BeanShell recored by jEdit. It search For each span it takes the text in quotes, append it to the Register $, then it take the text in the span tag and append it in Register $. // This is a recorded macro. First, check over the Oups... wrong macro... here is the full one... Sorry... it looks like I have to escape "<"... Just a moment... Here it is... Given that xsl is said to be lisp with brackets I'd say xsl is the same and doesn't require a particular editor. Posted by: Amir Brown at February 17, 2005 12:28 PMWell done, Emmanuel. That's the idea. I'm more interested in the keystrokes to define the macros than the source, though, but I definitely think it's the best way to do this kind of mass reformat. As for XSL... yuck :-) (especially if you are dealing with HTML, which might not be well-formed XML) Posted by: Cedric at February 17, 2005 12:58 PMYou can annoy everyone by using viper-mode in XEmacs. And mapping the Escape key to the End key under your left thumb really makes vi useful when you've got a Kinensis. Posted by: Bob McWhirter at February 17, 2005 01:09 PMBuffy Sommers? Dude... :) Posted by: Nico at February 17, 2005 01:34 PMYes, you could do this in Vim with a keystroke macro repeated 100 times: qa(keystrokes)q (Record a macro 'a') Depending what wanted to do (keystrokes) probably be a bunch for :s commands to modify the file to the canonical form, then write the file out to a new file with a different extension, then end with :n (Go to next file). Posted by: Alan Green at February 17, 2005 03:05 PMWhat Alan said. To extract your spans, the vi-speak would be (search for span to get it in the search buffer) There is a vi plugin for Eclipse which I find extremely useful: http://www.satokar.com/viplugin/ Posted by: Andrew Collins at February 17, 2005 09:54 PMOne tool I like to use for batch processing XML files is XMLStarlet (http://xmlstar.sourceforge.net/): For example, youcould extract the content of the <span> elements with the following command: :%!extractspans.rb :) Posted by: Charles Miller at February 17, 2005 11:33 PMI think Delphi IDE editor has quite powerful editor that can record macro... One aspect of macros you might want to mention is to use them only if there are a few invocations. Posted by: B Wheeler at February 18, 2005 05:30 AMNice one Cedric! I have been known to rope Emacs into "extreme" data munging on occasion - great for turning a CSV file or the like into a nice set of 10000 or so INSERT statements :) What I really like though is the way you can do this without context-switching: in Emacs you often lash out a whole set of keystrokes, stuff happens, and afterwards you can't actually say what you typed, but it just worked. Direct brain-to-text interface. Coding up a script to do the same thing just doesn't have the same flow. I know Eclipse is better for me, but somehow I remain stuck in Emacs-land, even for Java... PS: On my current machine Emacs (with JDE and a whole stack of extra modes) loads in about 2 secs, and Eclipse (standard, no plugins) in about 9! Posted by: Richard Rodger at February 18, 2005 05:31 AMI'd use Visual SlickEdit. It's easy to record macros of that kind, multi-file searches, outputing to a separate buffer, etc. Emacs never did it for me. I liked vi, and for quite a while I used Visual SlickEdit in vi emulation mode, but when Idea and Eclipse came around a few years ago, I got out of the vi habit. I mainly use Eclipse now, but I keep SlickEdit around for the fast and powerful searching, the top notch macro support, and the occasional hex editing of class files and other binaries. Posted by: Mocky Habeeb at February 18, 2005 05:56 AMOk, but now here's what I'd really like to see: recording macros that include java code refactorings. 1. Begin Recording Macro Cedric, key stroke to start recording : C-m C-r Then you just do the stuff you want to repeat. Then C-m C-s. Then C-s to save the macro. A way a bit more efficient (if you do not want to modify nor save the macro) is to use temporary macro
"Real programmers know that the Caps Lock key should be remapped to Control, not Esc! :)" Since this was a reply to the Kinesis Contour comment, I'd have to guess you've never seen a Kinesis Contour keyboard - the Ctrl key is directly under each thumb on this keyboard so there's no reason to remap it. Most people won't even give these keyboards a try since they look too alien and take a week or two to get used to but they absolutely rock once you've gotten used to them. My only complaint is that the curly braces are in the wrong place and I can't think of an appropriate location to remap them to... Posted by: at February 18, 2005 03:00 PMHi Emmanuel, The other way to optimize jedit load time is to use the -background flag during startup. I have a je.bat file which does something like below. After you close jedit using the CLOSE button(top right), you can recall jedit (with the je.bat ) shortcut or otherwise and it comes up (again) pretty fast. start javaw -Dcom.sun.management.jmxremote -Xms32M -Xmx128M -jar "C:\Program Fil James A. Hillyerd wrote: eeks!, no, no, not ctrl-c! If this ever changes due to unicode, Vi's interface and normal mode command set Post a comment
|