(Stupid) Useful Emacs Tricks?
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' ;-)"
Of all the Elisp I've written this grep is what I most miss when I use a coworker's environment.
/dev/null")))
(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)
M-x tetris
M-x doctor
M-x yow
M-x phases-of-moon
(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.
Football Odds
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.
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/
"Skill shows through where genius wears thin." -Wittgenstein || Religion: uniting aviation and architecture.
M-x hanoi - plays tower of hanoi for n number of towers
(Pretend that '.'==' ' because Slashdot hates programmers.)
Before:
After running M-xalign-regexp=:
Dewey, what part of this looks like authorities should be involved?
(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.
We can pick a topic (C++ STL,C++ boost, Eclipse, Electioneering, etc) and then post the many clever/stupid tricks we have learnt.
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".
;; 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)
[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!
Be nice to people on the way up. You will meet them again on your way down!
By far one of the best enhancement packages to come out for (X)Emacs in a long time is org-mode. It's a feature-full organization and agenda system that all operates on plain text files that are easy to read and write.
The next site to slashdot will be ready soon, but subscribers can beat the rush and start slashdotting it early!
Much goodness has been mentioned here, but not the optional emacs-calc package (does vi do calculus? I think not). Plus picture-mode, great for drawing little boxes and, surprise surprise, putting the row of backslashes down the right edge of newly macrofied code blocks (M-x picture-mode C-c ., set typing direction to vertically downwards).
Plus for maximum fun, thinking you're in insert mode because you slipped and hit the wrong key, and actually finding you're in command mode and have just made about 452 different random changes to your document with no idea of what has happened or how to fix it...
You are not alone. This is not normal. None of this is normal.
Haha, I really don't understand why vi doesn't have anything helpful at all when you start it up. Emacs' tutorial is so nice and helpful, whereas every time I start vi I have that same experience and just keyboard mash until I manage to quit. I admit that I haven't spent much time trying to learn vi, and only really started using emacs this semester (first semester of college) but if I'm ever on a system that doesn't have emacs nano is better since I can actually figure out how to save and quit without reading documentation. I think that has to be one of the tenets of good UI design; if it isn't completely obvious how to operate a program, it should at least be obvious how to get help within the program. Once I'm in vi and don't know anything knowing to check the manpage isn't going to be much help. I picked up some basic knowledge from the vi thread here but sheesh, that kind of stuff should be readily available inside the program.
All your base are belong to Wii.
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.
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.
Hey, I finally got my first freak! Took you long enough!
Notepad has precisely one useful trick, documented here.
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.
"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"...)
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.