Wikia

Vim Tips Wiki

Changes: A better gm command

Edit

Back to page

(Assign tip id + convert to TipNew template + minor clean.)
(Change <tt> to <code>, perhaps also minor tweak.)
Line 11: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
In normal mode, typing <tt>gm</tt> moves the cursor to the middle of the current ''screen'' line. With <tt>'wrap'</tt> on, a long line may be wrapped so it appears on several screen lines. Typing <tt>gm</tt> moves the cursor to the middle of the current line on the screen (or to the end of the line, if it ends before the middle of the screen).
+
In normal mode, typing <code>gm</code> moves the cursor to the middle of the current ''screen'' line. With <code>'wrap'</code> on, a long line may be wrapped so it appears on several screen lines. Typing <code>gm</code> moves the cursor to the middle of the current line on the screen (or to the end of the line, if it ends before the middle of the screen).
   
This tips remaps <tt>gm</tt> so the cursor is moved to the middle of the current physical line. Any leading or trailing whitespace is ignored: the cursor moves to the middle of the text between the first and last non-whitespace characters.
+
This tips remaps <code>gm</code> so the cursor is moved to the middle of the current physical line. Any leading or trailing whitespace is ignored: the cursor moves to the middle of the text between the first and last non-whitespace characters.
   
 
==Code==
 
==Code==

Revision as of 06:46, July 13, 2012

Tip 1671 Printable Monobook Previous Next

created March 15, 2011 · complexity basic · author Kurkale6ka · version 7.0


In normal mode, typing gm moves the cursor to the middle of the current screen line. With 'wrap' on, a long line may be wrapped so it appears on several screen lines. Typing gm moves the cursor to the middle of the current line on the screen (or to the end of the line, if it ends before the middle of the screen).

This tips remaps gm so the cursor is moved to the middle of the current physical line. Any leading or trailing whitespace is ignored: the cursor moves to the middle of the text between the first and last non-whitespace characters.

Code

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>

References

Comments

I have removed the following from the tip because I can't make it do anything useful. If an explanation or fix is available, please restore it.

onoremap <silent> gm :call <SID>Gm()<CR>
" xmap <silent> gm :call <SID>Gm()<CR>

JohnBeckett 08:22, March 31, 2012 (UTC)

Around Wikia's network

Random Wiki