LondonRob
LondonRob

Reputation: 79013

Typing function definitions in Vim

I'm newly excited about using Vim for all my programming needs, but I haven't yet managed to Google myself to a neat way of writing function definitions.

When typing a new function definition (I'm writing Javascript) I do this:

  1. ifunction foo(x) {}Esc which leaves me in normal mode with the cursor between the braces.

  2. Now iReturnEsc which puts the closing brace on a new line.

  3. Finally O opens a new line above the closing brace with the cursor on that new line.

This seems inefficient. Is there a neater way? (ideally not involving .vimrc hacks, because I'm trying to learn as much 'vanilla' vim as I can)

Upvotes: 1

Views: 65

Answers (2)

Ingo Karkat
Ingo Karkat

Reputation: 172778

snippets are like the built-in :abbreviate on steroids, usually with parameter insertions, mirroring, and multiple stops inside them. One of the first, very famous (and still widely used) Vim plugins is snipMate (inspired by the TextMate editor); unfortunately, it's not maintained any more; though there is a fork. A modern alternative (that requires Python though) is UltiSnips. There are more, see this list on the Vim Tips Wiki.

There are three things to evaluate: First, the features of the snippet engine itself, second, the quality and breadth of snippets provided by the author or others; third, how easy it is to add new snippets.

Upvotes: 1

Amadan
Amadan

Reputation: 198556

ifunction foo(x) {CR
}Ctrl-OO

is the golfiest I can get to your goal (parentheses closed at all times) without editing .vimrc or installing plugins.

Upvotes: 1

Related Questions