I have a subroutine in Perl that should be indented like this:
sub GetFiles
{
my $pwd = shift;
my @input = @_;
my @returned;
my @DirectoryContent = &GetContentInformation(@input);
foreach (@DirectoryContent)
{
my %current = %{$_};
if ($current{'info'} =~ /<DIR>/)
{
my $RecurseDir = &GetRecurseDir($pwd, \%current);
push(@returned, &GetFiles($RecurseDir,
&GetDirectoryContents($RecurseDir)));
}
else
{
# clean up the data
my $size = $current{'info'};
# filesize will be in number of bytes
# remove file separators
#$size =~ s/,//g;
my $extension = &GetFileExtension($current{'name'});
delete($current{'info'});
$current{'size'} = $size;
$current{'extension'} = $extension;
# push(@returned, \%current);
}
}
@returned;
}
But when I press =%
(yes, cindent
is on) with the cursor on the starting bracket of the subroutine block, it indents it like this:
sub GetFiles
{
my $pwd = shift;
my @input = @_;
my @returned;
my @DirectoryContent = &GetContentInformation(@input);
foreach (@DirectoryContent)
{
my %current = %{$_};
if ($current{'info'} =~ /<DIR>/)
{
my $RecurseDir = &GetRecurseDir($pwd, \%current);
push(@returned, &GetFiles($RecurseDir, &GetDirectoryContents($RecurseDir)));
}
else
{
# clean up the data
my $size = $current{'info'};
# filesize will be in number of bytes
# remove file separators
#$size =~ s/,//g;
my $extension = &GetFileExtension($current{'name'});
delete($current{'info'});
$current{'size'} = $size;
$current{'extension'} = $extension;
# push(@returned, \%current);
}
}
@returned;
}
Why does it do that? How can I fix it?
EDIT: It should be noted that I am using gvim 7.3 on Windows.
Fix indentation in the whole fileStart in the top of a file (to get there, press gg anywhere in the file.). Then press =G , and Vim will fix the indentation in the whole file. If you don't start in the beginning of the file, it will fix indentation from current line to the bottom of file.
To turn off autoindent when you paste code, there's a special "paste" mode. Then paste your code. Note that the text in the tooltip now says -- INSERT (paste) -- . After you pasted your code, turn off the paste-mode, so that auto-indenting when you type works correctly again.
autoindent essentially tells vim to apply the indentation of the current line to the next (created by pressing enter in insert mode or with O or o in normal mode. smartindent reacts to the syntax/style of the code you are editing (especially for C). When having it on you also should have autoindent on.
Though it is not necessary to use Whitespaces and Indentation in your Perl code, but it is a good practice to do the same.
Maybe this is magical thinking, but … I used to have:
filetype plugin on
filetype indent on
in my _vimrc
(on Windows XP, self-compiled gvim
, various versions), and I would get all sorts of interesting indentation problems in Perl, LaTeX, and HTML files.
Now, I have
filetype indent on
filetype plugin on
and everything seems to be hunk-dory. YMMV.
Also, I highly recommend Andy Lester's vim-perl.
cindent
is specific to the c
language and is broken when used with a lot of other languages. What you probably want to use is filetype plugin indent on
. You can add that to your .vimrc
and vim will figure out the correct syntax/indentation for most languages out of the box. You can also add syntax/indentation guides fairly easily if vim doesn't already have them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With