vim: indent next line more than the current

39 Views Asked by At

I am looking for a way to easily indent next line more than the current. This, in combination with autoindent, should make editing yaml files easier.

One of the possible solutions I thought would be to add several mappings for interactive mode. For example: <space><newline> to map to <newline><tabstop number of spaces>

<space><space><newline> to map to <newline><tabstop number of spaces><tabstop number of spaces>

I tried imap <8b>^M ^M<8b><8b>, but that didn't work. And I don't know why.

So if you could help me figure out why the above did not work, or suggest me a more elegant solution, I would be greatful.

1

There are 1 best solutions below

0
Friedrich On

The format for the special keys in your mapping is slightly off. If you write a mapping for <8b>^M, you would need to press <8B>^M. That's not what you want.

Starting from your textual explanation of what you want to accomplish, I created some mappings for SpaceEnter. The following mappings will work for up to four levels of indentation. For more than four, I expect things to become messy.

inoremap <Space><CR> <CR><Tab>
inoremap <Space><Space><CR> <CR><Tab><Tab>
inoremap <Space><Space><Space><CR> <CR><Tab><Tab><Tab>
inoremap <Space><Space><Space><Space><CR> <CR><Tab><Tab><Tab><Tab>

I'm also using :inoremap 1) on general principle and 2) because we don't want to trigger any mappings on their right-hand side. Some plugins just love to map tab for code completion etc.

Note that this will cause a tiny delay each time you press space because Vim will wait for the following key and whether to trigger a mapping. This can be quite irritating when writing text.

I also don't see any major advantage over breaking the line first and then adjusting indentation, either by pressing tab or backspace or Vim's < and > (see :help shift-left-right).

The usual way to go is to make Vim get the indentation right based on your buffer's contents. So, no, I don't think this is a good idea. This is how it could be done.