(Stupid) Useful Emacs Tricks? 412
Count Fenring writes "Since the Vi version of this question was both interesting and popular, let's hear from the other end of the spectrum. What are your favorite tricks, macros, extensions, and techniques for any of the various Emacs? Myself, I like 'M-x dunnet' ;-)"
grep and emacs integration (Score:5, Interesting)
(defun my-grep ()
"grep the whole directory for something defaults to term at cursor position"
(interactive)
(setq default (thing-at-point 'symbol))
(setq needle (or (read-string (concat "grep for <" default "> ")) default))
(setq needle (if (equal needle "") default needle))
(grep (concat "egrep -s -i -n " needle " *
(global-set-key "\C-x." 'my-grep)
(global-set-key [f8] 'next-error)
Lots of them (Score:4, Interesting)
M-x tetris
M-x doctor
M-x yow
M-x phases-of-moon
Some favorites (Score:5, Interesting)
(global-set-key (kbd "C-c c") 'comment-dwim)
C-c c to either comment out a region or uncomment it depending on context. Lovely feature.
(global-set-key "\M-g" 'goto-line)
M-g to go to specified line in buffer. Useful for emacs 21.x users where the keybinding is not yet standard.
(menu-bar-mode nil) (scroll-bar-mode nil) (tool-bar-mode nil)
Gets rid of the ugly TK widgets.
(iswitchb-mode t)
Superboosts C-x b.
(global-set-key "\C-z" 'undo)
The normal binding for C-z is suspend-emacs but having it bound as undo is much more useful imo.
Outlines w/ org-mode (Score:5, Interesting)
I've recently discovered and almost instantly become a fan of org-mode, which is a great outlining tool (including folding, numbering, and other similar things you'd probably expect).
It's also good for lists of things to do, schedules, deadlines, and related stuff. It uses its own really simple markup langauge (similar to trac wiki), but you can include LaTeX and HTML inline.
It comes with exporters to HTML and LaTeX (and iCal for date stuff). You can also put tables inline, and the table editor is excellent for simple tables.
I use it every day for my list of things to do, and use it regularly for outlining text documents, pseudocode, and meeting notes.
I replaced it (Score:3, Interesting)
After 25 years of vi and Emacs, I got fed up and wrote something better. And at 6k lines of C, it's not much bigger than my .emacs file was. :-)
Seriously, people: you don't have to live with the available options. Writing an editor is a easy job.
And yes, I GPL'd it. Have fun.
http://code.google.com/p/aoeui/ [google.com]
My fave (Score:3, Interesting)
Re:Some favorites (Score:3, Interesting)
If we consider external packages too:
The QuickSilver for Emacs: anything.el [emacswiki.org]
align-regexp (Score:4, Interesting)
(Pretend that '.'==' ' because Slashdot hates programmers.)
Before:
After running M-xalign-regexp=:
C-u to delete line backward (Score:1, Interesting)
(defun my-kill-prev-line ()
"Kills from the cursor to the beginning of the line."
(interactive)
(kill-line 0))
(global-set-key "\C-u" 'my-kill-prev-line)
Makes Control-U do what it does in every other place on your computer.
Re:grep and emacs integration (Score:4, Interesting)
Damn, and me with no mod points. Someone please mod this up. It is a good example of the neat sort of thing that you can do with emacs.
Since I use Eclipse a lot, I don't use emacs nearly as much as I used to, but there are somethings that emacs just makes easier. One of them is performing a complicated command many times over. I copy from Eclipse, paste in emacs, do a C-x (, do what I need it to do, C-x ), and then C-u 10000 C-x e. Then, copy and paste it back into Eclipse. Saves lots and lots of time.
Here's something I have in my .emacs:
(global-set-key "\C-x5c" 'word-count )
(defun word-count (start end)
(interactive "r")
(let ((words 0) (lines 0) (chars 0))
(save-excursion
(goto-char start)
(while ( (point) end) (forward-word 1) (setq words (1+ words))))
(setq lines (count-lines start end) chars (- end start))
(message "Region has %d lines; %d words; %d characters."
lines words chars)))
Why don't we do the same every week? (Score:2, Interesting)
We can pick a topic (C++ STL,C++ boost, Eclipse, Electioneering, etc) and then post the many clever/stupid tricks we have learnt.
Re: (Score:2, Interesting)
For GNU emacs:
(require 'ffap)
This means "find file at point." If the cursor is on anything that looks like a file (or URL, etc) it'll try to do the right thing for you automatically. RTFM for delicious details.
"M-q" will reindent a paragraph nicely. ." will set a fill prefix, i.e., if you need to reparagraph something with the " >" prefix for mail replies
"C-x
Keyboard macros are great for mass editing files. "C-x (" to start recording, "C-x )" to stop, "C-x e" to execute. Once you've got the macro working, repeat as needed, "C-u 99 C-x e".
Re:grep and emacs integration (Score:3, Interesting)
(defun get-previous-buffer (numbufs blist)
(if (not blist) (signal 'no-such-buffer ()))
(if (not (buffer-file-name (car blist)))
(get-previous-buffer numbufs (cdr blist))
(if (> numbufs 0)
(get-previous-buffer (1- numbufs) (cdr blist))
(car blist))))
(defun switch-to-previous-buffer (numbufs)
"Switches to the previous file-associated buffer in the buffer
list."
(interactive "p")
(switch-to-buffer (get-previous-buffer numbufs (buffer-list))))
(global-set-key "\M-\C-m" 'switch-to-previous-buffer)
In short, it allows you to switch back and forth between your two most recently used buffers with M-RET, or between elder buffers with a prefix argument. It's a very convenient alternative to C-x b (though it doesn't replace it, obviously).
My two cents (Score:1, Interesting)
;; Color parenthesis matching
(show-paren-mode t)
(setq show-paren-style 'parenthesis)
(setq blink-matching-paren-distance 51200)
;; Scrollbar on the right hand side
(set-scroll-bar-mode 'right)
;; Alt-G to jump to certain line number
(global-set-key "\M-g" 'goto-line)
;; Remove the annoying toolbar
(tool-bar-mode 0)
;; Turn off beeping
(setq visible-bell t)
;; No backup files to cleanup later
(setq make-backup-files nil
backup-inhibited t)
;; No more blinking cursor
(blink-cursor-mode nil)
;; Make pgup/dn behavior sane
(setq scroll-preserve-screen-position 1)
;; Highlight current line
(global-hl-line-mode 1)
;; No more annoying startup message
(setq inhibit-startup-message t)
Completion and incremental search are... (Score:3, Interesting)
[M]/ Will attempt to complete the current word. This is done working backwards from the edit point and can look in other buffers.
If (for example) you are editing a LaTeX document and you wish to cross reference an existing figure with label \label{f:my-figure} entering the following:
\ref{f:[M]/ will repeatedly try to match everything starting with f: , namely all the existing figure labels.
Likewise if referencing a citation in a BibTeX file that is open in another buffer, \cite{startofkey[M]/ works a treat!
Incremental search is also wonderful. When correcting text from (say) a hard-copy simply do [M]S and start typing the first few words of the sentence before the edit point. Magically Emacs will take you there.
This is also very useful for moving around in documents. Simply enter "??" or some other simple placemarker move around, then do [M]S?? and you'll fly back to where you were.
For formatting "paragraphs" [M]Q is the way forward. Quite simply no other editor I have come across has a better understanding of the 'right' behaviour for this.
If you have some text that looks like this (n.b. _ indicates space)
____Some text that is on a line that we wish to wrap around so that the new line starts aligned to the first line. Ordinarily there is no sensible way of doing this bar tab stops, however if you 'do a meta q' it will magically align this paragraph.
Then after a [M]q (fill-paragraph) you will get
____Some text that is on a line that we wish to wrap around so that
____the new line starts aligned to the first line. Ordinarilly there
____is no sensible way of doing this bar tab stops, however if you 'do
____a meta q' it will magically align this paragraph.
So that the paragraph is left aligned to the first line. Finally, as others have said regexp replace and various other packages (e.g. AucTeX) are unparalleled!
Re:Lots of them (Score:3, Interesting)
C-x r k ;; kill-rectangle ;; yank-rectangle
C-x r y
Cut and paste columns of text.
The best recent package for (X)Emacs: org-mode. (Score:3, Interesting)
calc and pictures (Score:3, Interesting)
Comment removed (Score:3, Interesting)
Re:vi is for building emacs (Score:3, Interesting)
Compiling over ssh (Score:4, Interesting)
Plus you can compile over ssh.
It's nice to be able to compile a local directory with M-x compile, then you can jump to errors in the offending file from the *compilation* window.
You can also set to do remote compile from emacs by putting something similar to this in your .emacs:
No you can compile on the remote host AND bring up offending files from the compile output window with the click of a button.
Re:Edit files from anywhere w/ tramp (Score:4, Interesting)
Tramp is also extremely useful for editing files as root without opening a root emacs session. If you use sudo on your system, it's C-x C-f /sudo::/path/to/file, or su: C-x C-f /root@localhost:/path/to/file.
Re:Notepad tricks (Score:3, Interesting)
Notepad has precisely one useful trick, documented here [microsoft.com].
Re:iso-accents-mode (Score:3, Interesting)
Even easier: set one of your keys up as Compose (also known as Multi_key). Then you can type accented characters in any program, not just Emacs.
Re:The EMACS equivalent of "." (Score:3, Interesting)
"C-x z". Then subsequent presses of "z" repeat the same command again.
(You could have found that out for yourself by typing "C-h w repeat"...)
Re:Young people being led astray? (Score:3, Interesting)
What I find tragic is that people actually do say "it's tragic that we're still using the same tools as we used 20 years ago".
Why wouldn't we be? We're pretty much the same people as we were 20 years ago. We still speak the same languages, wear the same clothes, drive vehicles that are only cosmetically different, perform very similar jobs. Sure, our computers are a bit faster, but there's no need to fundamentally change your interface just because there are more transistors behind it.