sponsor Vim development Vim logo Vim Book Ad

basic Tip #592: Smart <Home> and <End> keymaps

 tip karma   Rating 23/12, Viewed by 1617 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   October 26, 2003 2:30      complexity:   basic
author:   Fabio G. Strozzi      as of Vim:   5.7

I found interesting the way <Home> and <End> keys act in some editors and I wondered why try not
carry it out with Vim.
Put the following lines in your ~/.vimrc and so <Home> will move the cursor between the first column and
the first non-blank character. <End> is similar but work at the end of the line.
I think this should be very useful to the indented code and trailing characters.

let g:home_key = '0'
let g:end_key = '$'

function ToggleHome()
  exe "normal! \<Esc>".g:home_key
  let g:home_key = g:home_key == '0'? '^' : '0'

function ToggleEnd()
  exe "normal! \<Esc>".g:end_key
  let g:end_key = g:end_key == '$'? 'g_' : '$'

inoremap <silent> <Home> <Esc>:call ToggleHome()<CR>i
nnoremap <silent> <Home> :call ToggleHome()<CR>
inoremap <silent> <End> <Esc>:call ToggleEnd()<CR>a
nnoremap <silent> <End> :call ToggleEnd()<CR>

 rate this tip  Life Changing Helpful Unfulfilling 

<< Have a nice and easy use of plugins | basic postfix abbreviations >>

Additional Notes

hermitte {at} free {dot} fr, October 26, 2003 12:26
This mapping is not very handy as it is does take the position of the cursor into account.

The function you are looking for is available here into a plugin that define some key bindings inspired by VC++, and as a standalone into:

It will be easy to fix the mapping for <end> as well.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge.net Logo