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
Chris Smith
  • 5,218
  • 2
  • 26
  • 29

9 Answers9

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

GSto
  • 8,531
  • 7
  • 39
  • 59
  • I think I need to print this out and upvote you a million times. – CodexArcanum Nov 17 '10 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 '10 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 '10 at 19:28
  • @haylem, you're right. fixed it in the answer. – GSto Nov 18 '10 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 '13 at 02:25
  • start keyboard macro is `C-x (` and end kmacro is `C-x )`, but I think `f3` for start and `f4` for end or call macro are more common these days. – Greg Nisbet Nov 23 '16 at 05:38
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.

serverhorror
  • 101
  • 2
Alexey Voinov
  • 574
  • 4
  • 5
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

Jason Viers
  • 381
  • 1
  • 2
  • 5
  • 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
  • I bind C-; to toggle-comment-on-line to handle the line case and keep comment-dwim with its original functionality. – Chris Clark Aug 02 '16 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)))``` – Chris Clark Aug 03 '16 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.

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.

Inaimathi
  • 4,884
  • 1
  • 26
  • 34
  • 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
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!

Brad Clawsie
  • 740
  • 3
  • 7
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.

Paul Nathan
  • 8,560
  • 1
  • 33
  • 41
-1

M-x help-with-tutorial

Vatine
  • 4,251
  • 21
  • 20