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,258

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,521
  • 8
  • 41
  • 59
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.

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

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,874
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.

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

M-x help-with-tutorial

Vatine
  • 4,269