Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a large shell script that uses a mixture of spaces and tabs. I want to re-indent the whole file based on syntax, like Eclipse's Format. Is there a program (beautify ?) that will do this ?

I'm having a hard time figuring out the logic with everything jammed together e.g.

   if [ "$CANCELLATION" ]
   while test $num -gt 0
    if [ "$cjb" -gt 0 ]

Learned how to call functions in Vim but that didn't work.

Emacs - lost all the newlines

share|improve this question

Emacs can do that:

  • load the file into Emacs
  • press Ctrl-space at the top of the file
  • move the cursor to the bottom of the file
  • press Alt-x and type untabify then return
  • press Alt-x and type indent-region then return

This will get rid of tabs and indent everything properly.

If you need to do this more often and do not use Emacs as your editor, you might want to pack it all into a script:

#!/usr/bin/emacs --script

(setq require-final-newline 'visit)

(defun indent-files (files)
  (cond (files
         (find-file (car files))
         (untabify (point-min) (point-max))
         (indent-region (point-min) (point-max))
         (indent-files (cdr files)))))

(indent-files command-line-args-left)

;; EOF ;;
share|improve this answer
not really an emacs guy - but if the super shell vim doesn't work. I'll try that. – jsymolon Sep 10 '09 at 14:21
+1 for emacs! :) – warren Sep 10 '09 at 15:07
M-> is the usual binding for end-of-buffer. Because taking your hands off the keyboard is evil. – dmckee Sep 10 '09 at 21:52
Also marking the entire buffer can be done with C-x h, which runs mark-whole-buffer. – Victor Feb 18 '13 at 16:14

If you use Vim there is Super Shell Indent : Improved indentation for shell scripts.

share|improve this answer

I am using it (the most recent version V1.6 ), it still indent the line with TAB(8 char wide) during my pressing "== "keys

share|improve this answer

You probably want Super Shell Indent. Save it to ~/.vim/indent and when you're next in vim, execute :source ~/.vim/indent/sh.vim

You also probably want to setup file based smart indenting in your .vimrc

" Turn on smart indenting
filetype indent on
set smartindent

If you don't mind vim messing with your formatting, add this line to vimrc too.

" When you load/save a shell script, auto indent it
autocmd BufRead,BufWritePre *.sh normal gg=G

Anyway, here is what you've been waiting for. When you're next editing a shell script in vim, assuming you've turned on smart indenting and installed super shell ident, just hit the follow keys to reindent your script: gg=G

share|improve this answer

This sh.vim is really cool, but I had the word "case" in a comment which let the indentation stumble at this point. My workaround was to replace the word "case" with something else.

Otherwise it worked fine.

share|improve this answer

In Vi/Vim once you've set the correct file syntax (:set syntax=sh), you can press: gg=G (or 1G=G) to correct indentation of the entire file.

Command-line example:

ex +"norm gg=G" -cwq foo.txt

See also: Re-indenting badly indented code at Vi SE

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .