in reply to Slightly Off Topic: vim/perl comment behavior

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.

Replies are listed 'Best First'.
Re^2: Slightly Off Topic: vim/perl comment behavior
by saberworks (Curate) on Aug 23, 2005 at 17:43 UTC
    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
Re^2: Slightly Off Topic: vim/perl comment behavior
by xdg (Monsignor) on Aug 25, 2005 at 19:04 UTC

    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.