23

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
5
  • 9
    C-x C-c. No, seriously, I love Emacs. Nov 17, 2010 at 18:29
  • 6
    M-x vi-mode....
    – user4051
    Nov 17, 2010 at 18:35
  • 1
    org-mode is great for notes. Not a command though.
    – kasterma
    Nov 17, 2010 at 20:42
  • another not command, but nice to look at ido-mode.
    – kasterma
    Nov 17, 2010 at 20:42
  • 5
    M-g M-g is a shorter invocation of goto-line Nov 18, 2010 at 21:54

9 Answers 9

21
  • C-h a -- Apropos search functions
  • 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.

3
  • 6
    C-h a -- Apropos search functions
    – asm
    Nov 17, 2010 at 21:52
  • 3
    Apropos should be at the top of the list. First you have to discover things before you can read the documentation.
    – starblue
    Nov 18, 2010 at 8:18
  • C-h m -- describe-mode. Describes the current mode which is also super useful.
    – orpheus
    Jan 22, 2021 at 23:21
24

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

6
  • I think I need to print this out and upvote you a million times. Nov 17, 2010 at 20:44
  • 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, 2010 at 14:07
  • 2
    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, 2010 at 19:28
  • @haylem, you're right. fixed it in the answer.
    – GSto
    Nov 18, 2010 at 19:37
  • In fact M-x is for execute-extended-command. You use it for unbound commands (and align-regexp is just one of them).
    – rsenna
    Sep 18, 2013 at 2:25
11

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

4
  • nice! didn't know about this one, and found myself wishing that it existed.
    – GSto
    Nov 17, 2010 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, 2010 at 13:52
  • I bind C-; to toggle-comment-on-line to handle the line case and keep comment-dwim with its original functionality. Aug 2, 2016 at 21:43
  • Err, and should have included the toggle-comment-on-line function :) (defun toggle-comment-on-line () (interactive) (comment-or-uncomment-region (line-beginning-position) (line-end-position))) Aug 3, 2016 at 18:23
6

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.

1
  • Ashamed to say I had never heard of that one!
    – haylem
    Nov 18, 2010 at 19:27
3

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.

2
  • 1
    Nice symmetry, given that lisp is a programmable programming language. (Or, a programming language that allows you to succintly describe a programming language.) Nov 18, 2010 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. Jun 13, 2011 at 16:54
1
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!

1
  • 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.

-1

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

1
  • (global-auto-revert-mode 1) in your emacs file, I used that a lot as well.
    – asm
    Nov 17, 2010 at 21:50
-1

M-x help-with-tutorial

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