Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Why does vim move me to the begining of a line when I press # for a comment or remark say in a perl file?

Why would I not want my comment to be indented at the same level of the current code?

share|improve this question

migrated from stackoverflow.com Jul 6 '11 at 15:33

This question came from our site for professional and enthusiast programmers.

    
Do you meaning typing # in insert mode, or are you using a mapping to comment the line? –  Prince Goulash Jul 6 '11 at 12:43
1  
@jHack - check 'related' to the right; vi is absolutely related (and frequently asked/answered) –  KevinDTimm Jul 6 '11 at 12:52
    
@KevinDTimm: you're right, i'll try to remove my flag/comments –  jHackTheRipper Jul 6 '11 at 12:59
1  
Once again... these vim questions belong on SO, not SU. –  Randy Morris Jul 6 '11 at 22:00

3 Answers 3

Ensure that the following are set in your .vimrc:

filetype plugin indent on
syntax enable

If you are using the "smartindent" indenting option, a fix for your problem is explained in the :help smartindent VIM documentation:

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. When using the ">>" command, lines starting with '#' are not shifted right.

share|improve this answer

I think it is confused and thinks you are editting a C file, where preprocessor directives must be against the left margin.

share|improve this answer
:inoremap # X^H#

did not work for me, here is what did:

inoremap # ^T#^D
  • ^T (Ctrl-T) indents
  • ^D unindents.

The drawback to this solution is that if you are not on an indent column it will snap you to one. If your code is indented properly it should not be a problem.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.