16

I know 0 goes to the beginning of line and $ goes to the end, but is there something for the middle?

14
:call cursor(0, len(getline('.'))/2)
4
  • 4
    This would be fairly useful if it were mapped, e.g: :nnoremap gm :call cursor(0, len(getline('.'))/2)<cr>
    – Heptite
    Nov 29 '10 at 22:55
  • 4
    virtcol('$')/2 is enough. Nov 30 '10 at 1:34
  • @Luc Hermitte: Nice, but not exactly the best behavior when hard tabs are involved.
    – ephemient
    Nov 30 '10 at 3:54
  • That's why I use virtcol instead of col. :). But indeed we should use | and not cursor(). BTW, len() won't give any better result. Moreover, len will give other troubles with multi-bytes characters. Nov 30 '10 at 9:05
10

Typing gm would do it, but it moves by screen lines (see :help gm). In order to get it working with text lines, one can remap this command:

map gm :call cursor(0, virtcol('$')/2)<CR>
3
  • gm seems to move cursor to middle of screen, but if my line of text is shorter than the length of the screen, it doesn't take me to the middle. Sorry for not clarifying.
    – teggy
    Nov 29 '10 at 20:56
  • @teggy: I see. I modified my solution to remap gm to go to the middle of text line. As you can see, it combines solutions from ephemient and Luc Hermitte.
    – JooMing
    Nov 30 '10 at 9:55
  • As ephemient pointed out, it still has an issue with hard tarbs as cursor() counts them as one character each. Moreover, it won't work with multi-bytes characters either. Nov 30 '10 at 10:24
7

Here is a solution that will respect hardtabs, and multi-bytes characters as well.

:exe 'normal '.(virtcol('$')/2).'|'
1

Here is the best solution that I have found. Jumping exactly to the middle of the line is not very practical. You will most likely still need to move the cursor a few positions after moving to the middle. What if you could jump to a specific character? Many people just search for the character, then hit "next" until they reach it. This is slow. The Easy-motion plugin offers a better solution.

My favorite is <leader><leader>s then the character you want to jump to. Easy Motion replaces all matches with different letters for you to choose from. See the tutorial. Its a life-changing plugin...

1

Personally I use:

map g1 :call cursor(0, virtcol('$')/0)<CR>
map g2 :call cursor(0, virtcol('$')/4)<CR>
map g3 :call cursor(0, virtcol('$')/2)<CR>
map g4 :call cursor(0, virtcol('$')/3*2)<CR>
map g5 :call cursor(0, virtcol('$'))<CR>

To center vertically 0% - 25% - 50% - 75% - 100%

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lectus sapien, rutrum quis vestibulum vitae, fringilla ac nisi.

For example if you wanna go to the word 'adipiscing'. Then you press g2w (go 25% + one word).

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.