Reputation: 4970
I'm trying to figure out a way to do the following since I've been doing a lot of javascript development.
given the following line:
var someFunc = function() {};
into:
var someFunc = function() {
// proper indenting -- cursor placed here
};
I would like like to have vim do this from insert mode by just pressing the enter key. It would be easy to remap the enter key for the actual spacing, but the issue that I'm finding is checking to make sure I'm within the braces.
Any help would be great!
Thanks!
Upvotes: 2
Views: 544
Reputation: 196826
There are many ways to do that. And many similar questions here and on superuser.
Supposing your cursor is there and you are in insert mode:
var someFunc = function() {|};
this mapping would do what you want:
inoremap <C-Return> <CR><CR><C-o>k<Tab>
Supposing your cursor is elsewhere on the line and you are still in insert mode, this mapping would work:
inoremap <C-Return> <Esc>$T{i<CR><CR><C-o>k<Tab>
Because it is more generic, this second mapping would provide a good solution to your immediate problem.
But you could also use a plugin like DelimitMate (there are others) that inserts the closing bracket and does the "bracket opening" for you.
Snippets (SnipMate (original, fork), UltiSnips and others) are also useful: typing fun
followed by <Tabs>
is certainly a nice way to write a complete function definition stub.
Upvotes: 3