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).
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).