Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Can't get vim to stop outdenting Perl comments, any ideas?

by Tommy (Chaplain)
on Jan 14, 2010 at 14:16 UTC ( #817406=perlquestion: print w/replies, xml ) Need Help??
Tommy has asked for the wisdom of the Perl Monks concerning the following question:

I have tried everything I have found in monks supersearch, and all over the web. The suggestions that held the most promise were found at, but to absolutely no avail. I have tried so many different things, and so many combinations of different things in my .vimrc file, and none will work.

This problem has frustrated me for so long, and I'm at my wit's end.

To reproduce my woeful situation, one need only open a perl file in vim and type:

sub foo {

.. then hit the carriage return. That automatically indents your code for you, in most default vim installs. Great! That's what it should do! At this point you can start in with your code, which would look something like:

sub foo { my $bar = 'baz';

All is well. Now hit the carriage return again and start a comment. Instantly vim outdents the comment to column zero:

sub foo { my $bar = 'baz'; # some comment

This is exactly what I am so tired of experiencing. The comment should stay, imo, right in line with "my $bar..." at indented column n (where n=three spaces for me).

*Holds back a long string of curses* Please, anyone, can you tell me what I am missing here, and why vim just will NOT allow me to:

sub foo { my $bar = 'baz'; # some comment

Stuck, and so sad in vim indentation hell, I remain...


Replies are listed 'Best First'.
Re: Can't get vim to stop outdenting Perl comments, any ideas?
by roboticus (Chancellor) on Jan 14, 2010 at 15:02 UTC


    Yeah, I can see how that would suck. I just tried the steps you mentioned on my machine, and comment lines don't outdent for me. So just in case it helps, here's my .vimrc file:

    " .vimrc " 20100104 set cursorline for better visibility " 20080618 Trying to fix fileencoding so we can write UNICODE as LATIN " 20080617 reopen file at same position as last seen " 20080416 Chg/set backspace, display, fillchars, color scheme " 20050208 original version set fileencodings=ucs-bom,utf-8,default,latin1 "set tabstop=4 set nowrap set ruler " show cursor position always set showcmd " display incomplete commands set incsearch " incremental searching set modeline " Allow setting values in file "set background=dark " Make color scheme visible colorscheme roboticus set cursorcolumn " Make cursor column obvious for icicles, etc. if &t_Co > 2 || has("gui_running") syntax on set hlsearch endif "if has("autocmd") " " highlight the active cursor line " set cursorline " hi CursorLine ctermbg=1 "endif if has("autocmd") " highlight the active cursor line set cursorline "hi CursorLine ctermbg=7 hi CursorLine ctermbg=4 " Recognize file types and use appropriate indenting filetype plugin indent on augroup vimrcEx autocmd FileType text setlocal textwidth=78 augroup END else " set autoindent endif " has("autocmd") " 20080416 MCM New additions set backspace=eol,indent,start " Make backspacing a little easier set display=lastline " Show partial lines when in wrap mode " Doesn't work? set fillchars=stl:=,stlnc:= " Autowrap text with comment leader as appropriate, automatically exte +nd com " t - autoformat text, c - autoformat comments, r - automatically cont +inue " comment on next line, o - automatically continue column on o/O comma +nd, q - " allow comment formatting with gq command, l - don't break long lines + if they " started long, b - autowrap only if you hit a blank before the wrap m +argin. set formatoptions=tcroqlb " 20080617 MCM Reopen file at last-edited position (see :help " last-position-jump) au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$") | exe +"normal g'\"" | endif


      Roboticus, your .vimrc is very nice. I enjoyed the last line especially.
        I haven't used the "last-position-jump" au command, but I do get it via view (session is horrid).
Re: Can't get vim to stop outdenting Perl comments, any ideas?
by bv (Friar) on Jan 14, 2010 at 20:45 UTC

    I believe this is a result of using the cindent option. Luckily, this can be configured by setting the cinkeys or cink option. This is a comma-separated list of keys that force a reindent. Remove the 0#, and your problems should disappear. Try this: set cink-=0#

    print map{substr'hark, suPerJacent other l',$_,1}(11,7,6,16,5,1,15,18..23,8..10,24,17,0,12,13,3,14,2,4);
      Yes, I did try the cinkeys method also, which worked in some, but not all cases for me. I do have some good news though. I did get the functionality finally with the .vimrc file I quote below. However I still plan to borrow some coolness from ovid.
      :set nocompatible :filetype plugin indent on :syntax enable :set background=dark :set tabstop=3 :set shiftwidth=3 :set softtabstop=3 :set ignorecase :set expandtab :set modeline :set ruler :set showmatch :set nohlsearch :autocmd BufWritePre * :%s/\s\+$//e :nnoremap <F5> :set invpaste paste?<CR> :set pastetoggle=<F5> :let perl_fold = 1 :set foldmethod=syntax :set cursorline :set cursorcolumn :autocmd FileType perl highlight OverLength ctermbg=green ctermfg=whit +e guibg=#E293AA :match OverLength /\%81v.*/ :set number :set backspace=eol,indent,start :autocmd BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$") +| exe "normal g'\"" | endif


        I just found out that unless the cursor is in column 0, ":set cursorcolumn" completely negates ":autocmd BufWritePre * :%s/\s\+$//e" (the latter removing trailing whitespace from all lines on write)

        So "cursorcolumn" hot a feature after all, even though we had a good run together for a day. :-(

      Or, just turn off cindent.
Re: Can't get vim to stop outdenting Perl comments, any ideas?
by rubasov (Friar) on Jan 14, 2010 at 22:56 UTC
    There are several ways to get vim autoindent your code (set autoindent, set smartindent, set cindent, etc). When I first tried to use autoindenting I also faced your problem with comment outdenting, and later on I found a method to get rid of this "feature". So try this (put this to your .vimrc and comment out any other type of autoindenting)
    filetype indent on
    Probably this also helps you to get your desired indent level:
    autocmd FileType perl set softtabstop=3 autocmd FileType perl set shiftwidth=3 autocmd FileType perl set tabstop=3 autocmd FileType perl set expandtab
Re: Can't get vim to stop outdenting Perl comments, any ideas?
by creamygoodness (Curate) on Jan 16, 2010 at 17:07 UTC
    I used to have this problem. It went away when I replaced cindent with smartindent.
Re: OT - Can't get vim to stop outdenting Perl comments, any ideas?
by parv (Priest) on Jan 16, 2010 at 16:36 UTC

    Have you tried :set nocindent autoindent comments=n:# formatoptions=tcqr (invoke vim, if running on Unix-like OS, as \vim -N -u /dev/null some-file, then set previous options) ? If that does not work ...

    What options do you use to invoke vim? Could you please post vim version, and minimum configuration file to reproduce the problem?

    BTW, have you posted your problem on comp.editors newsgroup yet?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://817406]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2018-01-20 11:54 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (226 votes). Check out past polls.