Slashdot Mirror


(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' ;-)"

9 of 412 comments (clear)

  1. grep and emacs integration by meta+slash · · Score: 5, Interesting

    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)

    1. Re:grep and emacs integration by Beezlebub33 · · Score: 3, 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:

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

      --
      The more people I meet, the better I like my dog.
  2. Lots of them by Anonymous Coward · · Score: 4, Interesting

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

    1. Re:Lots of them by haystor · · Score: 3, Interesting

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

      Cut and paste columns of text.

      --
      t
  3. Some favorites by bjourne · · Score: 4, 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.

  4. Outlines w/ org-mode by Khelder · · Score: 4, 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.

  5. align-regexp by Just+Some+Guy · · Score: 3, Interesting

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

    --
    Dewey, what part of this looks like authorities should be involved?
  6. Compiling over ssh by hmckee · · Score: 3, 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:

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

  7. Re:Edit files from anywhere w/ tramp by Risen888 · · Score: 3, 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.

    --
    Hey, I finally got my first freak! Took you long enough!