17

By pressing gg and G you can jump to both ends of a buffer.

42G jumps to the line 42.

Is there any quick g shortcut to jump to the middle of buffer. Something like roughly round(number of lines/2)G ?

3 Answers 3

34

You can use N% to jump to the Nth percent of the file. So e.g. 50% jumps roughly to the middle of the buffer. See the help at :h N%

3

I use these mappings in my own settings:

nnoremap <expr> M (line('$')/2).'G'
xnoremap <expr> M (line('$')/2).'G'
onoremap <expr> M (line('$')/2).'G'

It's pretty simple, it simply evaluates the expression (line('$')/2).'G' before running the mapping. line('$') is the number of the last line in the buffer, which we divide by two. Then the .'G' just appends 'G' to the resulting string.

If you like using the default M (Middle of visible screen), you could make this a leader mapping so that you don't overwrite it. For example:

nnoremap <expr> <leader>m (line('$')/2).'G'
xnoremap <expr> <leader>m (line('$')/2).'G'
onoremap <expr> <leader>m (line('$')/2).'G'
-1

You can use N% to jump to the Nth percent of the file. So e.g. 50% jumps roughly to the middle of the buffer.

See the help at :h N%

1
  • I edited the answer because it appeared in the "first post" queue to make it readable (the formatting was terrible) but I didn't see it is just a duplicate of Christian's answer.
    – statox
    Dec 2, 2016 at 9:13

Your Answer

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

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