Wikia

Vim Tips Wiki

Changes: Fast one session mapping

Edit

Back to page

(Move categories to tip template)
(Remove html character entities)
 
Line 20: Line 20:
 
" vim supports this by @: but there are cases when I want to press one key only
 
" vim supports this by @: but there are cases when I want to press one key only
 
" and other cases I want to user other commands as well.
 
" and other cases I want to user other commands as well.
" Having to type :map <F5> :<cmd><CR> does need some time and I'm lazy ;-)
+
" Having to type :map <F5> :<cmd><CR> does need some time and I'm lazy ;-)
 
" So just copy this text, put it into .vim/plugins/mapFromF1ToF12.vim and enjoy
 
" So just copy this text, put it into .vim/plugins/mapFromF1ToF12.vim and enjoy
 
" Try
 
" Try
" :echo "dummy"&lt;CR&gt;
+
" :echo "dummy"<CR>
" &lt;F5&gt;
+
" <F5>
 
" now you can use F5 to execute :echo "dummy"
 
" now you can use F5 to execute :echo "dummy"
 
" Perhaps you want to have mappings local to your buffer only.
 
" Perhaps you want to have mappings local to your buffer only.
" Then use map &lt;buffer&gt; and unmap &lt;buffer&gt; instead
+
" Then use map <buffer> and unmap <buffer> instead
   
 
function MapLastCommandToKeys(keysToMapTo)
 
function MapLastCommandToKeys(keysToMapTo)
 
exe "unmap ".a:keysToMapTo
 
exe "unmap ".a:keysToMapTo
exe "map ".a:keysToMapTo." :".histget("cmd")."&lt;CR&gt;"
+
exe "map ".a:keysToMapTo." :".histget("cmd")."<CR>"
 
endfunction
 
endfunction
   
 
function PrepareMap(keysToMapTo)
 
function PrepareMap(keysToMapTo)
exe "map ".a:keysToMapTo." :call MapLastCommandToKeys('".a:keysToMapTo."')&lt;CR&gt;"
+
exe "map ".a:keysToMapTo." :call MapLastCommandToKeys('".a:keysToMapTo."')<CR>"
 
endfunction
 
endfunction
   
 
" This will allow you to use F1
 
" This will allow you to use F1
PrepareMap('&lt;F1&gt;')
+
PrepareMap('<F1>')
 
" This while loop will allow you to use F2,F3,..,F12
 
" This while loop will allow you to use F2,F3,..,F12
 
let i=2
 
let i=2
while i&lt;13
+
while i<13
call PrepareMap('&lt;F'.i.'&gt;')
+
call PrepareMap('<F'.i.'>')
 
let i=i+1
 
let i=i+1
 
endwhile
 
endwhile

Latest revision as of 23:56, September 29, 2008

Tip 1127 Printable Monobook Previous Next

created February 10, 2006 · complexity basic · author Marc Weber · version 5.7


" A little nice idea I got today...
"
" I've noticed that I often use one session mappings .. Sometimes :bn
" sometimes :make specialtarget
" in each case I need it often.
" vim supports this by @: but there are cases when I want to press one key only
" and other cases I want to user other commands as well.
" Having to type :map <F5> :<cmd><CR> does need some time and I'm lazy ;-)
" So just copy this text, put it into .vim/plugins/mapFromF1ToF12.vim and enjoy
" Try
" :echo "dummy"<CR>
" <F5>
" now you can use F5 to execute :echo "dummy"
" Perhaps you want to have mappings local to your buffer only.
" Then use map <buffer> and unmap <buffer> instead

function MapLastCommandToKeys(keysToMapTo)
  exe "unmap ".a:keysToMapTo
  exe "map ".a:keysToMapTo." :".histget("cmd")."<CR>"
endfunction

function PrepareMap(keysToMapTo)
  exe "map ".a:keysToMapTo." :call MapLastCommandToKeys('".a:keysToMapTo."')<CR>"
endfunction

" This will allow you to use F1
PrepareMap('<F1>')
" This while loop will allow you to use F2,F3,..,F12
let i=2
while i<13
  call PrepareMap('<F'.i.'>')
  let i=i+1
endwhile
" of cause you can use any mapping eg \a:
PrepareMap("\a")

CommentsEdit

Recordings can do much more (encompassing other modes) than handle :cmd-line executions. They can also be repeated. See :help complex-repeat.

From normal mode,

  1. To record to register a: 'qa'
  2. Perform any keystrokes and :cmd-line executions.
  3. To stop recording: 'q'
  4. To perform recording 2 times: '2@a'
  5. To perform recording 55 more times: '55@@'

Around Wikia's network

Random Wiki