Wikia

Vim Tips Wiki

Changes: Easy indenting in insert and normal mode with no cursor displacement

Edit

Back to page

(Move categories to tip template)
(Remove html character entities)
 
Line 12: Line 12:
 
|category2=
 
|category2=
 
}}
 
}}
The mappings provided below are basically indentations that could easily be achieved by typing ">>" or "<<" to indent forward or backward, respectively, in normal mode with a minor difference: the cursor stays fixed relative to its location on the pre-shifted line and remains in its current mode (whether the mapping be done in normal or insert mode).
+
The mappings provided below are basically indentations that could easily be achieved by typing ">>" or "<<" to indent forward or backward, respectively, in normal mode with a minor difference: the cursor stays fixed relative to its location on the pre-shifted line and remains in its current mode (whether the mapping be done in normal or insert mode).
   
 
<pre>
 
<pre>
 
"place in vimrc
 
"place in vimrc
 
"shift line forward (Ctrl-Shift-Tab for backward shift)
 
"shift line forward (Ctrl-Shift-Tab for backward shift)
nmap &lt;C-Tab&gt; i_&lt;Esc&gt;mz:set ve=all&lt;CR&gt;o&lt;C-o&gt;`z&lt;Down&gt;_&lt;Esc&gt;:exe "normal &gt;&gt;"&lt;CR&gt;my`z:exe "normal &gt;&gt;"&lt;CR&gt;`y&lt;Up&gt;mz&lt;Down&gt;dd`z:set ve=&lt;CR&gt;i&lt;Del&gt;&lt;Right&gt;&lt;Esc&gt;
+
nmap <C-Tab> i_<Esc>mz:set ve=all<CR>o<C-o>`z<Down>_<Esc>:exe "normal >>"<CR>my`z:exe "normal >>"<CR>`y<Up>mz<Down>dd`z:set ve=<CR>i<Del><Right><Esc>
nmap &lt;C-S-Tab&gt; i_&lt;Esc&gt;mz:set ve=all&lt;CR&gt;o&lt;C-o&gt;`z&lt;Down&gt;_&lt;Esc&gt;:exe "normal &lt;&lt;"&lt;CR&gt;my`zi&lt;Del&gt;&lt;Esc&gt;:exe "normal &lt;&lt;"&lt;CR&gt;`y&lt;Up&gt;mz&lt;Down&gt;dd`z:set ve=&lt;CR&gt;:&lt;Del&gt;
+
nmap <C-S-Tab> i_<Esc>mz:set ve=all<CR>o<C-o>`z<Down>_<Esc>:exe "normal <<"<CR>my`zi<Del><Esc>:exe "normal <<"<CR>`y<Up>mz<Down>dd`z:set ve=<CR>:<Del>
imap &lt;C-Tab&gt; _&lt;Esc&gt;mz:set ve=all&lt;CR&gt;o&lt;C-o&gt;`z&lt;Down&gt;_&lt;Esc&gt;:exe "normal &gt;&gt;"&lt;CR&gt;my`z:exe "normal &gt;&gt;"&lt;CR&gt;`y&lt;Up&gt;mz&lt;Down&gt;dd`z:set ve=&lt;CR&gt;i&lt;Del&gt;
+
imap <C-Tab> _<Esc>mz:set ve=all<CR>o<C-o>`z<Down>_<Esc>:exe "normal >>"<CR>my`z:exe "normal >>"<CR>`y<Up>mz<Down>dd`z:set ve=<CR>i<Del>
imap &lt;C-S-Tab&gt; _&lt;Esc&gt;mz:set ve=all&lt;CR&gt;o&lt;C-o&gt;`z&lt;Down&gt;_&lt;Esc&gt;:exe "normal &lt;&lt;"&lt;CR&gt;my`zi&lt;Del&gt;&lt;Esc&gt;:exe "normal &lt;&lt;"&lt;CR&gt;`y&lt;Up&gt;mz&lt;Down&gt;dd`z:set ve=&lt;CR&gt;i
+
imap <C-S-Tab> _<Esc>mz:set ve=all<CR>o<C-o>`z<Down>_<Esc>:exe "normal <<"<CR>my`zi<Del><Esc>:exe "normal <<"<CR>`y<Up>mz<Down>dd`z:set ve=<CR>i
 
</pre>
 
</pre>
   
Line 34: Line 34:
   
 
<pre>
 
<pre>
nmap &lt;C-Tab&gt; a&lt;C-t&gt;&lt;Esc&gt;
+
nmap <C-Tab> a<C-t><Esc>
nmap &lt;C-S-Tab&gt; a&lt;C-d&gt;&lt;Esc&gt;
+
nmap <C-S-Tab> a<C-d><Esc>
imap &lt;C-Tab&gt; &lt;C-t&gt;
+
imap <C-Tab> <C-t>
imap &lt;C-S-Tab&gt; &lt;C-d&gt;
+
imap <C-S-Tab> <C-d>
 
</pre>
 
</pre>
   
Line 48: Line 48:
   
 
----
 
----
About &lt;F10&gt;, just ":set wak=no" and you would be able to use it.
+
About <F10>, just ":set wak=no" and you would be able to use it.
   
 
See {{help|'winaltkeys'}}.
 
See {{help|'winaltkeys'}}.
Line 56: Line 56:
   
 
<pre>
 
<pre>
vmap &lt;C-Tab&gt; &lt;Esc&gt;`&lt;i&lt;C-t&gt;&lt;C-o&gt;my&lt;C-d&gt;&lt;C-o&gt;`&gt;&lt;C-t&gt;&lt;C-o&gt;mz&lt;C-d&gt;&lt;Esc&gt;gv:&gt;&lt;CR&gt;gv`yo`z
+
vmap <C-Tab> <Esc>`<i<C-t><C-o>my<C-d><C-o>`><C-t><C-o>mz<C-d><Esc>gv:><CR>gv`yo`z
vmap &lt;C-S-Tab&gt; &lt;Esc&gt;`&lt;i&lt;C-d&gt;&lt;C-o&gt;my&lt;C-t&gt;&lt;C-o&gt;`&gt;&lt;C-d&gt;&lt;C-o&gt;mz&lt;C-t&gt;&lt;Esc&gt;gv:&lt;&lt;CR&gt;gv`yo`z
+
vmap <C-S-Tab> <Esc>`<i<C-d><C-o>my<C-t><C-o>`><C-d><C-o>mz<C-t><Esc>gv:<<CR>gv`yo`z
 
</pre>
 
</pre>
   
Line 63: Line 63:
   
 
<pre>
 
<pre>
vmap &lt;Tab&gt; :&gt;&lt;CR&gt;gv
+
vmap <Tab> :><CR>gv
vmap &lt;S-Tab&gt; :&lt;&lt;CR&gt;gv
+
vmap &lt;S-Tab> :<<CR>gv
 
</pre>
 
</pre>
   

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

Tip 909 Printable Monobook Previous Next

created April 3, 2005 · complexity basic · author Gerald Lai · version 5.7


The mappings provided below are basically indentations that could easily be achieved by typing ">>" or "<<" to indent forward or backward, respectively, in normal mode with a minor difference: the cursor stays fixed relative to its location on the pre-shifted line and remains in its current mode (whether the mapping be done in normal or insert mode).

"place in vimrc
"shift line forward (Ctrl-Shift-Tab for backward shift)
nmap <C-Tab> i_<Esc>mz:set ve=all<CR>o<C-o>`z<Down>_<Esc>:exe "normal >>"<CR>my`z:exe "normal >>"<CR>`y<Up>mz<Down>dd`z:set ve=<CR>i<Del><Right><Esc>
nmap <C-S-Tab> i_<Esc>mz:set ve=all<CR>o<C-o>`z<Down>_<Esc>:exe "normal <<"<CR>my`zi<Del><Esc>:exe "normal <<"<CR>`y<Up>mz<Down>dd`z:set ve=<CR>:<Del>
imap <C-Tab> _<Esc>mz:set ve=all<CR>o<C-o>`z<Down>_<Esc>:exe "normal >>"<CR>my`z:exe "normal >>"<CR>`y<Up>mz<Down>dd`z:set ve=<CR>i<Del>
imap <C-S-Tab> _<Esc>mz:set ve=all<CR>o<C-o>`z<Down>_<Esc>:exe "normal <<"<CR>my`zi<Del><Esc>:exe "normal <<"<CR>`y<Up>mz<Down>dd`z:set ve=<CR>i

The mappings of Ctrl(-Shift)-Tab may not work with terminal versions of Vim (in which case, switch mappings to another key sequence). Works fine with gvim.

The imaps are for insert mode, not while in replace mode.

CommentsEdit

This may be done much easier using ctrl-t and ctrl-d (in insert mode).


Please disregard the tip above. Ctrl-T and Ctrl-D in insert mode does what I was looking for. I have changed my mappings to:

nmap <C-Tab> a<C-t><Esc>
nmap <C-S-Tab> a<C-d><Esc>
imap <C-Tab> <C-t>
imap <C-S-Tab> <C-d>

NOTE: Using ^TAB might be a bad choice! ^TAB (i.e. control TAB) is used to toggle among the active windows you have. I was trying to bind these to F9 and F10.

F10 seems to be bound by gvim always to doing something like the ALT key. Press F10 and the memu bar becomes active. Is there anyway to turn that off? I think F9 and F10 would be best for me. F9 shifting left and F10 shifting right.

If F10 is not changable, I could use F8 and F9. however I use F8 for something else presently


About <F10>, just ":set wak=no" and you would be able to use it.

See :help 'winaltkeys'.


Here are visual mappings that do the same indentation trick. Just grab the lines you want to indent with a visual block strip (Ctrl-V and select a vertical visual). The visual selection stays fixed relative to the indented line(s) (i.e. it follows the indentation) .

vmap <C-Tab> <Esc>`<i<C-t><C-o>my<C-d><C-o>`><C-t><C-o>mz<C-d><Esc>gv:><CR>gv`yo`z
vmap <C-S-Tab> <Esc>`<i<C-d><C-o>my<C-t><C-o>`><C-d><C-o>mz<C-t><Esc>gv:<<CR>gv`yo`z

A simple way of doing the above would be:

vmap <Tab> :><CR>gv
vmap <S-Tab> :<<CR>gv

but the visual selection remains where it was first selected.


Around Wikia's network

Random Wiki