You are not logged in.

#1 2011-06-18 14:28:34

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,673
Website

vim newbie question about formatting code

I just started learning vim and am wondering how I can get it to auto format some code.  For example:

check() {
for i in $nodes; do
ping -c 1 $i > /dev/null
if [ $? -eq 0 ]; then
echo "Looks like $i is up so initializing the cluster on it."
ssh $i "\$HOME/bin/cluster-setup.sh"
fi
done
}

I have tabstop=3 in my ~/.vimrc and I'd just like to apply formatting to that code block so it looks like:

check() {
for i in $nodes; do
   ping -c 1 $i > /dev/null
      if [ $? -eq 0 ]; then
         echo "Looks like $i is up so initializing the cluster on it."
         ssh $i "\$HOME/bin/cluster-setup.sh"
      fi
done
}

I read about =i{ but it has no effect for me.

Thanks!

Last edited by graysky (2011-06-18 14:32:18)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#2 2011-06-18 14:51:02

jiyuu
Member
Registered: 2010-04-13
Posts: 63

Re: vim newbie question about formatting code

You need to put in your .vimrc

set autoindent
filetype indent on

Offline

#3 2011-06-18 19:46:14

the_isz
Member
Registered: 2009-04-14
Posts: 280

Re: vim newbie question about formatting code

Also, be sure to read the help topics 'cinoptions', cinoptions-values and
C-indenting. If you should run into any problems, the guys over at freenode's
#vim channel are very helpful and eager to help out smile

Edit:

Oops, just realized this is shell script code we're talking about... My bad.

Last edited by the_isz (2011-06-18 19:47:14)

Offline

#4 2011-06-20 10:23:16

chrismerck
Member
Registered: 2010-08-31
Posts: 5

Re: vim newbie question about formatting code

graysky,

If you want vim to autoindent while typing, customize your ~/.vimrc file. I posted mine here: http://linuxandsuch.wordpress.com/2011/06/19/my-vimrc/ a few days back. I recommend all the options.

However, if you want to indent an already existing file use the indent program. Install with

sudo pacman -S indent

And then, to indent your file, run:

indent myfile.c

I'd recommend reading the manpage for indent to learn to tweak it to your coding style though smile.

Offline

Board footer

Powered by FluxBB