Vim Tips Wiki
Advertisement

Previous TipNext Tip

Tip: #713 - Create one mapping for both console and GUI

Created: May 8, 2004 11:09 Complexity: basic Author: Salman Halim Version: 6.0 Karma: 3/3 Imported from: Tip#713

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?

-- Mark

mark woodward At internode Dot on Dot net , May 9, 2004 5:50


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

salmanhalim--AT--hotmail.com , May 9, 2004 9:14


Ah.. OK, I new there had to be a reason. Thanks for the clarification. I tend to duplicate things in vimrc and gvimrc but can see now how this could be used.

-- Mark

mark woodward At internode Dot on Dot net , May 9, 2004 11:20


Advertisement