Wikia

Vim Tips Wiki

Easy indenting in insert and normal mode with no cursor displacement

Talk0
1,610pages on
this wiki
Revision as of 23:29, September 29, 2008 by JohnBot (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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.


Advertisement | Your ad here

Around Wikia's network

Random Wiki