Quantcast
Channel: CodeSection,代码区,Linux操作系统:Ubuntu_Centos_Debian - CodeSec
Viewing all articles
Browse latest Browse all 11063

Stupid Emacs Tricks

$
0
0

Stupid Emacs Tricks
Next to vi, Emacs is one of the oldest text editors out there, and yet it’s still as popular in 2016 as ever. Once it was derided as having a gross memory footprint, but now even a tablet can run it. In the meantime, it still has a whole pseudo-operating system and programming language built in, which raises the question: “Is there anything it can’t do?” Let’s find out…

Emacs versus vi (and its successor Vim)is a geek culture war as old as the Internet . Well, we’re not here to take sides in that war. But it’s hard to offer a balancing post on the vi editor and its derivatives, because there’s not much to talk about in vi. Which is exactly the point; Emacs is the text editor with an operating system built in (some would say an operating system with an editor built in), so there’s no end of Emacs stuff to talk about.

Origin story

The dominant editor around the time of the first Emacs was TECO, which stood for Tape Editor and Corrector. Editing programs in this early ’70s era suffered from the adaptation pains of moving from medium to medium; the norm for data storage was paper tape, magnetic tape, punched cards, and the like. It was only after computers developed the capacity to store large volumes of data on a hard disk drive that the idea of editing a file electronically (as opposed to mechanically) took off.

So we all know Richard Stallman , who was bopping around the Stanford AI Lab in the 1970s and got inspired to create his own editor program based on macros created for dealing with TECO. Guy Steele, whose name graces the language Steele Common Lisp, collaborated with Stallman to produce the first Emacs, which became the standard editor by the end of the 1970s. GNU Emacs was begat from this project by Stallman around 1984, and a port to the X windows system, dubbed XEmacs, was developed by Jamie Zawinski in 1991.

Since then, Emacs spawned countless ports and derivatives, and quite a lot of cultural goofiness. There is, after all, a church devoted to it, and how many editors can say the same? It’s also still relevant enough to be the butt of an XKCD comic . Part of Emacs’ early lore was that it, being a program that included everything but the kitchen sink, was a huge, bloated memory hog. But the years have been kinder to Emacs, since computers have now gotten powerful enough that Emacs has a smaller memory footprint than your average tablet app. It would be easy to port, if it weren’t for the fact that it demands an IBM-PC sized keyboard and about thirteen fingers at a time to operate.

The Swiss Army Chainsaw

Emacs owes its legendary multitude of features to the built-in Lisp language implementation. And yes, Lisp also gets its own XKCD cartoon . The Emacs version of Lisp is ELisp, and while it’s not one-to-one compatible with other Lisp implementations, it’s still familiar enough to fit most needs. Since having a built-in scripting language is a great enabler, there’s an archive of packages of Lisp scripts out there to use for anything from implementing a Ruby-on-Rails IDE to playing games.

Probably the most essential trick every Emacs user should know is how to implement your own macro. You start by recording it…

* Press ‘Ctrl-X (‘

* Do whatever you want the macro to do. This can include whole Lisp functions, even. As long as it can be defined in keystrokes, it’s all good.

* Press ‘Ctrl-X )’

* Now pressing ‘Ctrl-X e’ will repeat the macro each time.

* You can fire off multiple bursts of your macro by preceding it with ‘Ctrl-U N’ where N=the number of times you want it to repeat. So ‘Ctrl-U 5 Ctrl-X e’ will repeat your macro five times.

The best we can say outside this is that the notoriously chatty Emacs will happily tell you all about itself by typing ‘Ctrl-H ?’. Typing ‘Ctrl-H p’ will call up a menu of help topics.

Whole books have been written on getting the most out of Emacs, notably O’Reilly’s Learning GNU Emacs, Third Edition . Learning ELisp itself is another topic besides, but that’s all serious stuff. What about the stupid tricks?

Things You Didn’t Know Emacs Could Do…

Games include simple stuff like Tetris (‘Meta-X tetris’), Bubbles (‘Meta-X bubbles’), and Sokoban (‘Meta-X sokoban’). Plus a moderate psychoanalysis courtesy of Eliza (‘Meta-X doctor’). These are built-in amusements, they’re old hat. Plug-ins really expand that horizon…

* emacs-chess A chess client, which can play against any chess engine you have installed, or against another player.

* GNU-Go The other brainy board game. Once you have GNU-Go installed, you can call it from Emacs with ‘Meta-X gnugo’.

* Nethack If you’re loving PixelDungeon and all its variants on Android platforms, this is the 2D turn-based RPG that started it all.

* Poker A video poker game.

* Pacman And we don’t mean text mode, either.

That last entry brings the promise of breaking out of the world of text graphics could it be? With a proper install of image support, ‘Meta-X image-mode’ should be able to display images including PNG, JPG, and GIF. You can do this from dired mode for sequential browsing. What else could we be missing here?

* EWW A web browser. With some CSS and html5 support. Granted, it’s no Firefox, but it’s more like a miracle that it can work at all.

* In fact, if you have Emacs 25, you can even watch YouTube videos in Emacs. You do need to have VLC working for this trick. Here’s an overview for the brave.

That’s OK for the silly toys, but what about the power tools? Emacs has a long-standing reputation for being a code editor with extensions and modes for nearly any language you can name. It’s usually as simple as invoking the mode for the language you’re in. But the real splash in Emacs news is Magit , a Git interface implemented as an Emacs package. Org-mode is also one of the most highly-touted Emacs tools, with a number of spreadsheet-like functions.

How Far Have We Come…

Not bad for a 40-year-old program? It’s amazing how what was once a kludged-together text editor has now become an embedded operating system in its own right. Get good enough at using Emacs, and it could be the only interface you’d even need.

Read more articles by Pete

Viewing all articles
Browse latest Browse all 11063

Trending Articles