On Monday, July 16, 2012 10:39:03 AM UTC-5, Douglas Mayle wrote:
> Hi all, I'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:'courier new',monospace">func (</span><u style="font-family:'courier new',monospace">)</u></div>
>
>
> <u style="font-family:'courier new',monospace">
> </u></div>
> (The cursor is on the closing parentheses), when I hi enter, I'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'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
No comments:
Post a Comment