Kicking the ctrl-x/ctrl-c/ctrl-v habit with Emacs

There’s plenty you can do with Emacs… and lots of documentation floating around on the web. This is just a quick tutorial on some basic Emacs techniques that demonstrate how it can be a pleasure to use. I’m mainly writing this for myself, but maybe someone getting started will find this helpful if they’re just getting past the initial learning curve and are daunted by the prospect of having to “research” things that were trivial with their previous text editors.

Two of my favorite features in Emacs are find-file (C-x C-f) and iswitchb-buffer (C-x b). Emacs doesn’t bother me with a file dialog box that takes forever to appear. How many times in a Windows program have I accidentally hit “Open” or “F1” by accident and then have to wait while my computer stalls? The Emacs autocompleting feature makes it much faster to home in on a file I’m looking for and I never have to take my hands off the keyboard. And as far as buffer switching goes, Windows can be annoying no matter how it is set up. If you’ve got tabs on the screen, then you’re wasting on-screen “real estate.” If you are switching buffers using “control – F6” then you’re stuck cycling through the windows in a certain order. Emacs understands that I might want to work with a slew of buffers at once– but also that I tend to switch back and forth between a pair of buffers quite a bit. Accordingly, the buffer switcher lists the names of my open files and autocompletes the file name if I start typing. And the buffers are all listed in order of how recently I’ve touched them, so the default buffer is usually the one I need. Anyways, the point is that you can generally trust the designers of Emacs to do things in a really slick way. A lot of things will seem unnatural at first, but the more you use it, the more you appreciate it.

So fire up Emacs and type “one”, hit return, type “two”, hit return, type “three”, and so on until you’ve typed the words for the numbers one to ten each on their own line. Move the cursor so that it rests on the “o” in the word “one”. You can do this by hitting C-p ten time to move line by line. Or you can type C-u 10 C-p if you want to be fancy. You could even try M-{ C-n. (In Emacs, you should avoid using arrow keys and page up/down.)

Once you get to the “o” in “one” type C-space to set the mark. Type C-n to move down a line and then hit C-w to kill the text. Then alternate between the commands C-n and C-w until you’ve killed all ten of your lines. Hitting C-y will “yank” the most recent bit of text from the kill-ring.

If you type C-y a few times, you should get several lines of text that say “ten”. Try M-y and the last “ten” that you yanked will become a “nine”. Hit M-y a few more times and you’ll keep moving back through the kill-ring. Generally, your position in the kill ring just moves backwards as you keep hitting M-y. To jump several kills back hit C-u and then a number. Enter zero you move one notch forward in the kill ring… and enter a negative number to move several notches forward.

Type C-y to “yank” some text again… and then hit M-y to cycle back until your yanked text becomes a “one”. Type C-y to paste several lines that say “one”. Then type C-u 0 C-y. (That middle key there is a zero.)  This should make a new line that says “two”. Type C-u -2 M-y to change the “two” to a “four”. (The -2 there is negative two.) If you ever jump forward too far, just hit M-y to cycle back through the kill ring until you get where you want.

So you can move around fairly easily using next-line and previous-line commands. (Plus, knowing about using C-u to supply arguments to commands will help you with other tasks.) The curly braces commands to do next-paragraph and previous-paragraph are pretty useful, too. Use them to get into position and C-space to set your mark. Use the movement keys to go to the other end-point of your text section and type C-w to “cut” what you need. (You can use M-w to copy text without “cutting” it.)  Finally, you can use C-y to “paste”, and then M-y (in conjunction with C-u) to navigate a list of things you’ve previously “cut.”

Now that you’ve mastered the basic use of the kill-ring, you’ll be able to shift your text around however you need. Combine these techniques with the the already good file selector and buffer switcher and you have a really powerful tool for navigating and manipulating text files. It takes practice, but if you keep at it the mouse will start looking more like an impediment than a shortcut.

Advertisements

One Response to “Kicking the ctrl-x/ctrl-c/ctrl-v habit with Emacs”

  1. Leonel Says:

    Some other fancy stuff. Put this in your .emacs file:

    (setq x-select-enable-clipboard t) -> When you copy text with M-w, that text will be sent to the clipboard, so you can paste it on other windows.

    (pc-selection-mode) -> this activates the pc-selection mode, which emulates some keys in Emacs similar to Windows, such as HOME, END, S-Delete, S-Insert. This mode makes it easy for newbies to migrate from other editors to emacs.

    My favorite features in this last mode are that it highlights the selected region, and allows to select a region with S-arrow keys instead of C-space. But then, it’s a matter of personal taste.

    C-h l show the last 100 keys you typed.
    C-h f describes a function in emacs
    C-h k describes what a key does.
    M-x apropos lists every feature in Emacs that contains .

    That’s all for today. Of course, there are many more…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: