33

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?

  • 2
    and that's how google takes me to here. – towry Apr 28 '14 at 13:02
37

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

  • thanks! there was no .vimrc, is it ok to create a new one? – valya Jan 23 '10 at 21:13
  • yes, under your home directory. – John T Jan 23 '10 at 21:15
7

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

set smartindent

It will increase the indent in a new block.

5

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

Prior to pasting enter:

:set paste
  • This is true in the terminal. If you're using gvim you shouldn't need this. – Nathan Fellman Jul 28 '14 at 17:25
  • 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 Dec 9 '14 at 13:34
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.