Emacs Tip of the Day #Indenting A Region of Code#

by

We often have to work with code written by other people, and though it would be nice if people did their indentation on their own, many people just aren’t bothered. Life is too short to waste time re-indenting somebody’s badly written code. Let Emacs come to your rescue.
Select the offending piece of code, and run the command

M-x indent-region
alternatively,
C-M-\

Voila!

*Update*

Came across something a bit more useful. Here is a function to re-indent the entire buffer (without having to select it first)

(defun iwb ()
“indent whole buffer”
(interactive)
(delete-trailing-whitespace)
(indent-region (point-min) (point-max) nil)
(untabify (point-min) (point-max)))

Just add it to your .emacs file. Next time you open a buffer, execute M-x iwb, and get properly indented code.

[hat-tip]: M-x all-things-emacs

Advertisements

Tags: ,

8 Responses to “Emacs Tip of the Day #Indenting A Region of Code#”

  1. @$%deja vu$% Says:

    On a similar note, when many programmers code as team, it becomes difficult to track different coding styles.

    So, linux programmers have come up with a standard coding practice documented in Documentation/Coding_Style (or something similar) in the linux sources. Add that too in your .emacs file.

    Also, you can write
    /* -* {func-name} -*- */
    at the beginning of your file, to avoid typing in “M-x {func-name}” every time you start emacs!

    Enjoy!!

  2. @$%deja vu$% Says:

    Also, reindenting a file is generally not a very good idea because it shows up in the diff 🙂
    (And people here get VERY irritated when they have to review unnecessary code… just speaking from experience :P)

  3. Vedang Says:

    True.. I was just thinking about how screwed I’m going to get as the WHOLE damn code base is going to show up in the diff…
    But I cannot work with poorly indented files, especially when indentation styles change _within_ the same file!
    The way I see it, I’m going to re-indent the code I’m working on, as well as a backup copy of the original code, so that the end diff will only contain my changes..

  4. Vedang Says:

    and great tip about the {func-name}! thanks!

  5. CDK Says:

    thanks again…ur posts are not coming as regularly as i expected but when they do come …. they are very useful

  6. Dan Lewis Says:

    You might also consider
    (add-hook ‘find-file-hook ‘(lambda () (progn (my-untab) (my-indent)) ))

    I use it to change tabs to spaces in every file I visit. The only place it caused a problem was in hosing up the tabs in a makefile.

    You could get a little more fine-grained and use ‘cc-mode-hook or other language-mode hooks to do different standard indentations.

  7. Vedang Says:

    @dan
    thanks! must try this…

    @cdk
    they are as much for my ready reference as for helping everyone else! 🙂

  8. @$%deja vu$% Says:

    I apologize for a typo in an earlier comment.

    This would be the correct line:
    /* -*- {func-name} -*- */

    eg. write
    /* -*- linux-c -*- */
    (after u copy the linux coding style func to your .emacs file …

    enjoy!

    @ved
    Really, u should post regularly !

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: