Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is vim indenting my Perl code incorrectly?

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.

like image 893
djhaskin987 Avatar asked Oct 26 '11 16:10

djhaskin987


People also ask

How do I change the indentation in vim?

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.

How do I turn off auto indent in vim?

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.

What is smart indent in vim?

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.

Is indentation required in Perl?

Though it is not necessary to use Whitespaces and Indentation in your Perl code, but it is a good practice to do the same.


2 Answers

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.

like image 119
Sinan Ünür Avatar answered Sep 18 '22 11:09

Sinan Ünür


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.

like image 42
Swiss Avatar answered Sep 19 '22 11:09

Swiss