Wikia

Vim Tips Wiki

Changes: Quick generic option toggling

Edit

Back to page

(Move categories to tip template)
(Remove html character entities)
 
Line 14: Line 14:
   
 
<pre>
 
<pre>
map &lt;F2&gt; :set wrap!&lt;CR&gt;
+
map <F2> :set wrap!<CR>
 
</pre>
 
</pre>
   
 
Nicer, more elaborate versions of this idea exist, such as:
 
Nicer, more elaborate versions of this idea exist, such as:
 
<pre>
 
<pre>
nnoremap &lt;F8&gt; :set wrap! wrap?&lt;CR&gt;
+
nnoremap <F8> :set wrap! wrap?<CR>
imap &lt;F8&gt; &lt;C-O&gt;&lt;F8&gt;
+
imap <F8> <C-O><F8>
 
</pre>
 
</pre>
   
Line 32: Line 32:
 
" Map key to toggle opt
 
" Map key to toggle opt
 
function MapToggle(key, opt)
 
function MapToggle(key, opt)
let cmd = ':set '.a:opt.'! \| set '.a:opt."?\&lt;CR&gt;"
+
let cmd = ':set '.a:opt.'! \| set '.a:opt."?\<CR>"
 
exec 'nnoremap '.a:key.' '.cmd
 
exec 'nnoremap '.a:key.' '.cmd
exec 'inoremap '.a:key." \&lt;C-O&gt;".cmd
+
exec 'inoremap '.a:key." \<C-O>".cmd
 
endfunction
 
endfunction
command -nargs=+ MapToggle call MapToggle(&lt;f-args&gt;)
+
command -nargs=+ MapToggle call MapToggle(<f-args>)
   
 
" Display-altering option toggles
 
" Display-altering option toggles
MapToggle &lt;F1&gt; hlsearch
+
MapToggle <F1> hlsearch
MapToggle &lt;F2&gt; wrap
+
MapToggle <F2> wrap
MapToggle &lt;F3&gt; list
+
MapToggle <F3> list
   
 
" Behavior-altering option toggles
 
" Behavior-altering option toggles
MapToggle &lt;F10&gt; scrollbind
+
MapToggle <F10> scrollbind
MapToggle &lt;F11&gt; ignorecase
+
MapToggle <F11> ignorecase
MapToggle &lt;F12&gt; paste
+
MapToggle <F12> paste
set pastetoggle=&lt;F12&gt;
+
set pastetoggle=<F12>
 
</pre>
 
</pre>
   
Line 54: Line 54:
   
 
==Comments==
 
==Comments==
 
----
 

Latest revision as of 23:32, September 29, 2008

Tip 920 Printable Monobook Previous Next

created April 28, 2005 · complexity intermediate · author Piet Delport · version 5.7


A fairly well-known mapping to toggle boolean options is:

map <F2> :set wrap!<CR>

Nicer, more elaborate versions of this idea exist, such as:

nnoremap <F8> :set wrap! wrap?<CR>
imap <F8> <C-O><F8>

which displays the current value of the option after toggling it, and works both in normal and insert mode.

However, that definition quickly clutters up your vimrc, and is a pain to edit due to the redundant occurrences of the toggle key and option.

Wrapping the definition in a function solves the problem, and makes the toggle definition a one-line operation again:

" Map key to toggle opt
function MapToggle(key, opt)
  let cmd = ':set '.a:opt.'! \| set '.a:opt."?\<CR>"
  exec 'nnoremap '.a:key.' '.cmd
  exec 'inoremap '.a:key." \<C-O>".cmd
endfunction
command -nargs=+ MapToggle call MapToggle(<f-args>)

" Display-altering option toggles
MapToggle <F1> hlsearch
MapToggle <F2> wrap
MapToggle <F3> list

" Behavior-altering option toggles
MapToggle <F10> scrollbind
MapToggle <F11> ignorecase
MapToggle <F12> paste
set pastetoggle=<F12>

See alsoEdit

CommentsEdit

Around Wikia's network

Random Wiki