History Report a problem
Article Edit this page Discussion

Easy indenting in insert and normal mode with no cursor displacement

From Vim Tips Wiki

(Redirected from VimTip909)
Jump to: navigation, search

Tip 909 Previous TipNext Tip

Created: April 3, 2005 Complexity: basic Author: Gerald Lai Minimum version: 5.7 Karma: 10/7 Imported from: Tip#909


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.

[edit] Comments

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.


Rate this article:

Share this article:

Hubs Highlights International Sites Wikia messages
Entertainment
Gaming
Cartoons & Comics
Science Fiction
Hobbies
Sports
See all...
Grand Theft Auto
Pushing Daisies
Legend of Zelda Wiki
Terminator Wiki
Everquest II Wiki
Godzilla
German
Spanish
Chinese
Japanese
More...
Wikia is hiring for several open positions
Send this article to a friend
"Easy indenting in insert and normal mode with no cursor displacement"
 
 
Hi!

I thought you'd like this page from Wikia!

http://vim.wikia.com

Come check it out!
Send confirmation


.