created May 8, 2004 · complexity basic · author Salman Halim · version 6.0
This is a way to set mappings based on whether GUI or console Vim is running:
function! ModeMapping( guiLhs, termLhs, rhs, ... ) let mapCommand='map' if ( a:0 > 0 ) let mapCommand=a:1 endif if ( has( "gui_running" ) ) echo mapCommand . " " . a:guiLhs . " " . a:rhs else echo mapCommand . " " . a:termLhs . " " . a:rhs endif endfunction
Sample use 1:
call ModeMapping( "<Leader>b", "<Leader>c", ":echo 'Salman'<CR>" )
This means that if GUI is running, <Leader>b becomes the lhs and the :echo bit becomes the rhs; if no GUI is running, you get <Leader>c as the lhs instead.
Sample use 2:
call ModeMapping( "<Leader>a", "<Leader>d", "<Esc>:echo 'Halim'<CR>gv", 'vmap <buffer>' )
If the GUI is running, <Leader>a is the lhs, <Esc>:echo etc. is the rhs and the mapp command used is 'vmap <buffer>' (a buffer-specific visual mode mapping). Note that the last argument is optional (and wasn't there in the last example).
Comments[]
I'm not sure I understand this. Isn't this what the vimrc and gvimrc files are for? Put any gui specific maps in gvimrc, others in vimrc?
The point of this is to avoid having to create duplicate mappings with the same rhs; doing it in one place makes maintenance easier. Basically, someone had asked the question on the mailing list of how they could avoid the following construct:
if ( has( "gui_running" ) ) map lhs1 rhs else map lhs2 rhs endif
And my suggestion was to put it into a function (this tip).