Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm using Ubuntu 12.04. I do not have any root or sudo privileges as this is a company machine.

Is there, in a normal installation of Ubuntu 12.04, any terminal program that I can use to turn ugly malformed source code that lacks any indentation into nice looking code?

Again, I can't install any packages so I need one that already comes with Ubuntu, if such a thing exists.

For example:

    int main()
    {
test(1);
another_function(1);
}

And then convert it to:

int main()
{
    test(1);
    another_function(1);
}
share|improve this question
    
What do you use to see or edit the code? Presumably you want it to be indented in your editor right? Or do you want to do this from the command line for multiple files? –  terdon Apr 16 at 11:11
    
You mean like an online tool? –  Braiam Apr 16 at 16:26
    
Gedit will indent IIRC, and kate will highlight and indent based on filetype if you have the ability to switch to KDE –  E Carter Young Apr 16 at 22:44

6 Answers 6

If you have the vim editor installed, open the file with vim file.c and type ==G to indent the file from begin to end. Then save it with :wq.

On default installations, vi (not vim) is installed, this requires the ident package mentioned by karel.

share|improve this answer

By default nano should be installed in ubuntu.

You can use nano -i file to edit with auto-indent enabled.

This may not change existing lines, for that you may have to manually indent it.

See: http://www.nano-editor.org/dist/v2.0/nano.html

share|improve this answer

Open the terminal and run:

sudo apt-get install indent
indent -linux -l120 -i4 -nut unformatted-source-code.cpp

...where unformatted-source-code.cpp is the file that has unformatted C++ source code, such as the code in your example.

Or if you can't install it, you can download the package with apt-get download indent and extract it: dpkg-deb -x indent*.deb fs/, the indent binary is located in fs/usr/bin/ where fs is any directory in your home directory. If you copy the unformatted-source-code.cpp file to the same place, fs/usr/bin/ , then the commands to indent the code from the terminal are:

cd path/to/fs/usr/bin/  # change directories to the location of "indent" executable
./indent -linux -l120 -i4 -nut unformatted-source-code.cpp

These commands can be run as normal user. It is not necessary to be root.

share|improve this answer
2  
I do not have any root or sudo privileges as this is a company machine. –  user9993 Apr 16 at 11:03
4  
@user9993 You can download the package with apt-get download indent and extract it: dpkg-deb -x indent*.deb fs/, the indent binary is located in fs/usr/bin/. –  Lekensteyn Apr 16 at 11:08

astyle and indent spring to mind, but a default Ubuntu install doesn't include either. Of course, if you have a C compiler, you can compile them and install them in your own PATH somewhere.

# Something like -
./configure --prefix=$HOME/tools
make
make install
PATH=$PATH:$HOME/tools/bin
share|improve this answer

clang-format is your friend! Its easy to use and useful.
Here are some information about it.

Usage

$ clang-format file > formattedfile


Step by step guide

1. Horribly formatted code

#include <iostream>
  using namespace std;
    int main() {
         cout << "Oh";
      cout << "clang format rulez!";       
             }

main.cc

2. Magical command

$ clang-format main.cc > main.cc 


3. Well formatted code

#include <iostream>
using namespace std;
int main() {
  cout << "Oh";
  cout << "clang format rulez!";
}

main.cc

4. Happiness

Installing

If you like it, you can install it with,

$ sudo apt-get install clang-format

command.

share|improve this answer

emacs :

  • open c file

  • select all

  • indent ( tab key)

  • save file

HTH

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.