20

I want to be able to highlight lines I want to indent/reverse-indent and indent with tab and shift-tab respectively.

" for command mode reverse tab
nmap <S-Tab> <<
" for insert mode reverse tab
imap <S-Tab> <Esc><<i
" for command mode multiple line reverse tab(doesn't work)
nmap <Tab> i<
" for insert mode multiple line reverse tab(doesn't work)
imap <Tab> <

" for command mode tab
nmap <Tab> >>
" for command mode multiple line tab(doesn't work)
nmap <Tab> i>
" for insert mode multiple line tab(doesn't work)
imap <Tab> >

This is the last thing I need before I'm willing to use vim as my primary editor.

1
  • 1
    So? What exactly is the problem? Sep 17, 2011 at 3:52

3 Answers 3

40

Quick start

In visual mode,

  • < will shift selected lines leftwards and
  • > rightwards.

How to mark text in visual mode

To select and highlight your text, you need to start using visual mode, (I usually do this by hitting v, or if coming from insert mode: Escv) and select your text using the standard motions (such as h, j, k and l) .

If you like, map other keys (not recommended)

If you do not want to use the default shortcuts < and >, create mappings for use in visual mode. You need :vmap:

:vmap <Tab> >
:vmap <S-Tab> <

which would mean pressing

  • Tab for "increase indent" and
  • ShiftTab for "decrease indent"

But why do you need to create a mapping for this?

Sometimes it's better just to learn the Vim (or even Vi) keys (and < and > are easy anyway), and then you can use any installation, not just the one with your .vimrc.

For instance, you've put:

imap <Tab> <

This is a bad idea. This will make it harder for you to insert a tab-character into your text (even ctrl+i won't work as I'd expect), and every time you type tab you'll insert a <.

Happy editing!

6
  • If I use my mouse to highlight, I use my right hand. < and > are both on the right side of the keyboard. Also it's out of habit since every editor I've used did this.
    – deadghost
    Sep 17, 2011 at 4:01
  • Your recommendation is quite good. I'll ditch the old highlight with mouse and tab paradigm. Do you still use the tab key for single line indenting or have you switched off to > for that too?
    – deadghost
    Sep 17, 2011 at 4:11
  • 2
    @user894091: Happy to help. I use the standard keybindings for the vast majority of editing actions.
    – johnsyweb
    Sep 17, 2011 at 4:13
  • 2
    @Tapefreak: Type the number before you hit >. E.g. 3> to indent three 'shiftwidth's
    – johnsyweb
    Jan 29, 2012 at 21:48
  • 3
    @Tapefreak: Or hit '.' to repeat the indentation, See :he single-repeat.
    – johnsyweb
    Nov 14, 2012 at 0:32
5

In insert mode you can use control-d and control-t to remove/add an indent (respectively).

So, replace

imap <Tab> <

with

imap <Tab> <c-d>

and

imap <Tab> >

with

imap <Tab> <c-t>

In visual mode you can highlight a block and then hit < or >, but if you'd like it to be tab and shift-tab, you can set it with vmap like you have done for the others.

2
  • 3
    That's still a bad idea. How do you insert a tab character?
    – johnsyweb
    Sep 17, 2011 at 4:11
  • 1
    @Johnsyweb ① I want to insert a tab character at least two orders of magnitude less frequently that I want to use it as a command. ② When you do what it (or any other character you've bound to something) you can use <ctrl>+v<tab> to insert a literal tab (or whatever else).
    – Caleb
    Mar 14, 2015 at 7:59
1

put this in your .vimrc it's what you want

:map <Tab> >
:imap <Tab> <Esc> >
:vmap <Tab> >gv
:map <S-Tab> <
:imap <S-Tab> <Esc> <
:vmap <S-Tab> <gv

the gv after the vmaps will keep your highlights. the map and imaps will allow you insert in other modes. It's not quite like textmate, but it's getting there :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.