Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

vim: ruler offset

22 views
Skip to first unread message

Christopher Collins

unread,
May 10, 2013, 11:30:20 AM5/10/13
to
In vim, ":set ruler" causes the cursor position to be printed at the
bottom of the window. Is it possible to apply a constant offset to the
coordinates? E.g., if I applied (-2, 0), and my cursor were at position
(10,10), then "8,10" would be displayed instead of the actual cursor
position.

Here is some context. I have a C source file with an array of strings,
e.g.,:

const char *strings[] = {
"123456",
"abcdef",
"ABCDEF",
};

Elsewhere, I need to reference specific elements of the above block of
text with x-y coordinates. E.g., (5,0) would refer to the '6'
character. If I could configure vim such that it displayed "0,0" when
the cursor is on the '1' character, it would make this process easier.

All suggestions appreciated.

Thanks,
Chris

Michael Soyka

unread,
May 11, 2013, 12:19:37 PM5/11/13
to
Chris,

Trying putting this in your vimrc:

function! ShowOffset()
let colOffset = virtcol(".") - g:referenceColumn
let lineOffset = line(".") - g:referenceLineNum
let text = '(' . string(colOffset) . ',' . string(lineOffset) . ')'
return text
endfunction
function! SetRuler()
let g:referenceColumn = virtcol(".")
let g:referenceLineNum = line(".")
set ruler
set rulerformat=%{ShowOffset()}
endfunction
function! UnsetRuler()
set rulerformat&
endfunction
nmap <Leader>s :call SetRuler()<CR>
nmap <Leader>u :call UnsetRuler()<CR>


To use this, put the cursor on the "1" in "123456" and press \s. This
sets a "reference" position and changes the ruler appearance. Use \u to
revert to the default ruler.

Mike

Christopher Collins

unread,
May 12, 2013, 3:20:10 AM5/12/13
to
On 2013-05-11, Michael Soyka <mss...@gmail.com> wrote:
> Chris,
>
> Trying putting this in your vimrc:
>
> function! ShowOffset()
> let colOffset = virtcol(".") - g:referenceColumn
> let lineOffset = line(".") - g:referenceLineNum
> let text = '(' . string(colOffset) . ',' . string(lineOffset) . ')'
> return text
> endfunction
> function! SetRuler()
> let g:referenceColumn = virtcol(".")
> let g:referenceLineNum = line(".")
> set ruler
> set rulerformat=%{ShowOffset()}
> endfunction
> function! UnsetRuler()
> set rulerformat&
> endfunction
> nmap <Leader>s :call SetRuler()<CR>
> nmap <Leader>u :call UnsetRuler()<CR>
>
>
> To use this, put the cursor on the "1" in "123456" and press \s. This
> sets a "reference" position and changes the ruler appearance. Use \u to
> revert to the default ruler.
>
> Mike

Very cool, it works perfectly. Thanks, Mike!
0 new messages