Wikia

Vim Tips Wiki

Changes: Create one mapping for both console and GUI

Edit

Back to page

(Move categories to tip template)
(Remove html character entities)
 
Line 17: Line 17:
 
function! ModeMapping( guiLhs, termLhs, rhs, ... )
 
function! ModeMapping( guiLhs, termLhs, rhs, ... )
 
let mapCommand='map'
 
let mapCommand='map'
if ( a:0 > 0 )
+
if ( a:0 > 0 )
 
let mapCommand=a:1
 
let mapCommand=a:1
 
endif
 
endif
Line 31: Line 31:
   
 
<pre>
 
<pre>
call ModeMapping( "&lt;leader&gt;b", "&lt;leader&gt;c", ":echo 'Salman'&lt;cr&gt;" )
+
call ModeMapping( "<Leader>b", "<Leader>c", ":echo 'Salman'<CR>" )
 
</pre>
 
</pre>
   
This means that if GUI is running, &lt;leader&gt;b becomes the lhs and the :echo bit becomes the rhs; if no GUI is running, you get &lt;leader&gt;c as the lhs instead.
+
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:
 
Sample use 2:
   
 
<pre>
 
<pre>
call ModeMapping( "&lt;leader&gt;a", "&lt;leader&gt;d", "&lt;esc&gt;:echo 'Halim'&lt;cr&gt;gv", 'vmap &lt;buffer&gt;' )
+
call ModeMapping( "<Leader>a", "<Leader>d", "<Esc>:echo 'Halim'<CR>gv", 'vmap <buffer>' )
 
</pre>
 
</pre>
   
If the GUI is running, &lt;leader&gt;a is the lhs, &lt;esc&gt;:echo etc. is the rhs and the mapp command used is 'vmap &lt;buffer&gt;' (a buffer-specific visual mode mapping). Note that the last argument is optional (and wasn't there in the last example).
+
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==
 
==Comments==

Latest revision as of 09:16, September 29, 2008

Tip 713 Printable Monobook Previous Next

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

CommentsEdit

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


Around Wikia's network

Random Wiki