Slashdot is powered by your submissions, so send in your scoop

 



Forgot your password?
typodupeerror
×
GNU is Not Unix Programming Software IT Technology

(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' ;-)"
This discussion has been archived. No new comments can be posted.

(Stupid) Useful Emacs Tricks?

Comments Filter:
  • by meta slash ( 633499 ) * on Friday November 07, 2008 @10: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)
  • Lots of them (Score:4, Interesting)

    by Anonymous Coward on Friday November 07, 2008 @10: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 @10:12AM (#25674923) Homepage Journal

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

    by Khelder ( 34398 ) on Friday November 07, 2008 @10: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.

  • I replaced it (Score:3, Interesting)

    by ColonelPanic ( 138077 ) on Friday November 07, 2008 @10: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]

  • My fave (Score:3, Interesting)

    by scubamage ( 727538 ) on Friday November 07, 2008 @10:29AM (#25675101)
    M-x hanoi - plays tower of hanoi for n number of towers
  • Re:Some favorites (Score:3, Interesting)

    by patro ( 104336 ) on Friday November 07, 2008 @10:43AM (#25675263) Journal

    If we consider external packages too:

    The QuickSilver for Emacs: anything.el [emacswiki.org]

  • align-regexp (Score:4, Interesting)

    by Just Some Guy ( 3352 ) <kirk+slashdot@strauser.com> on Friday November 07, 2008 @10: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'

  • by Anonymous Coward on Friday November 07, 2008 @10:44AM (#25675279)

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

  • by Beezlebub33 ( 1220368 ) on Friday November 07, 2008 @10: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)))

  • by War Geese ( 865989 ) on Friday November 07, 2008 @10:47AM (#25675315)

    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)

    by Anonymous Coward on Friday November 07, 2008 @10:50AM (#25675355)

    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.
    "C-x ." will set a fill prefix, i.e., if you need to reparagraph something with the " >" prefix for mail replies

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

  • by Dolda2000 ( 759023 ) <fredrik.dolda2000@com> on Friday November 07, 2008 @10:59AM (#25675463) Homepage
    Of all the elisp I've written, this is the one I miss the most when I don't use my own EMACS:

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

    by Anonymous Coward on Friday November 07, 2008 @11:19AM (#25675709)

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

  • very useful and extremely simple. Assuming [M] means "Meta-key".

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

    by haystor ( 102186 ) on Friday November 07, 2008 @11:34AM (#25675933)

    C-x r k ;; kill-rectangle
    C-x r y ;; yank-rectangle

    Cut and paste columns of text.

  • by hardaker ( 32597 ) on Friday November 07, 2008 @12:36PM (#25676731) Homepage
    By far one of the best enhancement packages to come out for (X)Emacs in a long time is org-mode [orgmode.org]. It's a feature-full organization and agenda system that all operates on plain text files that are easy to read and write.
  • calc and pictures (Score:3, Interesting)

    by porpnorber ( 851345 ) on Friday November 07, 2008 @12:50PM (#25676921)
    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).
  • Comment removed (Score:3, Interesting)

    by account_deleted ( 4530225 ) on Friday November 07, 2008 @01:41PM (#25677447)
    Comment removed based on user account deletion
  • by i.of.the.storm ( 907783 ) on Friday November 07, 2008 @03:29PM (#25679313) Homepage
    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.
  • Compiling over ssh (Score:4, Interesting)

    by hmckee ( 10407 ) on Friday November 07, 2008 @04: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.

  • by Risen888 ( 306092 ) on Friday November 07, 2008 @04:05PM (#25680041)

    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)

    by Haeleth ( 414428 ) on Friday November 07, 2008 @04:28PM (#25680421) Journal

    Notepad has precisely one useful trick, documented here [microsoft.com].

  • Re:iso-accents-mode (Score:3, Interesting)

    by Haeleth ( 414428 ) on Friday November 07, 2008 @04:49PM (#25680725) Journal

    This is the easiest way I know to type accented characters on a US keyboard.

    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.

  • by Haeleth ( 414428 ) on Friday November 07, 2008 @04:51PM (#25680755) Journal

    "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"...)

  • by Haeleth ( 414428 ) on Friday November 07, 2008 @05:02PM (#25680951) Journal

    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.

Love may laugh at locksmiths, but he has a profound respect for money bags. -- Sidney Paternoster, "The Folly of the Wise"

Working...