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);
}
3
  • 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, 2014 at 11:11
  • You mean like an online tool?
    – Braiam
    Apr 16, 2014 at 16:26
  • 1
    Gedit will indent IIRC, and kate will highlight and indent based on filetype if you have the ability to switch to KDE
    – eyoung100
    Apr 16, 2014 at 22:44

6 Answers 6

15

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.

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

4

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.

2
  • 2
    I do not have any root or sudo privileges as this is a company machine.
    – user9993
    Apr 16, 2014 at 11:03
  • 5
    @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, 2014 at 11:08
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

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

You must log in to answer this question.

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