25

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);
}
user9993
  • 389

6 Answers6

16

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

Usage

$ clang-format file > formattedfile

Or:

$ clang-format -i file


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

mraron
  • 261
14

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, so it will not have the required ident package (as mentioned by karel).

JZL003
  • 243
Lekensteyn
  • 178,446
5

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.

karel
  • 122,292
  • 133
  • 301
  • 332
2

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

Avinash R
  • 179
2

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
1

emacs :

  • open c file

  • select all

  • indent ( tab key)

  • save file

HTH

Adam
  • 379