22

Is there a way I can move my cursor to the middle of the current line? By middle, I don't mean the middle of the screen, but the middle of the line of text, regardless of length.

The shortcut M moves the cursor to the middle of the screen - is there an equivalent shortcut to move the cursor to the middle of the line?

3
  • 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
16

I don't know of any builtin ways to do this. However, you can easily make a map to do it:

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

Ingo Karkat proposed the following variant in the comments 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 using two mappings, one for each mode:

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

The built-in gm does exactly this.

5
  • 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
5

As of October 2019, we have gM, which moves the cursor to the middle of the line. This requires Vim 8.1.2231 or later.

4

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.

1

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 . '|'
endfunction
nnoremap <silent> gm :call <SID>Gm()<CR>

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

0

Download and install vim 8.2

To move your cursor to the middle of a line, enter gM

(gM is not in vim 8.1)

4
  • Welcome to Vi and Vim!
    – D. Ben Knoble
    Jul 11 at 16:28
  • 3
    I think the majority of your answer is covered by this one: vi.stackexchange.com/a/22397/10604
    – D. Ben Knoble
    Jul 11 at 16:29
  • 1
    "gM is not in vim 8.1" - technically wrong. It was added in 8.1.2231, which is still 8.1, but a fairly late version of 8.1. (8.1.2424 was the last patch 8.1 received)
    – Zoe
    Jul 11 at 16:29
  • @D.BenKnoble @Zoe You are right. There were two different versions of Vim on my two different machines - 8.1 on one, 8.2 on the other. Sorry I did not notice the detailed version numbers. I tried it on both machines. gM worked on my 8.2 machine, but did not work on my 8.1. I upgraded 8.1 to 8.2, then gM worked. Jul 11 at 18:59

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.