7

I know that you can go to the Nth % percentage of a file by typing N% in normal mode. Is it possible to advance the line by a given percentage?

To clarify, for an example, can I type something to advance to 10% of the file, then the same thing to advance to 20% and so on?

1 Answer 1

9

Sure, you can do it like this:

function! <SID>PercentUp()
    execute 'normal ' . max([line('.') * 100 / line('$') - v:count1 * 10, 0]) . '%'
endfunction

function! <SID>PercentDown()
    execute 'normal ' . min([line('.') * 100 / line('$') + v:count1 * 10, 100]) . '%'
endfunction

nnoremap <silent> <leader>- :<C-u>call <SID>PercentUp()<CR>
nnoremap <silent> <leader>+ :<C-u>call <SID>PercentDown()<CR>

With these \- will recede by 10%, while \+ will advance by 10%. The macros accept counters.

3
  • Nice (+1). - Have you an idea why the macros don't work if in line 1 of the data file?
    – Janis
    Jul 19, 2015 at 12:58
  • @Janis Apparently the <C-u> didn't work the way I imagined it. Without it the macros work from everywhere (well, hopefully), but they no longer take a count. I'm not sure I understand why though.
    – lcd047
    Jul 19, 2015 at 13:13
  • @Janis I found a version that accepts counters, but it seems unnecessarily complicated. shrug
    – lcd047
    Jul 19, 2015 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.