Stories
Slash Boxes
Comments

News for nerds, stuff that matters

Slashdot Log In

Log In

Create Account  |  Retrieve Password

(Stupid) Useful Emacs Tricks?

Posted by timothy on Fri Nov 07, 2008 08:58 AM
from the bonus-if-it-uses-10-fingers-at-once dept.
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' ;-)"
+ -
story

Related Stories

[+] Ask Slashdot: (Useful) Stupid Unix Tricks? 2362 comments

So the other day I messaged another admin from the console using the regular old 'write' command (as I've been doing for over 10 years). To my surprise he didn't know how to respond back to me (he had to call me on the phone) and had never even known you could do that. That got me thinking that there's probably lots of things like that, and likely things I've never heard of. What sorts of things do you take for granted as a natural part of Unix that other people are surprised at?

[+] Ask Slashdot: (Useful) Stupid Vim Tricks? 702 comments
haroldag writes "I thoroughly enjoyed the recent post about Unix tricks, so I ask Slashdot vim users, what's out there? :Sex, :b#, marks, ctags. Any tricks worth sharing?"
[+] Ask Slashdot: (Useful) Stupid BlackBerry Tricks? 238 comments
Wolfger writes "Continuing the recent (useful) stupid theme: I've recently become a BlackBerry user, and I'm in love with the obvious(?) tricks, such as installing MidpSSH to access my home box remotely. But I'd like to know what more experienced Crackberry addicts can share."
This discussion has been archived. No new comments can be posted.
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More
Loading... please wait.
  • by rallymatte (707679) * on Friday November 07 2008, @08:58AM (#25674781)
    I've found this very useful whenever I'm put in front of emacs C-x C-c sudo apt-get -y purge emacs vi
  • XKCD (Score:5, Funny)

    by WK2 (1072560) on Friday November 07 2008, @09:00AM (#25674789) Homepage

    C-X M-C M-butterfly

  • Huh? (Score:3, Funny)

    by Anonymous Coward on Friday November 07 2008, @09:03AM (#25674825)

    the Vi version of this question was both interesting and popular

    Indeed. Probably because Vi is a popular and usable text editor (unlike Emacs).

  • by meta slash (633499) * on Friday November 07 2008, @09:07AM (#25674869)
    Of all the Elisp I've written this grep is what I most miss when I use a coworker's environment.

    (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 " * /dev/null")))
    (global-set-key "\C-x." 'my-grep)
    (global-set-key [f8] 'next-error)
    • by Anonymous Coward on Friday November 07 2008, @09:32AM (#25675121)

      Like M-x rgrep? It's builtin now.

    • by Beezlebub33 (1220368) on Friday November 07 2008, @09:46AM (#25675295)

      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:

      ;;--
      ;; This will count the number of words in a highlighted region
      ;;--

      (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)))

  • Lots of them (Score:4, Interesting)

    by Anonymous Coward on Friday November 07 2008, @09:09AM (#25674891)

    M-x tetris
    M-x doctor
    M-x yow
    M-x phases-of-moon

  • Some favorites (Score:5, Interesting)

    by bjourne (1034822) on Friday November 07 2008, @09:12AM (#25674923)

    (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.

    • Re: (Score:3, Informative)

      (global-set-key (kbd "C-c c") 'comment-dwim)

      Note: this is already bound to M-; by default.

    • Re:Some favorites (Score:4, Insightful)

      by Dolda2000 (759023) <fredrik@nOspAm.dolda2000.com> on Friday November 07 2008, @02:51PM (#25679805) Homepage

      (global-set-key "\C-z" 'undo)

      You mention undo only as such, but I'd like to add that the undo functionality really is one of Emacs' killer functions. See, when you undo a string of commands in Emacs, it doesn't just discard that part of the undo history as most (all?) other editors do, but it folds it backwards onto the undo stack, which is unbelievably useful.

      I often use it as a sort of short-term, local version control. I can type something, try it, undo it and type something else, try that, and then undo back to the first version if I weren't satisfied with the second. It also enables me to use undo as a sort of short-term memory extension in that I can hack around a bit, and then interrupt myself for a visit to the toilet or similar. Then, when I get back, I can check exactly what I was doing before I left by just undoing a bit, and then undo the undoings.

      I don't think I'd ever be able to use an editor that doesn't have Emacs' killer undo.

  • editing over ssh (Score:5, Informative)

    by tuffy (10202) on Friday November 07 2008, @09:14AM (#25674939) Homepage Journal
    Always handy when doing a bit of work remotely. Put:

    (require 'tramp)
    (setq tramp-default-method "scp")

    in one's .emacs file. Then open remote files with:

    /username@host:remote_path

    • Compiling over ssh (Score:4, Interesting)

      by hmckee (10407) on Friday November 07 2008, @03:02PM (#25679979)

      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:

      ;; remote compile support
      (setq remote-compile-host "hostname")
      (setq remote-shell-program "/usr/bin/ssh")
      (setq default-directory "/home/username/compileDir")

      No you can compile on the remote host AND bring up offending files from the compile output window with the click of a button.

  • Outlines w/ org-mode (Score:5, Interesting)

    by Khelder (34398) on Friday November 07 2008, @09:19AM (#25674979)

    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.

  • by P-Nuts (592605) on Friday November 07 2008, @09:23AM (#25675027)
    When writing scientific papers in LaTeX, there's nothing else that comes close to the power of AUCTeX with preview-latex http://www.gnu.org/software/auctex/preview-latex.html [gnu.org]. It allows you to view typeset equations inline with the rest of the document, but on moving the cursor into an equation, shows the original code. After editing, one brief command, and the new equation is typeset and displayed.
  • macros are cool (Score:3, Informative)

    by Anonymous Coward on Friday November 07 2008, @09:23AM (#25675029)

    C-x ( -- start a macro definition

    -- type some commands

    C-x ) -- end the macro

    C-x e -- execute the last macro

    For certain repetitive tasks which didn't warrent a new script I though this macro capability was awesome.

  • by Khelder (34398) on Friday November 07 2008, @09:24AM (#25675037)

    One of my favorite emacsisms a long time ago was ange-ftp, and the modern descendant, tramp, is one of my current faves. It lets you edit remote files over lots of protocols, including: ssh, scp, ftp, rsync, ftp, and smb.

    Most emacs stuff works transparently, like dired and archive browsing. When you edit a file and save it, it's automatically put back on the remote machine. I have had trouble with psvn, but that's about the only thing that I kinda expected to work that didn't.

    If you edit remote files and you use emacs, you want to start using this.

  • by david.emery (127135) on Friday November 07 2008, @09:26AM (#25675065)

    A couple of times the ability of Emacs to record keystrokes and then edit them has been a real power tool, particularly when I've needed to do very complex edits over a set of files.

    So I'd record edits on the first file, look at the keyboard recording (doing any substitutes if necessary), bind that to a key combination, then iterate over the list of files. Now if I were a good Emacs hacker, I could have automated the iteration step, too :-)

  • I replaced it (Score:3, Interesting)

    by ColonelPanic (138077) <pmk.google@com> on Friday November 07 2008, @09:27AM (#25675073)

    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]

    • Re:I replaced it (Score:4, Insightful)

      by MosesJones (55544) on Friday November 07 2008, @10:53AM (#25676247) Homepage

      Congratulations, beating out hot favourites Karl Rove and Dick Cheney you have just won single most arrogant statement of the year

      "I wrote something better"

      When you get several million users then call, until then you've got a pet project doing something that loads of people before you have done, and lots of people after you have done.

      and more people will still be using VI and Emacs

  • My fave (Score:3, Interesting)

    by scubamage (727538) on Friday November 07 2008, @09:29AM (#25675101)
    M-x hanoi - plays tower of hanoi for n number of towers
  • Modifying variables (Score:3, Informative)

    by dkf (304284) <donal.k.fellows@manchester.ac.uk> on Friday November 07 2008, @09:31AM (#25675113) Homepage

    If you've got a file that you always want to set some specific variable to a non-default value for when editing, use something like this (taken from the end of a C file...)

    /*
      * Local Variables:
      * fill-column: 78
      * c-basic-offset: 4
      * End:
      */

  • by felipecs (535825) on Friday November 07 2008, @09:38AM (#25675211) Homepage
    Take a look to http://www.emacswiki.org/emacs/EmacsNiftyTricks [emacswiki.org].
  • some of mine (Score:5, Informative)

    by flynt (248848) on Friday November 07 2008, @09:38AM (#25675213)

    1)First, ESS, Emacs speaks statistics, found at http://ess.r-project.org/ [r-project.org] . This lets you interface interactively with R, SAS, Stata, etc., all from the common Emacs interface. As a statistician, it's the one piece of software I could not do very well without!

    2) The 'ido' package, with flex matching, in my .emacs,

    (require 'ido)
    (ido-mode t)
    (setq ido-enable-flex-matching t)

    This lets you open files and switch buffers with fuzzy matching, really nice when you have lots of things open.

    See: http://www.emacsblog.org/2008/05/19/giving-ido-mode-a-second-chance/ [emacsblog.org]

    3) Make the mouse jump away when you type over it.
    (mouse-avoidance-mode 'cat-and-mouse)

    4) Open two windows side-by-side (C-x 3) one with LaTeX code, one with a pdf, then use this in your .emacs, (add-hook 'doc-view-mode-hook 'auto-revert-mode), when you compile the .tex file into PDF, the PDF automatically updates in Emacs, I used that a lot while working on my CV.

    5) The thunderbird extension that lets me compose replies in Emacs using emacsclient.

    6) org-mode http://www.org-mode.org/ [org-mode.org]

    7) preview-latex, now part of AUCTeX, this lets you see preview versions of formulae and graphics inline in your .text file, *while you edit*. Your formula is replaced by what it will look like when compiled.

    8) EmacsWiki: http://www.emacswiki.org/ [emacswiki.org]

  • Meta-/ (Score:5, Informative)

    by giblfiz (125533) on Friday November 07 2008, @09:39AM (#25675221)

    auto completes based on words that have been seen in the buffer.

  • by digitalhermit (113459) on Friday November 07 2008, @09:41AM (#25675237) Homepage

    It's often the case when you need to check the sectors on your disk for corruption, or just during hard drive testing. One of the coolest things that Emacs allows you to do is check your filesystem. For example on CentOS:

          yum -y install emacs*

    This will proceed to fill up your hard drive with tons of software until the filesystem is full.

    (I kid, I kid)

  • Emacs Lisp (Score:5, Funny)

    by tjwhaynes (114792) on Friday November 07 2008, @09:41AM (#25675241)

    Lisp is a language that CompScis see for two months at University before leaving it behind. But if you really want to learn tricks with Emacs, you should learn Emacs Lisp - I have all sorts of specials, such as "move text to marker" and modes for handling internal IBM dump formats, that would be impossible in vi.

    But if you want one quick piece of advice, here's one that should make someone smile

    M-x hippie-expand RET

    The ultimate, expand-this-thing-dammit-from-whatever-you-like completion trick.

    Cheers,
    Toby Haynes

  • by digitalderbs (718388) on Friday November 07 2008, @09:42AM (#25675249)
    I once knew a man that typed an emacs sequence, once locked in hidden, ancient papyri. Emacs conducted all of the research for his PhD, typed his dissertation, correctly formatted his bibliography (the most astounding feat of all), setup the defense with his advisory committee, presented and defended his thesis, printed, bound and submitted the dissertation.
  • align-regexp (Score:4, Interesting)

    by Just Some Guy (3352) <kirk+slashdot@strauser.com> on Friday November 07 2008, @09:44AM (#25675265) Homepage Journal

    (Pretend that '.'==' ' because Slashdot hates programmers.)

    Before:

    a.= 1
    longer.= 2
    some_variable.= None
    foo.= 'bar'

    After running M-xalign-regexp=:

    a...............= 1
    longer..........= 2
    some_variable...= None
    foo.............= 'bar'

  • Beginning of line (Score:5, Informative)

    by vslashg (209560) on Friday November 07 2008, @09:50AM (#25675361)

    My two .emacs modifications I find essential follow.

    First, turning off of obnoxious misfeatures:

    (fset 'yes-or-no-p 'y-or-n-p) ; stop forcing me to spell out "yes"
    (setq inhibit-startup-message t)
    (setq backup-directory-alist '(("." . "~/.emacs-backups"))) ; stop leaving backup~ turds scattered everywhere

    And second, stealing the beginning-of-line behavior from Dev Studio: if you invoke the command at the beginning of the line, advance to the first non-whitespace-character instead.

    (defun dev-studio-beginning-of-line (arg)
      "Moves to beginning-of-line, or from there to the first non-whitespace character.
     
    This takes a numeric prefix argument; when not 1, it behaves exactly like
    \(move-beginning-of-line arg) instead."
      (interactive "p")
      (if (and (looking-at "^") (= arg 1)) (skip-chars-forward " \t") (move-beginning-of-line arg)))
    (global-set-key "\C-a" 'dev-studio-beginning-of-line)
    (global-set-key [home] 'dev-studio-beginning-of-line)

  • by viridari (1138635) on Friday November 07 2008, @10:01AM (#25675491) Homepage

    I like emacs but I'm not ready to change over to it 100% yet.

    Is there a way to dual boot between Vista and emacs?

  • by raffe (28595) * on Friday November 07 2008, @10:07AM (#25675563) Journal

    I asked my email-pal: "UNIX or Windoze?". He replied "UNIX". I said "Ah...me too!".

    I asked my email-pal: "Linux or AIX?". He said "Linux, of course". I said "Me too".

    I asked him: "Emacs or vi". He replied "Emacs". I said "Me too. Small world."

    I asked him: "GNU Emacs or XEmacs?", and he said "GNU Emacs". I said "oh, me too."

    I asked him "GNU Emacs 19 or GNU Emacs 20"? and he said "GNU Emacs 19". I said "oh, me too."

    I asked him, "GNU Emacs 19.29 or GNU Emacs 19.34", and he replied "GNU Emacs 19.29". I said "DIE YOU OBSOLETE NOGOOD SOCIALLY MALADJUSTED CELIBATE COMMIE FASCIST DORK!", and never emailed him again.

    From an old slashdot [slashdot.org] story

  • by rockmuelle (575982) on Friday November 07 2008, @10:22AM (#25675763)

    Working with rectangular regions is a breeze in emacs (very useful for quickly swapping columns in csv-type files):

    Set the mark at the upper left of the rectangle... move the cursor the lower right...

    Kill rectangle: c-x r k

    Move somewhere else...

    Yank rectangle: c-x r y

    There are some other rectangle commands, but these are probably the two most useful "unknown" emacs commands I've come across.

    -Chris

  • by PPH (736903) on Friday November 07 2008, @01:21PM (#25677989)

    What are your favorite tricks?

    Why do you say what are my favorite tricks?

    • by cjfs (1253208) on Friday November 07 2008, @09:38AM (#25675205) Homepage Journal

      Hopefully this is the last story before we start with "Stupid pet tricks"

      Seeing as this is about emacs tricks, everything else is a subset. Including pet tricks.

      Don't get me wrong: Emacs is a great operating system -- it lacks a good editor, though.

    • by Just Some Guy (3352) <kirk+slashdot@strauser.com> on Friday November 07 2008, @09:51AM (#25675371) Homepage Journal

      This is the first content I've seen in years that appeals to Slashdot's original demographics: hardcore geeks who are passionate about the tools they use. I've picked up a few tips in this series of articles and have enjoyed hearing other people learn about "old" stuff for the first time.

      If "(Stupid) Useful $GEEK Tricks" isn't your cup of tea, then please feel free to look elsewhere [digg.com].

      • by Falkkin (97268) on Friday November 07 2008, @10:40AM (#25676037) Homepage

        Yeah, given that the *nix thread had over 2000 comments, I'd say there's a significant demand for more of these sorts of posts.

        My only regret is that I wish they'd spaced out the articles a bit, so that I had some time to digest the tricks in one article before reading the next one -- I find that if I add too many tricks to my toolbox at once I tend to forget about some of them. I guess I should just re-read the articles in another month or so. (Also an indication that these are quality articles -- there's not many discussion threads on Slashdot that I'd even consider going back and re-reading later.)

      • by alta (1263) on Friday November 07 2008, @09:51AM (#25675369) Homepage Journal

        A whole series...
        Useful(Stupid) things to say to get modded Insightful
        Useful(Stupid) things to say to get modded Interesting
        Useful(Stupid) things to say to get modded Funny
        Useful(Stupid) things to say to get modded Troll
        Useful(Stupid) things to say to get modded Redundant
        Useful(Stupid) things to say to get modded Flamebate
        Useful(Stupid) things to say to get modded Overrated
        Useful(Stupid) things to say to get modded Stupid
        Useful(Stupid) things to say to get modded Useful

        • by hardaker (32597) on Friday November 07 2008, @11:46AM (#25676863) Homepage

          Useful(Stupid) things to say to get modded Insightful

          • Emacs rocks!

          Useful(Stupid) things to say to get modded Interesting

          • Escape-: (message "Emacs rocks")

          Useful(Stupid) things to say to get modded Funny

          • Emacs: Escape Meta Alt Control Shift

          Useful(Stupid) things to say to get modded Troll

          • Emacs is better than vi

          Useful(Stupid) things to say to get modded Redundant

          • Emacs is better than vi

          Useful(Stupid) things to say to get modded Flamebate

          • Emacs is better than vi (really... Trolls are just flamers with green skin)

          Useful(Stupid) things to say to get modded Overrated

          • Emacs rocks!

          Useful(Stupid) things to say to get modded Stupid

          • What's emacs anyway?

          Useful(Stupid) things to say to get modded Useful

          • The its-all-text firefox plugin lets you edit slashdot posts in emacs
          • by 0xABADC0DA (867955) on Friday November 07 2008, @11:37AM (#25676747)

            It's a long time since Emacs has really been slow, but the jokes have long memories.

            Performance jokes are never garbage collected -- it would take too many cycles.
            Performance jokes are never garbage collected -- there's always a weak reference to them.

            I'll be here all week... or until tuesday if the alternate pickup schedule is in effect due to holiday or inclement weather.