最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Automatic new line and indentation in vim when inside braces? - Stack Overflow

programmeradmin1浏览0评论

For example, if I'm coding jQuery in vim, and I have this in insert mode ("|" is the current cursor position):

$('#something').click(function(){|})

...and I hit enter, I would like to get this:

$('#something').click(function(){
    |
})

...while currently, I'm getting this:

$('#something').click(function(){
|})

The same thing happens with function definitions in PHP. Is there a way to automatically insert the extra new line and indent the cursor position when hitting enter inside braces like that?

(Apologies if this has been asked before - it seems like a common request but I searched for awhile and couldn't find it.)

For example, if I'm coding jQuery in vim, and I have this in insert mode ("|" is the current cursor position):

$('#something').click(function(){|})

...and I hit enter, I would like to get this:

$('#something').click(function(){
    |
})

...while currently, I'm getting this:

$('#something').click(function(){
|})

The same thing happens with function definitions in PHP. Is there a way to automatically insert the extra new line and indent the cursor position when hitting enter inside braces like that?

(Apologies if this has been asked before - it seems like a common request but I searched for awhile and couldn't find it.)

Share Improve this question asked Jan 22, 2011 at 13:31 Mike CrittendenMike Crittenden 5,9176 gold badges49 silver badges77 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 8

I ended up just hardcoding this into .vimrc, as follows

" Make it so that a curly brace automatically inserts an indented line
inoremap {<CR> {<CR>}<Esc>O<BS><Tab>

It would seem like the <BS><Tab> parts aren't necessary, but for some reason it wasn't indenting correctly like the O command usually does, so I had to add it.

Just install auto-pairs plugin and be happy.

I use javascript.vim which (almost) does what you want for JavaScript code.

There's also a php.vim, but I didn't test it.

My version is similar but uses <C-o> instead of <Esc> since it would trigger validation by JavaScriptLint.vim.

Also, I don't really want every {} to be on multiple lines so it's mapped to ctrl + .

inoremap <C-Return> <CR><CR><C-o>k<Tab>
发布评论

评论列表(0)

  1. 暂无评论