How to add a new line with the same indentation

33

10

Then I press Enter in Insert mode, a new line is created, but the cursor is placed at the beginning of the line.

How do I create a new line with the same indentation as the current one?

valya

Posted 2010-01-23T21:09:04.687

Reputation: 748

2and that's how google takes me to here. – towry – 2014-04-28T13:02:23.617

Answers

38

Adding set autoindent to your ~/.vimrc can do that for you automagically.

brice

Posted 2010-01-23T21:09:04.687

Reputation: 2 206

thanks! there was no .vimrc, is it ok to create a new one? – valya – 2010-01-23T21:13:49.363

yes, under your home directory. – John T – 2010-01-23T21:15:59.847

7

I'd also suggest adding this to your .vimrc:

set smartindent

It will increase the indent in a new block.

Nathan Fellman

Posted 2010-01-23T21:09:04.687

Reputation: 8 152

5

Both will annoy you when pasting, as Vim will try and indent everything on the fly.

Prior to pasting enter:

:set paste

romant

Posted 2010-01-23T21:09:04.687

Reputation: 1 071

This is true in the terminal. If you're using gvim you shouldn't need this. – Nathan Fellman – 2014-07-28T17:25:45.950

In complex situations (virtual machines, remote desktops, ...), even gvim will not have access to the clipboard. On the other hand, running vim locally will have access to the clipboard and vim can paste properly if you use "*p or "+p. Sometimes vim and :set paste and 'dumb' pasting-raw-to-the-terminal are the only option. – Aaron McDaid – 2014-12-09T13:34:08.680

0

At bottom of the file, I'm using:

# vim: ts=2 sw=2 sts=2 sr noet st ai si

For example Dockerfile:

FROM centos-7
RUN ...
CMD ...

# vim: ts=2 sw=2 sts=2 sr noet st ai si

If you want keep the indentation only, use # vim: st ai si

Eduardo Cuomo

Posted 2010-01-23T21:09:04.687

Reputation: 115