saberworks has asked for the wisdom of the Perl Monks concerning the following question:

I use vim as my editor, and it's been driving me crazy: I use smartindent which means after I type this: if(some_condition()) { and press enter, it automatically tabs me in the correct number of tabs to line up my next line of code. However, if I then type a # character to start a comment, it jumps the cursor to the beginning of the line and then shows the character on the screen. So if I am 3 tabs in, and I press a #, it automatically jumps me to the 0th character of the line. Anybody know how to fix this? Here are my customized vim settings (I'm assuming everything else is the default - and by the way, I use gvim (vim-gtk package in Debian)).
colorscheme torte set nowrap set linebreak set nu set tabstop=4 set shiftwidth=4 set gfn=ProFont\ 11 set gfn=ProggyCleanTTSZ\ 12 set smartindent set noexpandtab set showmatch set formatoptions=tcr set textwidth=80

Replies are listed 'Best First'.
Re: Slightly Off Topic: vim/perl comment behavior
by itub (Priest) on Aug 22, 2005 at 23:59 UTC
    From the vim documentation (:h smartindent):
    When typing '#' as the first character in a new line, the indent for that line is removed, the '#' is put in the first column. The indent is restored for the next line. If you don't want this, use this mapping: ":inoremap # X^H#", where ^H is entered with CTRL-V CTRL-H.
      Thank you. I didn't make the connection between smartindent and the behavior, probably because the behavior was not smart :) This fixed my issue.

      As an aside, why would anyone want their comment to the far left no matter what? Is there some common commenting idiom I am missing?
        Vim's smartindent was designed mainly with C in mind. In C, the '#' is used for preprocessor directives, which are usually placed at the beginning of the line (I think some versions of the C preprocessor even require them to be at the beginning of the line, but I'm not sure).

        Added: Yep, it seems that "traditional" C only recognized preprocessor directives in column 1. With gcc, you can emulate this behavior by supplying the '-traditional-cpp' option.

        >why would anyone want their comment to the far left no matter >what? Is there some common commenting idiom I am missing? Usually, programmers do their programming with intermediate checks, by commenting the checks and removing them when they wish to check the code.This will be tiresome if some one want to search for the # symbols some where inside the code, So It becomes kind of an easy,rule to keep the comments at the beginning of the line. Best Vasundhar

      As long as we're off topic onto the subject of vim, .vimrc and comments in the left hand column -- let me add that I've found it very useful to put some macros into my .vimrc to allow me to quickly comment out a line or block of code (or to remove such comments).

      The following commands for .vimrc provide the macros ",ic" and ",rc" to insert and remove comments. In normal mode, it only affects the current line. More useful may be visual mode, where you can select a large chunk of code and comment it out in one shot.

      vmap ,ic :s/^/#/g<CR>:let @/ = ""<CR> map ,ic :s/^/#/g<CR>:let @/ = ""<CR> vmap ,rc :s/^#//g<CR>:let @/ = ""<CR> map ,rc :s/^#//g<CR>:let @/ = ""<CR>

      -xdg

      Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: Slightly Off Topic: vim/perl comment behavior
by webfiend (Vicar) on Aug 23, 2005 at 08:46 UTC
    That problem went away for me as soon as I started using the setting filetype indent on and filetype plugin on. I'd actually forgotten about it!