Vim Tips Wiki
Advertisement
Tip 644 Printable Monobook Previous Next

created 2004 · complexity basic · author stsi · version 6.0


Q: Why does Vim place # at the first column?

A: You are using 'smartindent', or you have 0# in your 'cinkeys' or 'indentkeys' options.

Under some circumstances, when you type a '#' character while in insert mode, the indent on the current line will be removed. Also, formatting or otherwise attempting to adjust the indent of a line starting with '#' may not work as you expect.

This tip discusses the issues and how problems can be avoided.

Using filetype indentation

When writing programs in languages such as C, Python or Ruby, the 'smartindent' option should not be set (typing :set si? should show "nosmartindent"). Instead, it is better to use the filetype based indentation provided for your language because it offers more options and functionality. See :help 30.3 and Example vimrc.

With this advice, you should avoid the '#' problem.

Using smartindent

If you need to set 'smartindent' you will find that inserting '#' at the start of a line will cause any indent to be removed (that is, any leading whitespace before '#' is deleted). To avoid this, :help 'smartindent' suggests the following mapping:

:inoremap # X<BS>#

Another consequence of setting 'smartindent' is that the operator > will not indent a line starting with '#', and the operator = will remove indents from any lines starting with '#'. If that is a concern, do not set 'smartindent'.

Adjusting cinkeys or indentkeys

When 'equalprg' has its default empty value, the = operator adjusts indentation. For example, typing == re-indents the current line. The re-indentation may remove all indents from lines that start with '#'.

To prevent any special treatment of lines starting with '#', remove the '0#' value from:

  • The 'cinkeys' option (if using 'cindent').
  • The 'indentkeys' option (if using 'indentexpr').

For example:

set cinkeys-=0#
set indentkeys-=0#

To be moved elsewhere

I moved the following to here (a mistake). I'll think it over and will move it elsewhere, perhaps VimTip224.

Rough merge in from 1019 (now removed)

The help for autindent says:

...If you do not type anything on the new line except <BS> or CTRL-D and then type <Esc> or <CR>, the indent is deleted again...

This give some difficulties (at least for me):

  • If I type <Enter>, the cursor moves to the new line and indented, but when I type <Esc>, the indent is deleted (the cursor moves the the leftmost position)
  • If I type <Enter>, and then paste something, the first line of the pasted text is not indented (the pasted text is not positioned from the cursor position).

To fix this, I use the mapping :

imap <CR> <CR> <BS>

At least, now if I pasted some text after pressing <Enter>, it starts from the current cursor position.

Comments

what about simply pressing Ctrl-R" ?


Unfortunately, this also has the side-effect of messing up the autoindent feature of if / while / for statements (at least in Java)


You can press Ctrl+R_ to make sure indent would not be deleted.

Rough merge in from 1327 (now removed)

The default behavior of the 'autoindent' option is as follow:

Copy indent from current line when starting a new line (typing <CR> in Insert mode or when using the "o" or "O" command). If you do not type anything on the new line except <BS> or CTRL-D and then type <Esc> or <CR>, the indent is deleted again.

I find that behavior quite annoying since the cursor jumps to the left when empty lines are inserted and 'escape' is pressed. Fortunately, it is possible to force Vim to keep the indentation with the following lines in your vimrc:

inoremap <CR> <CR><Space><BS>
nnoremap o o<Space><BS>
nnoremap O O<Space><BS>

If you are using filetype indentation (with filetype indent on in your vimrc) rather than using the 'autoindent' setting, this is unnecessary. After Vim removes the indentation from an empty line, you can press cc or S on that empty line and Vim will reapply the indentation rules. This happens because cc works by deleting the entire line and then re-entering insert mode on a brand-new line (or as :help cc says, "delete {count} lines and start insert linewise").

Comments;

Thank you. This tip is very helpful. I was getting very annoyed with Vim deleting the blank lines because it interferes with the way I type.

The way I type the following is: class A extends B {<CR><CR>}<Up><Tab>A() {<CR><CR>}<Up><Tab>super();

class A extends B {
    A() {
        super();
    }
}

I assume most other people do this: class A extends B {<CR><Tab>A() {<CR><Tab>super();<CR><BS>}<CR><BS>}. I wouldn't say either way of typing is better than the other, since they both require the same number of keystrokes.

How does that interfere with Vim (correctly) deleting whitespace on empty lines? This tip is generally misguided, since most users who think they need it, really need to learn to use S :help S. That, and turn :filetype indent on, which removes the need to type <Tab> in the above code. (Spiiph 09:05, 15 August 2009 (UTC))
Hmm. You just showed me something new! For some reason, it never occurred to me that "delete {count} lines and start insert linewise" would reapply the filetype indent, but after you pointed this out it becomes obvious. We should mention this fact in the tip, or create a new tip. I've always just used A<Tab><Tab>... until I had the indentation I wanted on empty lines. No longer! --Fritzophrenic 00:20, 17 August 2009 (UTC)
The Vim does everything correctly; don't try to fight it-tip? :) (Spiiph 01:29, 17 August 2009 (UTC))
This tip is not the slightest bit misguided. Some people like to leave indentation on empty lines, so for those people Vim deleting whitespace on empty lines is not correct behavior. Anyway, thanks for mentioning S--I added it and cc to the list.
You added mappings nnoremap S S<Space><BS> and nnoremap cc cc<Space><BS>. However, Spiiph did not mean that, so I removed the mappings. Spiiph is saying that you should have file type indentation on (:filetype indent on in vimrc). And, if you want to add code to a blank line, do not press i or A to start insert mode. Instead, press S which will enter insert mode with the cursor correctly indented. I hope to fix all these messy indent tips in a week or two... JohnBeckett 07:45, 16 August 2009 (UTC)

I would like my reply to 24.247.181.100 to stay here longer in case the user returns: [Snip] restored above

I hope to fix all these messy indent tips in a week or two, and certainly will find a place for Spiiph's advice which I will copy from this comment to some "how to indent" tip, and I will be merging any useful advice from this tip to somewhere else. JohnBeckett 03:22, 17 August 2009 (UTC)

Sorry, I have no idea how I wound up deleting the comment, that was unintentional. I must have accidentally edited a historic version accessed via a diff link from "recent changes" rather than the latest version. I've restored the deleted remarks, and I'll be more careful from now on! --Fritzophrenic 13:54, 17 August 2009 (UTC)

References

Comments

Advertisement