How to fix broken automatic indentation in vim

752 views
Skip to first unread message

Didlybom

unread,
Apr 27, 2010, 12:29:03 PM4/27/10
to vim...@googlegroups.com

Hi,

I am trying to use vim 7.2 (on Windows XP) to automatically indent and
format some VHDL and Matlab code. To do so I am trying to use the "gg=G"
command. However this does not work properly. The code is not properly
indented at all.

To give you an example, I had the following source code, which was already
properly indented:

%%%%%%%%%%% BEGIN_OF_CODE %%%%%%%%%%%%
% This function is based on the code_g_generator() function
function [v_code] = get_code(n_code_number)
% There is no need to clear the persistent variables in this function
mlock
%% Initialize the internal variables
persistent n_fifo_top;
if isempty(n_fifo_top)
n_fifo_top = 1;
end

N_MEMORY_SIZE = 4;
if n_code_number > 4
c_saved_code_fifo = {-1*ones(1, N_MEMORY_SIZE)};
end
%%%%%%%%%%% END_OF_CODE %%%%%%%%%%%%

If I use the "gg=G" command I get:

%%%%%%%%%%% BEGIN_OF_CODE %%%%%%%%%%%%
% This function is based on the code_g_generator() function
function [v_code] = get_code(n_code_number)
% There is no need to clear the persistent variables in this function
mlock
%% Initialize the internal variables
persistent n_fifo_top;
if isempty(n_fifo_top)
n_fifo_top = 1;
end

N_MEMORY_SIZE = 4;
if n_code_number > 4
c_saved_code_fifo = {-1*ones(1, N_MEMORY_SIZE)};
end
%%%%%%%%%%% END_OF_CODE %%%%%%%%%%%%

As you can see, in this example Vim incorrectly indents the code after the
first "if" block. For other files I get similar problems (although not
necessarily on the first if block).

For VHDL files I get similar problems.

I have tried using different combinations of the autoindent, smartindent and
cindent settings. After going through these forums I have also made sure
that the "syntax", "filetype", "filetype indent" and "filetype plugin
indent" are set to on. Still, it does not work. Also, if I do "set syntax?"
I get "matlab" for matlab files and "vhdl" for vhdl files which is correct.
And if I do "set indentexpr?" I get "GetMatlabIndent(v:lnum)" for matlab
files and "GetVHDLindent()" for vhdl files.

To try to isolate the problem (and ensure that it is not due to one of the
vim plugins that I have installed) by doing a fresh install of VIM on a
different computer (in which VIM had never been installed before). On that
computer I get the same sort of problems (that is why I do not think that I
need to give you the .vimrc, but if you need it I can upload it here too).

Any ideas? Thank you in advance!

Didlybom

--
View this message in context: http://old.nabble.com/How-to-fix-broken-automatic-indentation-in-vim-tp28379120p28379120.html
Sent from the Vim - General mailing list archive at Nabble.com.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

Subscription settings: http://groups.google.com/group/vim_use/subscribe?hl=en

Gary Johnson

unread,
Apr 27, 2010, 1:44:40 PM4/27/10
to vim...@googlegroups.com
You have the same problem on both computers and you're using the
same ~/.vimrc on both? Hmm.

It works fine for me. I copied your example code into a file named
foo.m. Then I started vim as

vim -N -u NONE

to avoid loading any configuration files at all. Within vim I
executed

:filetype plugin indent on
:e foo.m
gg=G

The result was that both indented lines were re-indented by 8
spaces, which makes sense since 'shiftwidth' defaults to 8.

I don't know what's specifically wrong with your ~/.vimrc since I
don't know what's in it. The indent plugin should override your
'cindent' and 'smartindent' settings, so those shouldn't be a
problem. As I demonstrated above, you don't need anything in your
~/.vimrc other than

filetype plugin indent on

and

set shiftwidth=4

to get the indentation you want.

HTH,
Gary

Ben Fritz

unread,
Apr 27, 2010, 2:23:59 PM4/27/10
to vim_use


On Apr 27, 11:29 am, Didlybom <didly...@gmail.com> wrote:
> I have tried using different combinations of the autoindent, smartindent and
> cindent settings. After going through these forums I have also made sure
> that the "syntax", "filetype", "filetype indent" and "filetype plugin
> indent" are set to on. Still, it does not work. Also, if I do "set syntax?"
> I get "matlab" for matlab files and "vhdl" for vhdl files which is correct.
> And if I do "set indentexpr?" I get "GetMatlabIndent(v:lnum)" for matlab
> files and "GetVHDLindent()" for vhdl files.
>

I know filetype indent plugins *should* override these options, but in
general you should NOT set cindent in your .vimrc, you should rely on
the filetype indent rules to set it for you. You should also not have
smartindent on in combination with any other indent option. I would
say you should not have smartindent on at all...but I know there are a
few people who actually prefer the behavior of this option.
Regardless, it's supposed to work *instead* of, not in addition to,
filetype indent rules.

Your indentexpr values indicate the correct rules are being applied,
but make sure cindent, smartindent, and possibly even autoindent are
all turned off as well. Also check that 'equalprg' is empty.

Other than that, it may be an error in your indent scripts (though
this is unlikely). Are your runtime files fairly recent?

Didlybom

unread,
Apr 28, 2010, 5:36:09 AM4/28/10
to vim...@googlegroups.com

Gary,

I do not have the same ~/.vimrc on both computers. In the first computer I
have a heavily modified .vimrc file. However, on the second computer I did a
fresh Vim 7.2 install. I actually went to www.vim.org and downloaded the vim
installer from there. Thus the .vimrc file on the second computer should be
the default.

However, I did try what you suggested. I executed "vim -N -u NONE", set the
"filetype plugin indent on" setting and loaded the example file. When I
issued the "gg=G" command the indentation worked fine.

However, I then tried to indent a VHDL file. This is a simple VHDL which I
put online. Hopefuly you can access it through:
http://old.nabble.com/file/p28386622/example.vhd example.vhd .

Again, I opened vim using "vim -N -u NONE", set "filetype indent plugin on"
and opened the file. I used the "gg=G" command and let vim indent the file.
Unfortunately I found that vim did not do a good job and that the
indentation was not good at all :-(

I saved and uploaded the file after indenting into
http://old.nabble.com/file/p28386622/example_indented.vhd
example_indented.vhd . If you can open it you will see that the "type
secondStruct" is not indented properly. Also, the comments (beginning with
"--") are not indented right and the final "END;" is not indented properly
either.

Could you give it a try and see if you have the same problem that I do?

I think that perhaps I originally had 2 problems. One was probably caused by
something on my .vimrc setup, but there seems to be something else.

Could there be some problem related to the format of the file? Some of these
files have been edited in Windows and in Linux. Could there be some mix of
line endings that could confuse vim somehow? When I open the file I do not
see any "^M" symbol on the line endings so I assumed that their format was
correct.

Thanks,

Didly
--
View this message in context: http://old.nabble.com/How-to-fix-broken-automatic-indentation-in-vim-tp28379120p28386622.html
Sent from the Vim - General mailing list archive at Nabble.com.

Didlybom

unread,
Apr 28, 2010, 5:38:00 AM4/28/10
to vim...@googlegroups.com

Ben,

On the second system in which I tried this I downloaded the Vim 7.2
installer from www.vim.org so I assume that it is as recent as it gets.

Cheers,

Didly
--
View this message in context: http://old.nabble.com/How-to-fix-broken-automatic-indentation-in-vim-tp28379120p28386632.html
Sent from the Vim - General mailing list archive at Nabble.com.

Gary Johnson

unread,
Apr 28, 2010, 11:29:17 AM4/28/10
to vim...@googlegroups.com
On 2010-04-28, Didlybom wrote:
> Gary,
>
> I do not have the same ~/.vimrc on both computers. In the first computer I
> have a heavily modified .vimrc file. However, on the second computer I did a
> fresh Vim 7.2 install. I actually went to www.vim.org and downloaded the vim
> installer from there. Thus the .vimrc file on the second computer should be
> the default.
>
> However, I did try what you suggested. I executed "vim -N -u NONE", set the
> "filetype plugin indent on" setting and loaded the example file. When I
> issued the "gg=G" command the indentation worked fine.
>
> However, I then tried to indent a VHDL file. This is a simple VHDL which I
> put online. Hopefuly you can access it through:
> http://old.nabble.com/file/p28386622/example.vhd example.vhd .
>
> Again, I opened vim using "vim -N -u NONE", set "filetype indent plugin on"
> and opened the file. I used the "gg=G" command and let vim indent the file.
> Unfortunately I found that vim did not do a good job and that the
> indentation was not good at all :-(
>
> I saved and uploaded the file after indenting into
> http://old.nabble.com/file/p28386622/example_indented.vhd
> example_indented.vhd . If you can open it you will see that the "type
> secondStruct" is not indented properly. Also, the comments (beginning with
> "--") are not indented right and the final "END;" is not indented properly
> either.
>
> Could you give it a try and see if you have the same problem that I do?

I don't know VHDL, but I see those problems you pointed out and it
doesn't seem right to me, either.

> I think that perhaps I originally had 2 problems. One was probably caused by
> something on my .vimrc setup, but there seems to be something else.
>
> Could there be some problem related to the format of the file? Some of these
> files have been edited in Windows and in Linux. Could there be some mix of
> line endings that could confuse vim somehow? When I open the file I do not
> see any "^M" symbol on the line endings so I assumed that their format was
> correct.

As long as you don't see any ^Ms, the file uses only Unix or only
DOS line endings and you should be OK.

I don't know what the problem is, but it seems to be in the indent
plugin and not with your configuration. I looked briefly at the
plugin, $VIMRUNTIME/indent/vhdl.vim, and didn't see anything obvious
in the way of settings that would affect these issues. You might
have a look and see what you think. The author's name and address
are in that file so you might try contacting him directly. That
file says that it supports the "'93 syntax" if that means anything
to you. The plugin I have also says that it is version 1.54, last
changed on 2007 Aug 17.

Regards,

Didlybom

unread,
Apr 28, 2010, 12:36:47 PM4/28/10
to vim...@googlegroups.com

Gary,

thanks for your effort in trying to help me find the problem. Did you try to
format the file using your vim installation? I'd like to confirm that the
problem really is not with my vim installation but with the plugin itself as
you suspect. If the problem is with the plugin I will try to contact the
author and hopefully he'll be able to help me.

BTW, the file with which I have the problem uses the VHDL-93 syntax (which
is the latest widely used syntax, BTW).

Cheers,

Didly
--
View this message in context: http://old.nabble.com/How-to-fix-broken-automatic-indentation-in-vim-tp28379120p28391063.html
Sent from the Vim - General mailing list archive at Nabble.com.

Ben Fritz

unread,
Apr 28, 2010, 12:47:40 PM4/28/10
to vim_use


On Apr 28, 4:38 am, Didlybom <didly...@gmail.com> wrote:
> On the second system in which I tried this I downloaded the Vim 7.2
> installer fromwww.vim.org so I assume that it is as recent as it gets.
>

Not quite. This installer is a couple years old and has not been
updated with any of the patches and bugfixes that have occurred since
it was released.

For Windows, Steve Hall maintains a Vim installer with all the latest
patches and most recent official runtime files at the time of the
patch, available here:

http://sourceforge.net/projects/cream/

There are two different installers in this project, one for a "plain
Vim" which is basically equivalent to the installer on www.vim.org,
but with all the recent updates, and another for a "point-and-click"
configuration (which I tried recently after installing it for my
wife's use...it drives me nuts, but mostly just because I know that
Vim has so much more that it can do, not made readily available).

Gary Johnson

unread,
Apr 28, 2010, 4:16:01 PM4/28/10
to vim...@googlegroups.com
On 2010-04-28, Didlybom wrote:

> Gary,
>
> thanks for your effort in trying to help me find the problem. Did you try to
> format the file using your vim installation? I'd like to confirm that the
> problem really is not with my vim installation but with the plugin itself as
> you suspect. If the problem is with the plugin I will try to contact the
> author and hopefully he'll be able to help me.

Didly,

I fetched your example.vhd using wget to a machine running Linux.
I then re-indented it using my standard configuration and saw the
problem. Then I followed the procedure I used before,

vim -N -u NONE
:filetype plugin indent on
:set sw=2
gg=G

and still saw the indenting problems.

I had not looked at your example_indented.vhd file, so I did that
just now. If I leave 'sw' at the default of 8 and re-indent your
example.vhd, my result is identical to your example_indented.vhd.


I neglected to mention this earlier, but the convention on this list
is to put the text of one's reply at the bottom of the post rather
than at the top. I appreciate that you do trim your replies,
though.

Jack Donohue

unread,
Apr 28, 2010, 6:27:45 PM4/28/10
to vim...@googlegroups.com
I use the x (execute) command a lot

" Netrw Directory Listing (netrw
v132)
" Sorted by time
" Quick Help: <F1>:help -:go up dir D:delete R:rename s:sort-by
x:exec
"
============================================================================

Works fine in vim70, but I recently changed to vim 7.2 and I sometime get
this:

Press <cr> to continue

instead of opening the file with the file association. I'm running Windows
XP SP 3

Any ideas what's wrong?

Thanks


Jack

Didlybom

unread,
Apr 29, 2010, 4:30:39 AM4/29/10
to vim...@googlegroups.com


Gary Johnson-4 wrote:
>
> On 2010-04-28, Didlybom wrote:
> Didly,
>
> I fetched your example.vhd using wget to a machine running Linux.
> I then re-indented it using my standard configuration and saw the
> problem. Then I followed the procedure I used before,
>
> vim -N -u NONE
> :filetype plugin indent on
> :set sw=2
> gg=G
>
> and still saw the indenting problems.
>
> I had not looked at your example_indented.vhd file, so I did that
> just now. If I leave 'sw' at the default of 8 and re-indent your
> example.vhd, my result is identical to your example_indented.vhd.
>
>
> I neglected to mention this earlier, but the convention on this list
> is to put the text of one's reply at the bottom of the post rather
> than at the top. I appreciate that you do trim your replies,
> though.
>
> Regards,
> Gary
>
> --
> You received this message from the "vim_use" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>
>

Gary, thanks again for checking. This seems to confirm that the problem is
with the indenter logic in Vim. I have contacted the author of the VHDL
indent script and hopefully he'll be able to find the problem. If he does I
will post his answer in this thread.

Thanks for the heads up on the proper way to reply to messages on this lits.
I will do as you suggested from now on! :-)

Cheers,

Didly

--
View this message in context: http://old.nabble.com/How-to-fix-broken-automatic-indentation-in-vim-tp28379120p28397882.html
Sent from the Vim - General mailing list archive at Nabble.com.

Andy Wokula

unread,
Apr 29, 2010, 6:17:53 AM4/29/10
to vim...@googlegroups.com
Am 27.04.2010 18:29, schrieb Didlybom:
> Hi,
>
> I am trying to use vim 7.2 (on Windows XP) to automatically indent and
> format some VHDL and Matlab code. To do so I am trying to use the "gg=G"
> command. However this does not work properly. The code is not properly
> indented at all.
> ...

> For VHDL files I get similar problems.
>
> I have tried using different combinations of the autoindent, smartindent and
> cindent settings. After going through these forums I have also made sure
> that the "syntax", "filetype", "filetype indent" and "filetype plugin
> indent" are set to on. Still, it does not work. Also, if I do "set syntax?"
> I get "matlab" for matlab files and "vhdl" for vhdl files which is correct.
> And if I do "set indentexpr?" I get "GetMatlabIndent(v:lnum)" for matlab
> files and "GetVHDLindent()" for vhdl files.
>
> To try to isolate the problem (and ensure that it is not due to one of the
> vim plugins that I have installed) by doing a fresh install of VIM on a
> different computer (in which VIM had never been installed before). On that
> computer I get the same sort of problems (that is why I do not think that I
> need to give you the .vimrc, but if you need it I can upload it here too).

At least for VHDL, you may check the help:

:h ft-vhdl-indent

:let g:vhdl_indent_genportmap = 0

--
Andy

Didlybom

unread,
Apr 29, 2010, 6:45:35 AM4/29/10
to vim...@googlegroups.com


Andy Wokula wrote:
>
> At least for VHDL, you may check the help:
>
> :h ft-vhdl-indent
>
> :let g:vhdl_indent_genportmap = 0
>
> --
> Andy
>

Thanks Andy. I tried it but it still does not work right. It is not that I
want to change the way it indents the VHDL code, but that the way the plugin
indents the code is simply wrong.

At some point the indenting gets messed up, and things which should be at
the same level of indentation are put in different levels as in the example
files I sent yesterday.

Cheers,

Didly
--
View this message in context: http://old.nabble.com/How-to-fix-broken-automatic-indentation-in-vim-tp28379120p28398978.html
Sent from the Vim - General mailing list archive at Nabble.com.

Charles E Campbell Jr

unread,
Apr 30, 2010, 10:44:35 PM4/30/10
to vim...@googlegroups.com
Jack Donohue wrote:
> I use the x (execute) command a lot
>
> " Netrw Directory Listing
> (netrw v132)
> " Sorted by time
> " Quick Help: <F1>:help -:go up dir D:delete R:rename s:sort-by
> x:exec
> "
> ============================================================================
>
>
> Works fine in vim70, but I recently changed to vim 7.2 and I sometime
> get this:
>
> Press <cr> to continue
>
> instead of opening the file with the file association. I'm running
> Windows XP SP 3
>
> Any ideas what's wrong?
Does increasing your cmdheight option help? (:set ch=2)

Regards,
Chip Campbell

Jack Donohue

unread,
May 2, 2010, 1:32:59 PM5/2/10
to vim...@googlegroups.com

----- Original Message -----
From: "Charles E Campbell Jr" <drc...@campbellfamily.biz>
To: <vim...@googlegroups.com>
Sent: Friday, April 30, 2010 10:44 PM
Subject: Re: Strange problem with netrw in vim 7.2 windows


> Jack Donohue wrote:
>> I use the x (execute) command a lot
>>
>> " Netrw Directory Listing (netrw
>> v132)
>> " Sorted by time
>> " Quick Help: <F1>:help -:go up dir D:delete R:rename s:sort-by
>> x:exec
>> "
>> ============================================================================
>>
>> Works fine in vim70, but I recently changed to vim 7.2 and I sometime get
>> this:
>>
>> Press <cr> to continue
>>
>> instead of opening the file with the file association. I'm running
>> Windows XP SP 3
>>
>> Any ideas what's wrong?
> Does increasing your cmdheight option help? (:set ch=2)

No, didn't make any difference, no other messages besides the "Press <cr> to
continue" message.

Thanks,


Jack

Jack Donohue

unread,
May 2, 2010, 3:05:57 PM5/2/10
to vim...@googlegroups.com
> Jack Donohue wrote:
>> I use the x (execute) command a lot
>>
>> " Netrw Directory Listing (netrw
>> v132)
>> " Sorted by time
>> " Quick Help: <F1>:help -:go up dir D:delete R:rename s:sort-by
>> x:exec
>> "
>> ============================================================================
>>
>> Works fine in vim70, but I recently changed to vim 7.2 and I sometime get
>> this:
>>
>> Press <cr> to continue
>>
>> instead of opening the file with the file association. I'm running
>> Windows XP SP 3

So, I found that this line:

exe 'silent !start rundll32 url.dll,FileProtocolHandler
'.shellescape(fname,1)

is not working for some reason.

I added:

call input("start rundll32 url.dll,FileProtocolHandler
".shellescape(fname,1));

and it prints out for a sample file:

start rundll32 url.dll,FileProtocolHandler 'G:/CRW/Membership/missing.csv'

which looks fine to me.

I tried the former:

exe 'silent !start rundll32 url.dll,FileProtocolHandler "'.escape(fname,
'%#').'"'

and that works fine. The only difference I can see it the text for the two
commands is that the old escape ends up with a double quoted filename, while
shellescape generates single quotes.

Vim 7.0 version: Date: May 02, 2006
Vim 7.2 version: Date: Aug 08, 2008

Jack


Jack

Charles Campbell

unread,
May 3, 2010, 9:40:13 AM5/3/10
to vim...@googlegroups.com
Hmm -- maybe changing your shellquote option would help.
Regards,
Chip Campbell

Charles Campbell

unread,
May 6, 2010, 12:44:36 PM5/6/10
to vim...@googlegroups.com
Jack Donohue wrote:
>> Jack Donohue wrote:
>>> I use the x (execute) command a lot
>>>
>>> " Netrw Directory Listing
>>> (netrw v132)
>>> " Sorted by time
>>> " Quick Help: <F1>:help -:go up dir D:delete R:rename
>>> s:sort-by x:exec
>>> "
>>> ============================================================================
>>>
>>>
>>> Works fine in vim70, but I recently changed to vim 7.2 and I
>>> sometime get this:
>>>
>>> Press <cr> to continue
>>>
>>> instead of opening the file with the file association. I'm running
>>> Windows XP SP 3
> ..snip..
>
> and that works fine. The only difference I can see it the text for
> the two commands is that the old escape ends up with a double quoted
> filename, while shellescape generates single quotes.

Hello!

The help pages for shellescape() say:

On MS-Windows and MS-DOS, when 'shellslash' is not set, it
will enclose {string} in double quotes and double all double
quotes within {string}.
For other systems, it will enclose {string} in single quotes
and replace all "'" with "'\''".

IIUC, your problem is that you're getting single quotes instead of
double quotes. Do you happen to have 'shellslash' set?

:echo &shellslash

Regards,
Chip Campbell

Jack Donohue

unread,
May 8, 2010, 8:57:57 AM5/8/10
to vim...@googlegroups.com
> IIUC, your problem is that you're getting single quotes instead of double
> quotes. Do you happen to have 'shellslash' set?
>
> :echo &shellslash

Yes, that's it, shellslash is set to 1 since I usually like to generate unix
type pathnames. I'd prefer to keep that, and I just changed the code in
netrw.vim to the old escape way, and that works fine.

Thanks,


Jack
Reply all
Reply to author
Forward
0 new messages