Vim Tips Wiki
No edit summary
(Remove html character entities)
Line 21: Line 21:
 
endif
 
endif
 
endfunction
 
endfunction
nnoremap <silent> <Home> :call SmartHome()<CR>
+
nnoremap <silent> <Home> :call SmartHome()<CR>
inoremap &lt;silent&gt; &lt;Home&gt; &lt;C-O&gt;:call SmartHome()&lt;CR&gt;
+
inoremap <silent> <Home> <C-O>:call SmartHome()<CR>
 
</pre>
 
</pre>
   
 
== See also ==
 
== See also ==
* [http://code.google.com/p/lh-vim/source/browse/cpp/trunk/plugin/homeLikeVC++.vim homeLikeVC++.vim] provides the same feature in a slightly different way. Mappings for visual mode, and a smart-<tt>&lt;end&gt;</tt> are also provided.
+
* [http://code.google.com/p/lh-vim/source/browse/cpp/trunk/plugin/homeLikeVC++.vim homeLikeVC++.vim] provides the same feature in a slightly different way. Mappings for visual mode, and a smart-<tt><End></tt> are also provided.
   
 
==Comments==
 
==Comments==
Line 42: Line 42:
 
See the latest version of homeLikeVC++ (the link has been updated)
 
See the latest version of homeLikeVC++ (the link has been updated)
 
----
 
----
We don't need to use functions, <tt>:normal!</tt>, or <tt>@=</tt> if we use <tt>&lt;expr&gt;</tt> maps. These maps should work properly in normal mode, visual mode, insert mode, select mode, and operator-pending mode:
+
We don't need to use functions, <tt>:normal!</tt>, or <tt>@=</tt> if we use <tt><expr></tt> maps. These maps should work properly in normal mode, visual mode, insert mode, select mode, and operator-pending mode:
 
<pre>
 
<pre>
 
noremap <expr> <Home> (col('.') == matchend(getline('.'), '^\s*')+1 ? '0' : '^')
 
noremap <expr> <Home> (col('.') == matchend(getline('.'), '^\s*')+1 ? '0' : '^')

Revision as of 05:34, 29 September 2008

Tip 315 Printable Monobook Previous Next

created August 14, 2002 · complexity advanced · author Alex A. Naanou · version 5.7


Put the following in your vimrc. When you press the Home key, the cursor will move to the first nonblank character on the line, or, if already at that position, the cursor will move to the first character.

function! SmartHome()
  let s:col = col(".")
  normal! ^
  if s:col == col(".")
    normal! 0
  endif
endfunction
nnoremap <silent> <Home> :call SmartHome()<CR>
inoremap <silent> <Home> <C-O>:call SmartHome()<CR>

See also

  • homeLikeVC++.vim provides the same feature in a slightly different way. Mappings for visual mode, and a smart-<End> are also provided.

Comments

If you don't mind smashing a mark ('h in this case), you can use the map in visual mode, too:

vnoremap <silent> <Home> <Esc>:call <SID>SmartHome()<CR>mhgvg`h

There is no need to smash any mark if we:

  • return what must be executed instead of playing with :normal!
  • and then use @=

See the latest version of homeLikeVC++ (the link has been updated)


We don't need to use functions, :normal!, or @= if we use <expr> maps. These maps should work properly in normal mode, visual mode, insert mode, select mode, and operator-pending mode:

noremap  <expr> <Home> (col('.') == matchend(getline('.'), '^\s*')+1 ? '0'  : '^')
noremap  <expr> <End>  (col('.') == match(getline('.'),    '\s*$')   ? '$'  : 'g_')
vnoremap <expr> <End>  (col('.') == match(getline('.'),    '\s*$')   ? '$h' : 'g_')
imap <Home> <C-o><Home>
imap <End>  <C-o><End>