Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim Auto Indent with newline

How do I get vim to place the cursor within the braces starting on a new line, ie with | denoting the cursor position :

class {   | } 

right now with my settings it only does this

class { |} 

I got this in my .vimrc file set autoindent shiftwidth=2 tabstop=2 noexpandtab

Basically I just want how a normal IDE would indent it.

update:

I found how to do this with inoremap { {<CR>}<Esc>O

like image 320
Zen Avatar asked Dec 18 '10 06:12

Zen


People also ask

What is auto indent?

Automatic indenting occurs when you insert new lines. • None No special indenting occurs. Source Insight will return the insertion point to the very beginning of the next line when you insert a new line or word wrap.


2 Answers

I have Ubuntu 12.04 and I found no vimrc file in home directory. Global vimrc file was in /etc/vim/vimrc.
There was almost nothing in this file. So for me it worked to add this 3 lines to the end of /etc/vim/vimrc

set autoindent set cindent inoremap { {<CR>}<up><end><CR> 

When you will type { next time it will be changed by combination {, Enter, }, up, end, Enter. cindent and autoindent will add required amount of Tab's.
P.S. I'm not good in tuning up vim so some explanations may be not so accurate. It's how I think it works.

like image 114
Ndmitry Avatar answered Oct 02 '22 11:10

Ndmitry


I found that delimitMate does exactly what you describe and more (that is, automatically inserting the ending }). Note you have to tell delimitMate to expand carriage returns by adding let delimitMate_expand_cr=1 to your config.

From my observation, this is exactly the behaviour found in TextMate and SublimeText.

like image 28
trkoch Avatar answered Oct 02 '22 09:10

trkoch