vim - remapping >> << (indent commands)
Asked Answered
P

3

11

I would love to map << and >> to single keys to speed up my workflow, but I can't find any info on how to write the remap in my vimrc. Any idea how I can get my remap on?

Preciado answered 6/12, 2011 at 2:22 Comment(6)
Do you really want to remap them? After the first indentation via >>, you can press . to repeat the action. Just about as good as remapping to a single keystroke.Swifter
In visual mode, a single < or > performs the indentation already.Swifter
note that in insert mode CTRL-T and CTRL-D indent and deindent respectively.Pericranium
@Michael, I know, I'm totally lazy. There's just something exhausting about hitting the same key twice all day to initiate indents (even if I repeat them with . After). I figure the answers below will still save me hundreds of keystrokes a day. (Yes, I deal with that much markup). Thanks for the tips everyone!Preciado
@Preciado if you're dealing with a lot of HTML or XML and you find yourself constantly indenting stuff, hopefully you're using = to fix indentation as well...Swifter
yeah I do, it's more where I run into something like a two row set of brackets I want to organize and it's the same amount of work to highlight them and == as it would be to >> the two rows (like if it only needed one indentation, == would be the same as >>). You're totally right about lots of html auto indenting with == though. It just always seemed strange that it had to be >> instead of > in command mode when it's a single in every other mode.Preciado
P
11

You're probably looking for :noremap:

:noremap > >>
:noremap < <<

If you just use :map, you of course get recursively defined function, which is hilarious. (^C will stop it.) The :noremap variant doesn't recursively expand mappings.

Pampa answered 6/12, 2011 at 2:27 Comment(0)
C
13

The other responses given answer your immediate question. I would just like to suggest that you remap that to indent and dedent to and respectively, in both normal and visual modes (making sure to add gv to reselect in visual selection mode):

nnoremap <TAB> >>
nnoremap <S-TAB> <<
vnoremap <TAB> >gv
vnoremap <S-TAB> <gv

In insert mode of course, as you probably might know already, you can just use Ctrl-t and Ctrl-d to indent/dedent.

Cassycast answered 6/12, 2011 at 2:59 Comment(1)
I can't exit from visual mode with escape when I use this keybindings. Is there any solution to that?Banta
P
11

You're probably looking for :noremap:

:noremap > >>
:noremap < <<

If you just use :map, you of course get recursively defined function, which is hilarious. (^C will stop it.) The :noremap variant doesn't recursively expand mappings.

Pampa answered 6/12, 2011 at 2:27 Comment(0)
G
1

I find these to be the most natural indentation keymaps for both normal and visual modes

" Normal mode  
nnoremap > >>
nnoremap < <<

" Visual mode
vnoremap < <gv
vnoremap > >gv
Goldfinch answered 5/6, 2022 at 15:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.