47

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

Zen
  • 847
  • 1
  • 10
  • 10

6 Answers6

27

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.

Ndmitry
  • 271
  • 3
  • 2
  • I added a "" at the end of the last line to make it indent the middle line one step. I.e. inoremap { {} – Skurpi Dec 17 '13 at 10:01
24

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.

trkoch
  • 2,688
  • 1
  • 16
  • 17
  • FYI for users of delimitMate + neocomplcache: If you use the default neocomplcache .vimrc setup, you will need to adjust the mapping to accommodate both plugins. More info [here](https://github.com/Raimondi/delimitMate/issues/111). My .vimrc now has `imap pumvisible() ? neocomplcache#smart_close_popup() : 'delimitMateCR'` and both plugins work like I want them to – bknoles Feb 12 '14 at 21:10
  • 4
    For `delimitMate` `expand_cr_1` hasn't been working recently. Instead, you can use `imap pumvisible() ? "\" : "delimitMateCR"`. – Yuriy Nemtsov Nov 26 '14 at 17:21
14

Put this in your .vimrc :

imap <C-Return> <CR><CR><C-o>k<Tab>

Assuming autoindent and smartindent are set correctly, typing Ctrl + Return between braces will put your cursor where you want it to be.

romainl
  • 186,200
  • 21
  • 280
  • 313
  • 1
    is there anyway to map it to just enter tho? Im kind of used to pressing just enter key as I been using eclipse all this while. – Zen Dec 18 '10 at 07:58
  • or is there anyway to integrate this within a indent file for a particular language? – Zen Dec 18 '10 at 08:14
  • I think that mapping such a functionality to the regular Return would mean making it a real function and do at least two RegExp tests each time you hit the key. It seems to be a bit overkill. That, and I'm afraid I'm not advanced enough for that. +1 for the indent file, though. – romainl Dec 18 '10 at 08:29
  • 3
    Here's a slight variation that is more compatible with plugins that remap `imap k` – Jedidiah Hurt Sep 22 '11 at 15:22
  • Thanks, actually I found another one yesterday: `imap k` with `` starting insertion at the right indentation level. Also I think that using `inoremap` should protect from side effects. – romainl Sep 22 '11 at 15:40
  • First, not all terminals are capable to send correctly. You might need to configure something: https://stackoverflow.com/questions/16359878/vim-how-to-map-shift-enter/42461580#42461580 Second, another alternative can use O, which will minimize cursor movements: `imap ` – Hielke Walinga Aug 10 '20 at 18:21
12

autoindent refers to it carrying over the current indentation level onto subsequent lines. To get it to indent according to syntax, you need to specify a flag like smartindent or cindent as well.

Justin Spahr-Summers
  • 16,893
  • 2
  • 61
  • 79
  • the indentation is not a prob, i just want a newline inserted automatically when i hit enter with the correct indentation – Zen Dec 18 '10 at 07:08
  • I'm confused. Are you outside of insertion mode? If you press `i` then `Return`, do you get the behavior you want? – Justin Spahr-Summers Dec 18 '10 at 07:10
  • Yes i'm in insert mode, what i want is when I press enter after { i want vim to put me on a new line within the closing }, basically the first block in my question. – Zen Dec 18 '10 at 07:22
3

I wrote this in my .vimrc

inoremap <expr> <CR> InsertMapForEnter()
function! InsertMapForEnter()
    if pumvisible()
        return "\<C-y>"
    elseif strcharpart(getline('.'),getpos('.')[2]-1,1) == '}'
        return "\<CR>\<Esc>O"
    elseif strcharpart(getline('.'),getpos('.')[2]-1,2) == '</'
        return "\<CR>\<Esc>O"
    else
        return "\<CR>"
    endif
endfunction

The code above first check if you are using Enter to do confirm a code completion, if not it will indent the {|} when you type Enter. Also, it provides html tags auto indent.

For your problem:

class {|}

press Enter and you will get

class {
    |
}
<html>|<html>

press Enter and you will get

<html>
    |
</html>
WW00WW
  • 417
  • 4
  • 15
1

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
  • 17,828
  • 6
  • 117
  • 94