tags:

views:

205

answers:

4

Hi,

I am rather new to VIM. I got some source code and this is a mess. At a first sight I would like at least to get a clear and organised view of the code, so I like to get it rightly formatted, I mean indented depending on the depth of the functions and so.

I wonder if it can be done with VIM, and otherwise which other commandline tools for that can you recommend.

Thanks

+4  A: 

you can do the following:

gg=G
codaddict
To clarify, =[motion] indents the region encompassed by the motion. gg moves to the beginning of the file and G moves to the end of the file.
Sarah
or start with `V` to enter visual line mode, then move down with `j` to select all the lines you want to format, then hit `=`
hasen j
A: 

Vim will definitely do this, although the results may not be perfect:

  1. First, select the entire file in visual mode: ggVG
  2. Then hit = to reindent everything.

You can learn more about the equal command with: :help =

Mike Mueller
+2  A: 

While vim is a true Swiss-knife I still prefer external tools for some jobs. This approach is some times much more intuitive and easy to remember than using the built-in equivalent.

In the case of indenting, I filter the whole file buffer through astyle. The astyle parameters are much easier to grasp in a couple of minutes, especially if you are not a vim guru. Also astyle provides much more flexibility in fine-tuning the output.

First install astyle:
# apt-get install astyle

Then inside vim:
:%!astyle (simple case - astyle default mode is C/C++)
or
:%!astyle --mode=c --style=ansi -s2 (ansi C++ style, use two spaces per indent level)
or
:1,40!astyle --mode=c --style=ansi (ansi C++ style, filter only lines 1-40)

m000
wow, this is really great, thanks a lot
Werner
A: 

A generic solution along the lines of m000's idea is to use UniversalIndentGUI as an external tool.

Mike
thanks for the info, but i prefer VIM because i have to debug code on remote machines
Werner