Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm a fairly new convert to Emacs and I really love it as an editor, primarily because I keep finding new and super-useful commands. Are there any other programmer 'must known' commands missing from my list?

M-x replace-string     - Find and replace a given string.
M-x goto-line          - Goto a specific line
M-x column-number-mode - Show the current column number in text bar
share|improve this question

closed as not constructive by Mark Trapp Jul 16 '11 at 4:27

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

8  
C-x C-c. No, seriously, I love Emacs. –  Adam Crossland Nov 17 '10 at 18:29
5  
M-x vi-mode.... –  user4051 Nov 17 '10 at 18:35
1  
org-mode is great for notes. Not a command though. –  kasterma Nov 17 '10 at 20:42
    
another not command, but nice to look at ido-mode. –  kasterma Nov 17 '10 at 20:42
5  
M-g M-g is a shorter invocation of goto-line –  Frank Shearar Nov 18 '10 at 21:54

9 Answers 9

up vote 18 down vote accepted
  • C-h b -- runs describe-binding
  • C-h k -- runs describe-key
  • C-h f -- runs describe-function
  • C-h v -- runs describe-variable

If you know those, you can explore emacs and find things you still don't know. Learn how to learn, thats essential. Everything else can be found out later.

share|improve this answer
4  
C-h a -- Apropos search functions –  Andrew Myers Nov 17 '10 at 21:52
2  
Apropos should be at the top of the list. First you have to discover things before you can read the documentation. –  starblue Nov 18 '10 at 8:18

Well, First You need to know some of the basics of text editing:

C-w : Cut 
M-w : Copy
C-y : Paste
C-x s : save
C-x c : save all and close

Then, it's handy to learn how to move around the file:

M-b : back one word
M-f : foward one word
C-a : beginning of line
C-e : end of line
C-n : next line
C-p : previous line
M-< : beginning of buffer
M-> : end of buffer   

Then, It's good to start learning how to navigate with multiple files/buffers and windows

C-x C-f : find file
C-x b : switch buffer
C-x k : kill buffer
C-x 2 : split-window-vertically
C-x 3 : split-window-horizontally
C-x o : switch window
C-x 0 : kill this window
C-x 1 : kill all other windows

After that, here are a few other misc. commands that can come in handy:

C-s : search
C-r : search backward
M-/ : autocomplete word (based on previous words in the file)
M-x : align-regexp
M-( : start keyboard macro
M-) : end keyboard macro
C-x e: execute keyboard macro.

For a complete reference: link

share|improve this answer
    
I think I need to print this out and upvote you a million times. –  CodexArcanum Nov 17 '10 at 20:44
    
+1 for macros.. –  gablin Nov 18 '10 at 9:28
    
Indeed, the last three, macros, are what makes emacs a big win. You can use it subtly and people will think emacs is psychic. –  Macneil Nov 18 '10 at 14:07
    
isn't that rather M-/ : autocomplete word (based on previous words in the file) ? (slash instead of back-slash? Or am I confusing this with something else?) –  haylem Nov 18 '10 at 19:28
    
@haylem, you're right. fixed it in the answer. –  GSto Nov 18 '10 at 19:37

Incredibly handy while coding:

M-; : comment-dwim

comment-dwim will toggle commenting on the current region; commenting if it's not commented, and vice versa. Your current language mode lets emacs know how to do the commenting.

By default, if there's no active region and there's text on the line, it will insert a comment at the end of the line. Personally, I prefer to have it comment the entire current line, which this accomplishes:

      ;; Original idea from
      ;; http://www.opensubscriber.com/message/emacs-devel@gnu.org/10971693.html
      (defun comment-dwim-line (&optional arg)
        "Replacement for the comment-dwim command.
        If no region is selected and current line is not blank and we are not at the end of the line,
        then comment current line.
        Replaces default behaviour of comment-dwim, when it inserts comment at the end of the line."
          (interactive "*P")
          (comment-normalize-vars)
          (if (and (not (region-active-p)) (not (looking-at "[ \t]*$")))
              (comment-or-uncomment-region (line-beginning-position) (line-end-position))
            (comment-dwim arg)))
      (global-set-key "\M-;" 'comment-dwim-line)

Stole it myself from http://www.emacswiki.org/emacs/CommentingCode

share|improve this answer
    
nice! didn't know about this one, and found myself wishing that it existed. –  GSto Nov 17 '10 at 19:30
    
I prefer comment-or-uncomment-region for my commenting purposes. It lets me comment out a block of code temporarily if I need to. –  Inaimathi Nov 19 '10 at 13:52

Try doing the tutorial (C-h t). It teaches you a lot of the fundamental keybindings, and THEN you can start looking for even more fun ones.

share|improve this answer
    
Ashamed to say I had never heard of that one! –  haylem Nov 18 '10 at 19:27

M-x apropos

M-x describe-key

M-x describe-bindings

C-x C-f ~/.emacs (it helps if you know Elisp before running this one)

Pretty much everything else is personal preference. People sometimes talk about Emacs as though it's an editor.

That's not true.

Emacs is a language designed to succinctly express editors (which is to say, Elisp is its best 'feature'). How much mileage you get out of it depends directly and entirely on how well you understand this principle.

share|improve this answer
1  
Nice symmetry, given that lisp is a programmable programming language. (Or, a programming language that allows you to succintly describe a programming language.) –  Frank Shearar Nov 18 '10 at 21:55
1  
Not QUITE correct. Emacs is an interactive LISP environment, that has had certain LISP design decisions resolved in ways that make it easy to write editors and editor extensions, and a whole lot of canned code, some of it compiled into infrastructure, that help to write editors. –  John R. Strohm Jun 13 '11 at 16:54
M-:

this allows you to evaluate arbitrary elisp in the minibuffer

C-x C-q

readonly viewing of a file

C-c C-c

comment region

among others!

share|improve this answer
  • C-j (M-x newline-and-indent)
  • C-M-\ (M-x indent-region)
  • M-. (M-x find-tag) requires running etags on your code
  • M-/ (M-x dabbrev-expand)
  • M-x compile
  • C-x v v (M-x vc-next-action)
  • M-x font-lock-mode

and read the documentation for the language mode you use (C-h m (M-x describe-mode))

I'm also a huge fan of (M-x shell), M-! (M-x shell-command) and M-| (M-x shell-command-on-region) becuase I find it very handy to be able to run command from inside emacs and cut-and-paste the output.

Also, M-x sort-lines, M-x sort-fields and M-x sort-num-fields are useful for keeping long lists of things (like variable names) in alphabetic or numeric order.

share|improve this answer

M-x revert-buffer is one I use a lot.

share|improve this answer
    
(global-auto-revert-mode 1) in your emacs file, I used that a lot as well. –  Andrew Myers Nov 17 '10 at 21:50

M-x help-with-tutorial

share|improve this answer

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