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

29 of 412 comments (clear)

  1. editing over ssh by tuffy · · Score: 5, Informative
    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

    --

    Ita erat quando hic adveni.

  2. Re:What is next ? by rallymatte · · Score: 2, Informative

    Well no, there were loads of bash tricks in the (Stupid) Useful Unix Tricks.

  3. AUCTeX with preview-latex by P-Nuts · · Score: 5, Informative

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

  4. macros are cool by Anonymous Coward · · Score: 3, Informative

    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.

    1. Re:macros are cool by Haeleth · · Score: 2, Informative

      This feature is built into C-x e as well: `C-x e e e e e' is exactly the same as `C-x e C-x z z z z'. :)

  5. Edit files from anywhere w/ tramp by Khelder · · Score: 4, Informative

    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.

  6. Modifying variables by dkf · · Score: 2, Informative

    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:
      */

    --
    "Little does he know, but there is no 'I' in 'Idiot'!"
  7. Re:grep and emacs integration by Anonymous Coward · · Score: 5, Informative

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

  8. Open jars, wars, ears with archive-mode by wytten · · Score: 2, Informative

    Configure emacs auto-mode-alist to open Java archives with archive-mode, and then edit deployment descriptors contained in the archive without having to extract the archive. Bliss.

  9. Re:Some favorites by Just+Some+Guy · · Score: 2, Informative

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

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

    --
    Dewey, what part of this looks like authorities should be involved?
  10. Mistake made by jez9999 · · Score: 2, Informative

    I think this article has been incorrectly filed; please fix, editors. It's been put under "News".

  11. there's a whole wiki to answer this question by felipecs · · Score: 5, Informative
  12. some of mine by flynt · · Score: 5, Informative

    1)First, ESS, Emacs speaks statistics, found at http://ess.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/

    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/

    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/

  13. Meta-/ by giblfiz · · Score: 4, Informative

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

  14. Obligatory link by fprintf · · Score: 3, Informative
    --
    This post brought to you by your friendly neighborhood MBA.
  15. Re:Not much of a trick... BUT... by Anonymous Coward · · Score: 1, Informative

    you can actually do something similar with Konqueror:
    Window -> Split View Left/Right or Split View Top/Bottom.

    it also comes with keyboard shortcuts.

  16. zippy the pinhead! by sl0ppy · · Score: 2, Informative

    M-x yow

    and of course, the ever relevant:

    M-x psychoanalyze-pinhead

  17. Beginning of line by vslashg · · Score: 5, Informative

    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)

  18. Re:Editing recorded keystrokes by david.emery · · Score: 2, Informative

    This is covered in Chapter 10 of that invaluable reference "Learning GNU Emacs" (now in its 3rd edition, I need to 'upgrade' my 1st edition so the chapter organization might have changed) (http://oreilly.com/catalog/9780596006488/index.html)

    You can do M-x apropos macro to get info. There's an edit-kbd-macro command as well as a whole bunch of other useful things.

    dave

  19. Two tips for trailing whitespace by stevey · · Score: 2, Informative

    There are a lot of things that you can do with Emacs, but I find the two simplest to share would be related to trailing whitespace:

    ;; Show trailing whitespace by default
    (when (>= emacs-major-version 21)
    (setq-default show-trailing-whitespace t))

    ;;
    ;; kill trailing white space on save
    ;;
    (add-hook 'write-file-hooks
    'delete-trailing-whitespace)

  20. Rectangle Cut and Paste by rockmuelle · · Score: 4, Informative

    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

  21. Re:Favorite Acronym by Simon+Brooke · · Score: 2, Informative

    The funny thing is, those jokes about Emacs being slow date back to the 80s. These days, I find that Emacs is about the fastest app to start on a modern Linux distribution.

    The rest of userland has gotten fatter and fatter, but Emacs has stayed about the same.

    Since the story asks for favorite things about Emacs, I will just add: Gnus. The best email app bar none.

    Back in the early nineties, I had an Acorn R140 with 4Mb of core; it ran RISC iX, which was basically BSD 4.2. It could just about run X11; and it could happily run a full Emacs development session (but not under X11). Pretty soon after that I got an R280, which had 8Mb of core; and that ran Emacs under X11 really nicely...

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

    --
    I'm old enough to remember when discussions on Slashdot were well informed.
  22. Browsing Code: ecb-mode and etags by jonaskoelker · · Score: 2, Informative

    Whenever you download a bunch of code and want to dip your feet in it, it helps getting a good overview.

    To that end, ecb-mode can be greatly helpful. It displays navigable directory trees, a list of all symbols defined in your current file, a list of recently visited files, and a compile window. Double-click anything to jump to it, quite nice to use. The symbol list is really neat; I've thrown C, python and makefiles at it, much to my satisfaction. Haskell support is a bit rudimentary, but still very useful. It says it handles perl and TeX specially [they can't be parsed without being partially executed]. I don't know how well.

    If you just want to follow symbols around, use etags [use it anyways]. Run etags in your source root (zsh% etags **/*.[ch]), cursor-over a symbol, press M-. to jump to it.

  23. Re:grep and emacs integration by gowen · · Score: 5, Informative

    Isn't that usually spelled

    M-| wc

    Meta-pipe is a great one -- it's "pipe region to external command" (M-x shell-command-on-region)

    --
    Athletic Scholarships to universities make as much sense as academic scholarships to sports teams.
  24. Re:Questions for Emacs People by An+ominous+Cow+art · · Score: 2, Informative

    The problem was, there was some key combination I was hitting on accident (probably since I am used to nano keybindings) that was causing me to delete large chunks of my document (usually everything above my current view). I didn't realize it, then I would save and have to restore the file from backup. This happened several times. I think it was happening as I was saving, but could never trace back my error.

    My guess would be that you were pressing ctrl-w, "kill-region", which removes everything between your cursor and the 'mark', and places it in the kill buffer. As with ctrl-k, pressing ctrl-y will 'yank' it out and insert it (insert juvenile jokes here :-)).

  25. Re:calc and pictures by martinw89 · · Score: 2, Informative

    (does vi do calculus? I think not).

    No, but it does edit files pretty well.

  26. Re:Lots of them by Anonymous Coward · · Score: 1, Informative

    M-x where-is, also known as C-h w :)

  27. Re:vi is for building emacs by StandardDeviant · · Score: 2, Informative

    Solaris, it's been said, has a great kernel and an utterly shit userland (i.e. the tools you actually interact with). I can almost guarantee you that at some point the admins of your system have gotten pissed off at the Sun-shipped utils (from hell's heart, sun tar, I stab at thee) and have installed some variety of 3rd-party-freeware utilities on the system (either Sun's freeware, Sun Freeware [different], or Blastwave freeware come to mind). You might try looking, if you haven't yet, in /opt and /usr/local to see if bin dirs exist under there somewhere as these are common locations for these packages. If so, adding them into your shell's $PATH will make interactive use a lot more pleasant.

  28. Re:Wanted: su for a buffer by dhTardis · · Score: 2, Informative

    Tramp will also let you use sudo or su to edit your files without SSH: C-x C-f /su::/etc/motd, for example.