Vim Tips Wiki
m (Console vs. GUI mappings moved to Create one mapping for both console and GUI: Page moved by JohnBot to improve title)
(Change to TipImported template + severe manual clean)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=713
 
|id=713
  +
|previous=712
|title=Console vs. GUI mappings
 
  +
|next=714
|created=May 8, 2004 11:09
+
|created=May 8, 2004
 
|complexity=basic
 
|complexity=basic
 
|author=Salman Halim
 
|author=Salman Halim
 
|version=6.0
 
|version=6.0
 
|rating=3/3
 
|rating=3/3
 
}}
|text=
 
This is a way to set mappings based on whether GUI or console Vim is running:
+
This is a way to set mappings based on whether GUI or console Vim is running:
 
   
 
<pre>
 
<pre>
function! ModeMapping( guiLhs, termLhs, rhs, ... )
+
function! ModeMapping( guiLhs, termLhs, rhs, ... )
 
let mapCommand='map'
 
if ( a:0 &gt; 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
  +
</pre>
   
 
Sample use 1:
let mapCommand='map'
 
   
  +
<pre>
 
call ModeMapping( "&lt;leader&gt;b", "&lt;leader&gt;c", ":echo 'Salman'&lt;cr&gt;" )
  +
</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.
   
 
Sample use 2:
if ( a:0 &gt; 0 )
 
   
  +
<pre>
let mapCommand=a:1
 
 
call ModeMapping( "&lt;leader&gt;a", "&lt;leader&gt;d", "&lt;esc&gt;:echo 'Halim'&lt;cr&gt;gv", 'vmap &lt;buffer&gt;' )
 
endif
 
 
 
 
if ( has( "gui_running" ) )
 
 
echo mapCommand . " " . a:guiLhs . " " . a:rhs
 
 
else
 
 
echo mapCommand . " " . a:termLhs . " " . a:rhs
 
 
endif
 
 
endfunction
 
 
</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).
   
 
==Comments==
Sample use 1:
 
 
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?
   
call ModeMapping( "&lt;leader&gt;b", "&lt;leader&gt;c", ":echo 'Salman'&lt;cr&gt;" )
 
 
 
 
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.
 
 
 
 
Sample use 2:
 
 
call ModeMapping( "&lt;leader&gt;a", "&lt;leader&gt;d", "&lt;esc&gt;:echo 'Halim'&lt;cr&gt;gv", 'vmap &lt;buffer&gt;' )
 
 
 
 
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).
 
 
 
}}
 
 
== 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:
+
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:
   
 
<pre>
 
<pre>
if ( has( "gui_running" ) )
+
if ( has( "gui_running" ) )
map lhs1 rhs
+
map lhs1 rhs
else
+
else
map lhs2 rhs
+
map lhs2 rhs
endif
+
endif
 
</pre>
 
</pre>
   
 
And my suggestion was to put it into a function (this tip).
 
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
 
 
----
 
----
<!-- parsed by vimtips.py in 0.651646 seconds-->
 

Revision as of 01:23, 11 November 2007

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

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