Monday, July 16, 2012

Re: Scripting the enter key

Thanks for the tip on <expr>, I hadn't known about it!

Long story short, I was using 'autoclose' to do what delimitmate does, and was looking to add the CR expansion myself.  It turns out that delimitmate does everything that autoclose does, with smarter handling of matching (e.g. dm: I'm versus ac: I''m), and adds the functionality as you've mentioned.

The only caveat was that SuperTab (which I don't use, but was hanging around in my bundle directory) conflicts with delimitmate, and so I had to remove it.

In any case, I've got exactly what I was looking for, so thanks!

Cheers,
Doug

On Mon, Jul 16, 2012 at 11:07 AM, Ben Fritz <fritzophrenic@gmail.com> wrote:
On Monday, July 16, 2012 10:39:03 AM UTC-5, Douglas Mayle wrote:
> Hi all, I&#39;m trying to write a function to make smarter line breaking when I split a pair of parentheses or braces.  For example, with the text:
> <span style="font-family:&#39;courier new&#39;,monospace">func (</span><u style="font-family:&#39;courier new&#39;,monospace">)</u></div>
>
>
> <u style="font-family:&#39;courier new&#39;,monospace">
> </u></div>
> (The cursor is on the closing parentheses), when I hi enter, I&#39;d like to end with the following state:</div>
> <font face="courier new, monospace">func (</font></div>
>
>
> <font face="courier new, monospace">  _</font></div>
> <font face="courier new, monospace">)</font></div>
> In this case, the closing parentheses gets pushed down one line, and I end up on the following line with correct indentation following my settings.</div>
>
>
>
> </div>
> I&#39;ve written a script to accomplish this, but the indentation is always borked (It always starts at column 0).  Could anyone help me with pointers?</div>
>

Here's a few pointers:

1. If you use an <expr> map (or if start your mapping with <C-R>= instead of :), you can remain in insert mode when the mapping completes, without need for the <C-\><C-O>
2. Whenever you leave insert mode, and the line has nothing but whitespace, and you have made no changes to the line in insert mode other than whitespace, Vim will automatically remove the whitespace on the line. I think this (combined with temporarily leaving insert mode) may be why your indent is not what you expect it to be.
3. There are plugins which can do your desired task already. For example, DelimitMate. http://www.vim.org/scripts/script.php?script_id=2754 Note this plugin is primarily for inserting matched pairs together, but it also does balanced insertion of spaces and line breaks within empty matched pairs. See http://vim.wikia.com/wiki/Automatically_append_closing_characters#Plugins

Were I doing what you ask by hand, I'd have a mapping which basically does one of these (starting from insert mode):

<CR><Esc>O
<CR><CR><Esc>kcc

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: