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);
}
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
(notvim
) is installed, so it will not have the requiredident
package (as mentioned by karel).clang-format is your friend! Its easy to use and useful.
Here are some information about it.
Usage
Or:
Step by step guide
1. Horribly formatted code
main.cc
2. Magical command
3. Well formatted code
main.cc
4. Happiness
Installing
If you like it, you can install it with,
command.
Open the terminal and run:
...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 infs/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:These commands can be run as normal user. It is not necessary to be root.
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
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.
emacs :
open c file
select all
indent ( tab key)
save file
HTH