Vim Tips Wiki
(start merging)
(Spiiph + Fritzophrenic: please check comments)
Line 46: Line 46:
   
 
==To be moved elsewhere==
 
==To be moved elsewhere==
I moved the following to here (a mistake). I'll think it over and will move it elsewhere, perhaps [[VimTip224]].
+
''I moved the following to here (a mistake). I'll think it over and will move it elsewhere, perhaps [[VimTip224]].''
  +
  +
===Inserting text in a blank line===
  +
Here is how to insert text into a blank line, while having the text correctly indented:
  +
*Use file type indentation (<tt>:filetype indent on</tt> in vimrc).
  +
*Do not press <tt>i</tt> or <tt>A</tt> to start insert mode in a blank line.
  +
*Instead, type <tt>cc</tt> to start insert mode with the cursor correctly indented.
  +
*Alternatively, press <tt>S</tt> (same as <tt>cc</tt>).
  +
  +
<tt>cc</tt> deletes the current line, then starts insert mode on a new line. {{help|cc}}
   
 
===Rough merge in from 1019 (now removed)===
 
===Rough merge in from 1019 (now removed)===
The help for autindent says:
+
The help for autoindent 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...
+
: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):
 
This give some difficulties (at least for me):
Line 71: Line 80:
   
 
----
 
----
You can press Ctrl+R_ to make sure indent would not be deleted.
+
You can press Ctrl-R_ to make sure indent would not be deleted.
   
 
===Rough merge in from 1327 (now removed)===
 
===Rough merge in from 1327 (now removed)===
Line 102: Line 111:
 
I assume most other people do this: <code>class A extends B {<CR><Tab>A() {<CR><Tab>super();<CR><BS>}<CR><BS>}</code>. I wouldn't say either way of typing is better than the other, since they both require the same number of keystrokes.
 
I assume most other people do this: <code>class A extends B {<CR><Tab>A() {<CR><Tab>super();<CR><BS>}<CR><BS>}</code>. 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 <tt>S</tt> {{help|S}}. That, and turn <tt>:filetype indent on</tt>, which removes the need to type <Tab> in the above code. ([[User:Spiiph|Spiiph]] 09:05, 15 August 2009 (UTC))
 
: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 <tt>S</tt> {{help|S}}. That, and turn <tt>:filetype indent on</tt>, which removes the need to type <Tab> in the above code. ([[User:Spiiph|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! --[[User:Fritzophrenic|Fritzophrenic]] 00:20, 17 August 2009 (UTC)
 
:::The ''Vim does everything correctly; don't try to fight it''-tip? :) ([[User:Spiiph|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 <code>S</code>--I added it and <tt>cc</tt> to the list.
 
:::You added mappings <tt>nnoremap S S<Space><BS></tt> and <tt>nnoremap cc cc<Space><BS></tt>. However, Spiiph did not mean that, so I removed the mappings. Spiiph is saying that you should have file type indentation on (<tt>:filetype indent on</tt> in vimrc). And, if you want to add code to a blank line, do not press <tt>i</tt> or <tt>A</tt> to start insert mode. Instead, press <tt>S</tt> which will enter insert mode with the cursor correctly indented. I hope to fix all these messy indent tips in a week or two... [[User:JohnBeckett|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: ''<nowiki>[Snip]</nowiki> 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. [[User:JohnBeckett|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! --[[User:Fritzophrenic|Fritzophrenic]] 13:54, 17 August 2009 (UTC)
 
   
 
==References==
 
==References==
Line 118: Line 118:
   
 
==Comments==
 
==Comments==
  +
;Thoughts wanted
  +
*I moved the "hash" tips to here and I think that section is fairly ok.
  +
*Inexplicably, I also moved the "keep indent on blank line" tips to here – they need to be fixed and moved elsewhere. I want to clean these up first, which will help decide where the "To be moved elsewhere" section should be moved.
  +
*I can't make sense of "You can press Ctrl-R_ to make sure indent would not be deleted". Can anyone see the point, or should that comment be deleted?
  +
*What about the Ctrl-R" comment? Is it at all helpful (regarding indents)? I suspect it should be deleted.
  +
*I added the "Inserting text in a blank line" section (from Spiiph). What needs to be kept from the stuff after that section?
  +
*What about the issue of pasting? I can't understand the point made above. Is it claiming that the advice given somehow allows pasting with correct indents?
  +
*Is there anything helpful we can say about pasting with correct indents? If moving a few lines of code around, I would use normal mode, and would manually fix the indent. We have some advice somewhere on using <tt>=</tt> to fix indents on pasted text – I'll find that later. [[User:JohnBeckett|JohnBeckett]] 12:29, 23 August 2009 (UTC)

Revision as of 12:29, 23 August 2009

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.

Inserting text in a blank line

Here is how to insert text into a blank line, while having the text correctly indented:

  • Use file type indentation (:filetype indent on in vimrc).
  • Do not press i or A to start insert mode in a blank line.
  • Instead, type cc to start insert mode with the cursor correctly indented.
  • Alternatively, press S (same as cc).

cc deletes the current line, then starts insert mode on a new line. :help cc

Rough merge in from 1019 (now removed)

The help for autoindent 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))

References

Comments

Thoughts wanted
  • I moved the "hash" tips to here and I think that section is fairly ok.
  • Inexplicably, I also moved the "keep indent on blank line" tips to here – they need to be fixed and moved elsewhere. I want to clean these up first, which will help decide where the "To be moved elsewhere" section should be moved.
  • I can't make sense of "You can press Ctrl-R_ to make sure indent would not be deleted". Can anyone see the point, or should that comment be deleted?
  • What about the Ctrl-R" comment? Is it at all helpful (regarding indents)? I suspect it should be deleted.
  • I added the "Inserting text in a blank line" section (from Spiiph). What needs to be kept from the stuff after that section?
  • What about the issue of pasting? I can't understand the point made above. Is it claiming that the advice given somehow allows pasting with correct indents?
  • Is there anything helpful we can say about pasting with correct indents? If moving a few lines of code around, I would use normal mode, and would manually fix the indent. We have some advice somewhere on using = to fix indents on pasted text – I'll find that later. JohnBeckett 12:29, 23 August 2009 (UTC)