Tell me more ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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" ]
   then
   while test $num -gt 0
    do
    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

2 Answers

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))
         (save-buffer)
         (kill-buffer)
         (indent-files (cdr files)))))

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

;; EOF ;;
share|improve this answer
1  
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 at 16:14

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.