Emacs: Friend or Foe?
If you're like me, you find Emacs somewhat intimidating. Well, if you're like me, you've been an exclusive vi user for years, and have always wanted to start using Emacs, but have had trouble doing so. Each time you try to switch over, you find yourself pressing <esc> after every third sentence, which (in Emacs) produces some random, and usually undesirable, result. Once, I was thrown into the infamous “Doctor” mode, which is a simple AI program emulating a psychiatrist. There, I spent a good ten minutes telling Emacs what I thought of it. (The response was, "Perhaps you could try to be less abusive.")
Using one of Emacs' vi-emulation modes isn't of much help, either; none of them seem to handle even basic vi commands appropriately. And if you tend to use less-well-known vi features, you're simply out of luck. (Ever try changing the window size with nnnzwww^? I didn't think so.)
As impossible as it may seem, you can find a place for Emacs in your everyday life. If you have the diskspace and memory (a far-from-negligible detail), I strongly suggest looking into Emacs; it can make your life easier, even if you think that you're perfectly content with vi.
Whether you're accustomed to vi or not, however, the default Emacs configuration on many systems is, well, less than adequate. While many users learn to live with Emacs “out of the box”, I've never been content with that approach. Instead, you can get Emacs to do just about whatever you want it to do. So, let's take a look at some customizations that I've found particularly useful.
All of the customizations discussed here involve editing the Emacs configuration file, which is ~/.emacs by default. This file is written in Emacs LISP (Elisp), which is the internal LISP engine that Emacs uses for nearly everything. For example, keystrokes map to Emacs LISP commands. You can modify the command to be executed for each key sequence, and even write your own Elisp functions to bind to keys.
If you're not a LISP programmer, never fear. Most of the LISP forms described here are quite simple and don't require a degree in artificial intelligence to comprehend.
I do assume that you have at least tried to use Emacs, and know how to get into and out of it, are familiar with the Info documentation system, and so on. If not, fire up Emacs and type Ctrl-h (C-h) followed by t, which will drop you into a tutorial. (From there, you're on your own.) The following customizations work under Emacs 19.24. By the time you read this article, there will more than likely be a newer version available, for which certain things may have changed.
Before we can customize Emacs, we need a customization file. By default this is .emacs in your home directory. (Later, we're going to move the contents of .emacs to another file, so buckle your seatbelt.)
Our first task is to rebind several keys to perform more reasonable actions. Admittedly, several of these key bindings are vi-oriented, but should make sense nonetheless. First of all, I like to page back and forth through the document with C-f and C-b, as in vi. In order to rebind these keys, we'd include these lines in our .emacs file:
; Modify meaning of C-f and C-b (global-set-key "\C-f" 'scroll-up) (global-set-key "\C-b" 'scroll-down)
As you can see, comments in Elisp begin with a semicolon and extend to the end of the line. Here, we invoke the function global-set-key twice; once for C-f and again for C-b. In LISP, expressions are parenthesized. The first item in each list is the name of the function to call, and it is followed by any arguments. The first argument to global-set-key is a string constant, representing the key sequence to bind. The second argument is the name of the Emacs LISP function to bind it to. The function name is single-quoted in order to refer to the function name itself, not the function that it points to.
One question remains: How did we know that scroll-up and scroll-down referred to the Emacs functions to scroll pagewise through the document? Other than reading the documentation, you could find this out by using the Emacs function describe-key (which you can invoke with C-h k). Calling this function and following it with a key sequence will tell you the Emacs function name that the key sequence maps to. If you knew that the Emacs keys C-v and M-v (Meta-v, where “meta” is usually the <esc> key) scrolled back and forth through the document, you could use C-h k to determine the function names for these keys. For example, the sequence C-h k C-v will display the function name bound to C-v.
Here are a few more key rebindings that I use often:
(global-set-key "\C-v" 'end-of-buffer) (global-set-key "\M-v" 'beginning-of-buffer) (global-set-key "\C-p" 'yank) (global-set-key "\C-u" 'undo) (global-set-key "\C-r" 'overwrite-mode)
The first two commands rebind C-v and M-v (which previously scrolled by pages) to move to the beginning and end of the buffer, respectively. The third causes C-p to yank the previously deleted or copied region of text, and the fourth causes C-u to execute the undo command. (I tend to use the Emacs' undo feature quite a bit, and find C-x u or C-_ too cumbersome.) The last command causes C-r to toggle overwrite mode.
There are some caveats associated with rebinding keys in this manner. For one thing, it destroys the previous definition of the key. In many cases, this is fine; as long as you didn't use the key as previously defined. However, the last of the above commands rebinds C-r, which is used to invoke a backwards incremental search; a very useful feature. Before rebinding a key you should be sure that its previous definition doesn't mean much to you.
Fast/Flexible Linux OS Recovery
On Demand Now
In this live one-hour webinar, learn how to enhance your existing backup strategies for complete disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible full-system recovery solution for UNIX and Linux systems.
Join Linux Journal's Shawn Powers and David Huffman, President/CEO, Storix, Inc.
Free to Linux Journal readers.Register Now!
- Back to Backups
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- Google's Abacus Project: It's All about Trust
- A New Version of Rust Hits the Streets
- Secure Desktops with Qubes: Introduction
- Seeing Red and Getting Sleep
- Fancy Tricks for Changing Numeric Base
- Secure Desktops with Qubes: Installation
- Working with Command Arguments
- Linux Mint 18
Until recently, IBM’s Power Platform was looked upon as being the system that hosted IBM’s flavor of UNIX and proprietary operating system called IBM i. These servers often are found in medium-size businesses running ERP, CRM and financials for on-premise customers. By enabling the Power platform to run the Linux OS, IBM now has positioned Power to be the platform of choice for those already running Linux that are facing scalability issues, especially customers looking at analytics, big data or cloud computing.
￼Running Linux on IBM’s Power hardware offers some obvious benefits, including improved processing speed and memory bandwidth, inherent security, and simpler deployment and management. But if you look beyond the impressive architecture, you’ll also find an open ecosystem that has given rise to a strong, innovative community, as well as an inventory of system and network management applications that really help leverage the benefits offered by running Linux on Power.Get the Guide