Saturday, August 20, 2011

Re: Tweaking $ command

On 08/20/2011 06:05 PM, AK wrote:
> Hi, I was trying to change $ command to go to N chars before the end of
> line, and found it surprisingly hard to do. Here's what I come up with:
>
> func! EndOfLine()
> exe "normal \<End>"
> let c = v:count
> let c2 = c - 1
> let cmd = "normal " . c2 . "k" . c . "h"
> if c | exe cmd | endif
> endfu
> nnoremap $ :<c-u>call EndOfLine()<cr>
>
>
> Is there a better way to do this?

I got it working with:

:nnoremap <silent> $ :<c-u>exec 'norm
'.((virtcol('$')-v:count)<0?0:(virtcol('$')-v:count)).'<bar>'<cr>

> Why is<c-u> needed there?

Since you're typing a number and then the mapping acts like
typing a colon, it will prepopulate the Ex command with the range
of lines as detailed at

:help N:

So if you type "4:" it will populate with

:.,.+3

The <c-u> is used to clear that out.

-tim


--
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: