5

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?

7

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.

  • Nice (+1). - Have you an idea why the macros don't work if in line 1 of the data file? – Janis Jul 19 '15 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 '15 at 13:13
  • @Janis I found a version that accepts counters, but it seems unnecessarily complicated. shrug – lcd047 Jul 19 '15 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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