Wednesday, October 27, 2010

Re: For languages that end statements with semicolons: d$-1

On Wed, October 27, 2010 11:42 am, Dotan Cohen wrote:
> Does VIM have an easy way to delete up to the last character of a
> line, not inclusive?
>
> How about not including the last N characters? For instance, in C#
> there may be a statement in which I wish to delete all but the last 3
> characters, such as:
> Console.WriteLine("Lots of text here that I want to delete");
>
> Something like d$N where N is the amount of characters to keep (d$1
> for the first case and d$3 for the second case) would be great. Thanks
> for any ideas.

Interessting challenge. Here we go:

fun! s:GetOffset()
let offset=''
let char=nr2char(getchar())
while char =~? '[-0-9]'
let offset .= char
let char=nr2char(getchar())
endwhile
return [ abs(offset+0)*-1, char ]
endfun

fun! s:LastChar(offset)
if a:offset[0] < 0
echomsg a:offset[0] . '|' . a:offset[1]
return string(virtcol('$') + a:offset[0]) . '|' . a:offset[1]
else
return '$' . a:offset[1]
endif
endfun

onoremap <silent> <expr> $ <sid>LastChar(<sid>GetOffset())

regards,
Christian

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