Is there a way I can move my cursor to the middle of the current line? (the middle of the line of text - not middle of screen width)

I was wondering if there was a shortcut similar to M that moves cursor to the middle of the screen.

EDIT: Added clarification inside parentheses

  • 2
    So is middle middle of the full line or middle of the screen width? – muru Mar 10 '15 at 16:26
  • @muru Was thinking of middle of full line – thameera Mar 11 '15 at 3:41
  • 2
    Could you add that to the question as well? And what do you expect to happen with wrapping? – muru Mar 11 '15 at 3:45

I don' know any builtin way to do this, however, you can easily have a map to do it:

nnoremap µ :exe 'normal! '.(virtcol('$')/2).'\|'<cr>

@Ingo Karkat proposed the following variant that also works in VISUAL-mode.

noremap <expr> gM (virtcol('$') / 2) . '<Bar>'

Unfortunately, it's also triggered in SELECT-mode. As a consequence, I prefer the couple:

nnoremap <expr> gM (virtcol('$') / 2) . '<Bar>'
xnoremap <expr> gM (virtcol('$') / 2) . '<Bar>'
  • 3
    This variant will also work correctly for visual mode: noremap <expr> gM (virtcol('$') / 2) . '<Bar>' – Ingo Karkat May 13 '15 at 18:16

The built-in gm does exactly this.

  • 9
    Note that this takes you to the middle of the line with respect to the console width, not with respect to the line length. So, if you have a line of 10 characters and a console that is 80 characters wide, the cursor will end up on the 10th character in the line (because it's "trying" to get to the 40th), not the 5th. – apnorton Mar 10 '15 at 14:29
  • 3
    Yep. From the question, it is not clear whether we're looking for the middle of the line or the middle of the screen. Based on the analogy to M, I assumed screen. – tommcdo Mar 10 '15 at 14:32
  • 1
    Actually I just tried this on a wide (about 180 chars) screen with 80-char text. The cursor went to the end of the line. Doesn't seem a very useful command. – Ben Mar 12 '15 at 13:47
  • @Ben Did it go to the end of the line, or did it go as close to the center of the screen width as it could get? Try it with a line that's more than 50% as wide as the screen, or try it with :set virtualedit=all. – 8bittree Sep 21 '17 at 19:51
  • Yes but the OP was asking for a command to jump to the middle of the line text, not the middle of the screen. So it doesn't answer the question. – Ben Sep 22 '17 at 4:21

gM moves the cursor to halfway the text of the line: https://vimhelp.org/motion.txt.html#gM

This has been implemented in October 2019: https://github.com/manuelschiller/vim/commit/a267c57acdd8843a91eed2cede6ef02d970147fd


To add to Luc's answer, there's also the Easymotion plugin, which makes moving quickly to a specific position a breeze. Pressing <Leader><Leader>w will highlight which key you can press to move to a specific word. That way you could easily move to a word in the middle of the current line.


If you want to go to the middle of the text (not taking into account the whitespace in the beginning of the line), this will work:

function! s:Gm()
  execute 'normal! ^'
  let first_col = virtcol('.')
  execute 'normal! g_'
  let last_col  = virtcol('.')
  execute 'normal! ' . (first_col + last_col) / 2 . '|'
nnoremap <silent> gm :call <SID>Gm()<CR>

I don't think this will work in visual mode, however.

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.