×Welcome to Q&A for professional and enthusiast programmers — check out the FAQ!

My primary editor is Emacs, but my usage habits and knowledge of features has barely changed over the last few years.

What are the Emacs features that you use on a daily basis? Are there any little-known Emacs features that you find very useful?

Edit: Made this into the recommended poll format...please put one feature per answer from now on.

link|improve this question
show 4 more comments
feedback

89 Answers

1 2 3

Auto-complete words in your code based on what already exists in the buffers:

M-/

Keep hitting M-/ until you find the word you are looking for.

Cuts down tremendously on how much you type.

link|improve this answer
13  
As mentioned below, hippie-expand is a good feature. (global-set-key (kbd "M-/") 'hippie-expand) It's just like the default binding for M-/ - except more powerful. – Trey Jackson Sep 22 '08 at 15:02
feedback

A not so well known feature that I find very useful is M-x align-regexp. If you mark a region and execute it, you will be asked for a regular expression, and emacs will then line up the first match of that expression on every line, by padding with spaces (or tabs, if you like). For example, say that you have a list of variables in your code:

int a = 2;
int a_longer_variable_name = 73;
int i = 0;

Then you can mark those lines and to M-x align-regexp and specify = as the expression, and it will align it like:

int a                      = 2;
int a_longer_variable_name = 73;
int i                      = 0;

Maybe not the most important feature in the world, but I use it regularly, and it sometimes impresses non-emacs people. :)

link|improve this answer
2  
I love this! Doing this by hand is a real pain. I'd never even thought about automating it! – allyourcode Apr 17 '09 at 18:31
50  
The only problem with this is that if later you add a longer variable name than your maximum, you need to re-align the other lines - which introduces meaningless DIFFs into your source control. The general rule is that a line shouldn't change in source control unless its meaning or functionality changes. – Erik Forbes Apr 30 '09 at 18:20
13  
Erik: If you add a new variable, "it" (the block of variable definitions) has changed it's meaning or functionality. Code should be presented in a way to best convey it's meaning to the reader and, until we have editors that can do it all automatically the way each person likes it done, this type of formatting will continue to happen. – RHSeeger Jun 17 '09 at 14:34
8  
@Erik Forbes: That's because source version control systems are broken. They should be agnostic: parsing the AST for a C source file and noticing that they're actually identical files, same for Java, C#, etc. Anyway, the issue you raise and 15 people upvote can be solved by formatting to a common accepted codestyle before committing. Now comes a line about "people seeing the world as it is and wondering: why?" versus "people imagining the world as it could be and wondering: why not?". Something like that ;) – SyntaxT3rr0r May 21 '10 at 16:01
4  
@Erik, that's what diff -bB is for – jdizzle Jul 14 '10 at 12:24
show 6 more comments
feedback

M-x occur

This feature is very useful when re-factoring/analyzing code. It's one of the things that keeps me from becoming a Vim user.

link|improve this answer
11  
:g/regex in vim will do the same thing ;) – camflan Oct 28 '08 at 15:01
4  
Just hitting C-s (for search) already highlights all occurrences in a default Emacs – ShreevatsaR May 15 '09 at 23:02
1  
This is one of the reasons I simply love Emacs: after about 7 years of use I can still discover useful new things coming with the default installation. Sometimes the shiny "new" feature I just discovered or learned about is 10-15 years old ... :-) – paprika Feb 12 '10 at 7:17
5  
Don't forget about M-x multi-occur and M-x multi-occur-in-matching-buffers, which allow you to occur across multiple buffers. – ataylor Jul 15 '10 at 5:36
feedback

Anything

Anything! It's a kind of Quicksilver / Launchy / slickedit / Ubiquity tool inside Emacs.

Out of the box it allows you to type a word or regex and presents you lists of matching:

  • buffers (defaulting to the previous buffer, allowing you to switch back & forth between two buffers)
  • files from the current directory
  • recent files
  • emacs functions / commands
  • info pages
  • man pages

And it's easily extensible, you can easily add Google suggestions, or grep / find / mdfind tools (most of them being allready there).

With Anything, I don't need iswitchb anymore, and I rarely use (ido-)find-file, since most of the time the files I want to open are already in the recent files list or the current directory list.

It changed the way I use Emacs!

Org Mode

Org Mode for project planning. From the home page:

Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing project planning with a fast and effective plain-text system.

It comes with Emacs, and it has a great integrated, plain text, programmer friendly, builtin table & spreadsheet engine! (Includig auto re-aligning and formulas.)

Smart Tab

Smart Tab makes your tab key smart. If you're at the end of a symbol it tries to complete it, otherwise it indents the current line / region.

It works great for me: it makes line indenting, region indenting and completion available near the home line.

link|improve this answer
1  
Tried anything, and its an awesome tool! I was just unable to actually use it, since it "destroys" my regular find-files by making me unable to complete dirs/files and/or edit the suggestions from anything! Not sure if this could be win32-related or not, but I went back to ido. – sandos Feb 9 '10 at 12:58
feedback

Don't forget about a little known command C-x M-c M-butterfly

link|improve this answer
17  
Please don't upvote jokes in questions not tagged as 'fun' – Jakub Narębski May 7 '09 at 11:49
58  
Quite right: no fun unless the fun label is present; also, never on Sundays. – Telemachus Jul 14 '09 at 12:16
16  
Hey, it exists in emacs 23 now... – Kendall Helmstetter Gelner Aug 13 '09 at 22:19
9  
Somebody set us up the vote! – Konamiman Oct 23 '09 at 16:41
2  
You know M-x butterfly actually exists, right? – Adam Jul 14 '10 at 17:44
show 2 more comments
feedback

self-insert, by far, imo.

link|improve this answer
9  
+1. Wow, using Emacs for typing text?! That's the last thing one would imagine. – ShreevatsaR Oct 23 '09 at 16:45
feedback

Keyboard macros are very powerful when dealing with list of stuff...I use them as often as possible.

link|improve this answer
feedback

Here's the rundown of my F-keys:

F1: Gnus. Takes some learning, but cut my daily email time down to 20 minutes.

F2: ansi-term. Fairly full featured terminal emulator with a mode where you can switch back into behaving like an emacs buffer for kill/yank/macro goodness.

F3: slime. Crazy good tool for hacking the good hack with Common Lisp.

F4: switch-to-buffer "scratch". The scratch buffer is a good place to do fancy Emacs stuff when you don't want to type in the mini-buffer (try C-j).

F5: compile. You don't realize how great this is until you also grok next-error and previous-error.

F6: visits the buffer I have usually associated with an SQL session, either in an ansi-term or using psql.

F7: w3m. Very slick, fast web browser. Perfect for searching documentation.

F9: Visit my todo file in org-mode. org-mode, like gnus, takes a while to learn, but you can go so damn fast with it, it's worth it.

F10: calendar. Not as nice as org-mode but I haven't switched over all the way yet.

F11: open the EMMS playlist. EMMS doesn't come with the usual upstream Emacs distribution, but it's a pretty reasonable OGG/MP3/etc player.

F12: I run ERC and bitlbee. F12 brings me to the chat buffer with all my contacts from Google Talk and AIM in it.

Insanely cool thing not on one of my F-keys: TRAMP. TRAMP integrates into the way emacs finds, reads, and writes files. You can use file names like /sudo::/etc/passwd to edit things as root without starting another emacs, or /ssh:jfm3@jfm3.org:foo/bar.html to edit files remotely without staring a remote emacs.

Generalized ompletion modes and facilities are useful too. I like completion.el, but there are several to choose from.

Definitely check out www.emacswiki.org. Tons of good ideas there. Good luck!

link|improve this answer
feedback

string-rectangle (C-x r t) and kill-rectangle (C-x r k). They allow to add stuff in front of every line in a block of text or---vice-versa---remove such stuff.

link|improve this answer
1  
@Sridhar: Indenting blocks of code, editing text tables (in combination with yank-rectangle (C-x r y)). But yes, I also use it mainly for commenting/uncommenting and similar stuff (like adding a double quotation mark in front of every line). – Heinzi Jul 22 '10 at 9:56
show 1 more comment
feedback

Elisp. While the language itself can be a little bit clunky (for a lisp) it is so nice to be able to extend or fix your editor on the fly when you need to do so.

link|improve this answer
1  
That's like wishing for more wishes! You can't do that. =) – technomancy Jul 27 '10 at 17:43
feedback

Flyspell-mode! "Flyspell enables on-the-fly spell checking in Emacs by the means of a minor mode."

Can be configured to check LaTeX documents and comments in some other languages (flyspell-prog-mode).

link|improve this answer
show 3 more comments
feedback

dired (M-x dired) let me navigate folders by touch-typing, much faster than browsing in a GUI.

link|improve this answer
1  
This is what I mainly use emacs for :) – sandos Feb 9 '10 at 13:02
show 3 more comments
feedback

yasnippets

Textmate-like snippet expansion functionality for Emacs.

link|improve this answer
6  
I wouldn't call it refactoring; maybe snippet expansion or code-assistance are better terms. – Cheeso May 11 '09 at 16:12
3  
the project is hosted on google code - code.google.com/p/yasnippet – mozillalives Apr 14 '10 at 13:31
show 1 more comment
feedback

I'm not sure if iswitchb is little known but it's something I use countless times everyday. It makes switching buffer that little bit easier. When you enter C-x b to switch buffers iswitchb allows you just to enter a unique substring of the buffer name to select it which can often just be a single character. It's hard to explain but easy to try and once you've tried it you'll never switch it off.

partial-completion-mode does something similar in that it can autocomplete any minibuffer commands. It works best if it give it a hint by typing any dashes in the commands. For example, for M-x yank-rectangle I can use M-x y-r [tab] and have it autocomplete.

And there's TRAMP for opening files on remote machines, usually via SSH. It works a treat on Windows using plink.exe from the Putty package.

link|improve this answer
show 5 more comments
feedback

Most of us are familiar with M-! for running a quick shell command from inside Emacs. A somewhat lesser known facet of this function involves using a prefix command of 1:

M-1 M-!

This runs the shell command and inserts the output at point in my buffer.

But what good is that? It lets me insert text into buffers, but not interact with it in any significant way. What I really need is a way to get the shell to take input from my buffer. Enter the cousins M-| and M-1 M-|. From the docs:

M-| runs the command shell-command-on-region, which is an interactive compiled Lisp function in `simple.el'

M-1 M-| runs a shell command on the region and replaces the region with the output of the command.

In general, I prefer to use features built into Emacs, but when there's no Elisp equivalent available, I can carry out text transformations I need simply and easily:

M-1 M-| perl -p -e 'y/a-zA-Z/n-za-mN-ZA-M/'

To rot-13 encrypt a region quickly, for instance.

(Incidentally, The keystrokes C-1 through C-0 and M-1 through M-0 are the same (by default) as C-u 1 through C-u 0, which makes it very easy to type in repeat arguments. Just hold down M and type in 1 and |, or hold down C and type in 7 and n.)

link|improve this answer
show 2 more comments
feedback

I like interactive SQL mode to give me command line editing and history while using sqlplus.

M-x sql-oracle

And I add the following to my .emacs file:

(defun my-sql-interactive-mode-hook ()
  (setq tab-width 8))
(add-hook 'sql-interactive-mode-hook 'my-sql-interactive-mode-hook)
(require 'sql)

Now I can start a sqlplus session and I get C-up and C-down to navigate through prior sql commands (similar to shell mode). This requires sqlplus to be available.

link|improve this answer
3  
Also, M-x sql-mysql, M-x sql-postgres, etc, for just about any SQL database you're likely to be using. – ataylor Jul 15 '10 at 5:39
1  
In addition, I find it useful to (setq truncate-lines t) in my sql-interactive-mode-hook, so emacs won't try to wrap wide table selects. – ataylor Jul 15 '10 at 5:55
show 2 more comments
feedback

M-x re-builder

Interactive regular expression builder that shows you live in the current buffer just what the regular expression is matching. Great for working out what things might need to be escaped (e.g. () pairs) and whether there's hidden tabs in the whitespace.

When you're done you just cut-n-paste into M-x query-replace-regexp or similar.

link|improve this answer
feedback

How about the newbie-friendly

C-h k

which shows the documentation for the command associated with any key.

Or

C-h f

which shows the documentation for any particular command, given its name (i.e. what you would type after M-x).

I find these to be very helpful while learning emacs.

link|improve this answer
1  
C-h a is arguably more useful than C-h f on the assumption that you don't quite know the command name – jk. Mar 23 '10 at 13:50
show 1 more comment
feedback

hippie-expand, best text expansion in emacs, binding it to TAB and it will always do the right thing (and also very customizable).

link|improve this answer
feedback

Though it's not an emacs feature, it's always worthwhile to point out to any newbies to remap their caps lock keys to control. Save those pinkies!

link|improve this answer
2  
despite popularity, this is a very bad advice--it puts undue stress on the left hand and the left pink! the original emacs keyboard had controls on both sides of spacebar (followed by meta, super and hyper). splittist elaborates here, splittist.livejournal.com/3114.html – user6205 Sep 13 '08 at 20:26
11  
Traditional UNIX keyboards typically had the control key where the caps lock key is now, hence the remapping tradition. You use your pinky for hitting shift and tab, and caps is even easier - give the finger some credit! It's a LOT better than the default layout, so I wouldn't call it bad advice. – Alasdair Sep 16 '08 at 2:49
show 3 more comments
feedback

Using ido for finding symbols in the current buffer: http://www.emacswiki.org/cgi-bin/wiki/ImenuMode

Speeding up navigation by adding shortcuts to jump 5 lines per keystroke:

;; Faster point movement
(global-set-key "\M-\C-p" 
  '(lambda () (interactive) (previous-line 5)))

(global-set-key "\M-\C-n" 
  '(lambda () (interactive) (next-line 5)))

But the single biggest productivity booster for me has been swapping numbers and punctuation when programming in C++: http://infolab.stanford.edu/~manku/dotemacs.html

link|improve this answer
2  
If you're moving more than a few lines, you probably should be moving either by page, or by using search. It's much faster to C-s sometext in most cases. Just pick something near the line you need. When it gets automatic, it's lightning fast. – Paul McMillan Dec 14 '09 at 4:18
1  
If you want to invoke commands that accepts numeric arguments you can use prefix C-u to send an argument of 4 to the command. So C-u C-p moves 4 lines up by default. – antonj Apr 7 '10 at 8:44
feedback

Pretty hard to limit myself to the single most useful feature, but I like this command that hasn't been mentioned yet: auto-revert-tail-mode.

Open up a log file you’d like to monitor in emacs and then do M-x auto-revert-tail-mode. Emacs will automatically watch the file, just like POSIX tail -f.

link|improve this answer
show 2 more comments
feedback

M-x rgrep

I've bound it to F4, I'm using it all the time to interactively greping for file in a recursive manner. Combine that with easy bindings for next-error (F9), previous-error (shift-F9) and you have struck gold!

It always makes my co-workers impressed how quickly I can find things. No tags needed and it's /very/ fast.

link|improve this answer
feedback

Someone else mentioned keyboard macros, but provided a broken link.

That feature may seem inane if you've used it with other platforms. But keyboard macros in Emacs are far more powerful, or at least easy to access the power of, than any other system I have used.

You can start simply by typing "C-x (", then anything you do is recorded to be played back... which means searches, replacement, opening new files, and so on. Then when you are done you can type "C-x )" to end the macro, and C-x e to replay the last keyboard macro. If you want to execute a macro multiple times, you can do so by pressing "e" repeatedly after the first execution via "C-x e". You can also run name-last-kbd-macro to name it, then use insert-kbd-macro to save off the macro you just defined somewhere (like .emacs).

The real power of macros comes into play when you use multiple cut buffers (registers), to save away various fragments of text in the middle of a macro and re-use the parts elsewhere. You can for example have a macro that expects some key text to be in register A for insertion, perhaps a search term in register B, and some other text to append elsewhere in register C grabbed on the fly within the macro. Or of course use multiple registers like multiple clipboards, all within emacs - the key bindings are different than they once were, but the commands you want are copy-to-register and insert-register.

All that, and it's basically as simple as typing something once (carefully) and then having Emacs repeat what you were doing with very complex changes possible.

I have used the combination of keyboard macros and registers to do things as diverse as turn lines of words into multiple lines of code appropriate for the word on the line, repair damaged LDIF files from an LDAP system, or do various refactoring tasks.

link|improve this answer
show 7 more comments
feedback

Org-mode. My whole life is in a massive, single .org file.

link|improve this answer
feedback

Compiling from within emacs, and then for each error, being able to visit the line of code causing the error with a keystroke.

link|improve this answer
feedback

Bookmarks. Emacs bookmarks are both named and persistent, two features that many editors fail to offer.

They are invaluable because not only can I save my place in a file, I can also bookmark a directory that will be opened with Dired. I use them to jump to my most visited locations on the file system, and my main Org Mode files. Having a bookmark which effortlessly takes me where I want to go prevents me from losing my focus while I click around in the Finder or Windows Explorer.

I manage large numbers of bookmarks by namespacing them - all of my Org file bookmarks start with 'org' for example. This makes it easier to find a bookmark with tab-completion.

How to use them:

  • C-x r m BOOKMARK <RET> Set the bookmark named BOOKMARK at point (`bookmark-set').

  • C-x r b BOOKMARK <RET> Jump to the bookmark named BOOKMARK (`bookmark-jump').

  • C-x r l List all bookmarks (`list-bookmarks').

I'm still waiting for Visual Studio to introduce named bookmarks - you need to install ReSharper just to get numbered ones!

link|improve this answer
2  
Thank you so much for that, I had no idea it existed. Tt will make my life so much easier! – Vivi Aug 25 '10 at 23:26
1  
See also Bookmark+. – Drew Sep 5 at 7:29
show 1 more comment
feedback

M-x gdb to test in the debugger while automatically moving through the source tree in other windows. Set break points by opening the source file and doing C-x on the appropriate source lines.

link|improve this answer
show 2 more comments
feedback

M-x shell to directly access the shell from emacs. When used with a split screen (C-x 3), it becomes very quick and easy to do things. No need to continually switch between programs.

link|improve this answer
3  
I prefer M-x eshell when running emacs on a Windows. It gives a more *nix-like experience over the native cmd.exe shell. (Plus, you can execute elisp at the command-line.) – Ray Vega Mar 27 '10 at 6:19
show 1 more comment
feedback

Most-used and most-useful features (for me):

  • navigational keyboard bindings (M-b, M-f, C-a, C-e, etc) work well for fancy keyboard layouts. You do not use Home, End, PgUp, PgDn, arrows, therefore there is no need to relearn touch-typing when, for example, on notebook these keys are placed in unusual places.

  • It works in console (emacs -nw) as well as with GUI. And it works under Windows, Linux, Mac. You can use the same editor both in command-line and GUI environment on any OS.

  • It has server-mode which allows an instant opening of new documents in the same editor environment.

  • It allows to view several documents (and/or different parts of the same document) simultaneously. It is especially useful for wide-screen monitors.

  • Embedded command-line (M-!).

  • version-control (C-x v v - do the next logical version control operation on the current file.)

  • find, open, switch, create file, buffer (C-x C-f, C-x b) via ido.el

link|improve this answer
feedback
1 2 3

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.