How can I indent multiple lines, and indent "backwards", in gvim?

5

4

I'm just learning vim (via gvim, I used to be a Notepad++ user) and haven't yet found how to do 2 things:

  1. How can I indent a set of rows x spaces/tabs right? In Notepad++, for example, I just highlight the rows I need and press 'Tab' key.
  2. Also, is there a way to move backwards equivalent to "Shift+Tab" in Notepad++?

Thanks

drapkin11

Posted 2011-03-01T22:48:11.993

Reputation: 646

1As Luc Hermitte suggested, vimers use the 'indent' or 'indentation' keyword in this context. By tabs we mean something slightly different - a layout feature. For more help, see :help tabpage in vim. – vtest – 2011-03-02T11:05:10.333

Answers

7

Start at the first line you want to indent, then press > and type the number of lines you want to indent and press > again (for 10 lines you'd press >10>) To un-indent you'd just use < instead of > (<10<)

Majenko

Posted 2011-03-01T22:48:11.993

Reputation: 29 007

1Yes, that worked. Any way of doing that without first knowing how many lines I'd want to indent? Is there a way to select all the lines I want using the cursor and then issuing the command? – drapkin11 – 2011-03-01T23:28:05.643

I don't know about selecting lines, but you can use >> to indent just the current line, then use the 'repeat last command' key (.) on all subsequent lines ([down][.][down][.][down][.]) After a while you get quite fast at this ;) – Majenko – 2011-03-01T23:47:09.387

5@drapkin11: One way to select the lines you want to shift is to move the cursor to the first line, type V, then move the cursor to the last line. All the selected lines should be highlighted. Now type < to shift the selected lines left or > to shift right. – garyjohn – 2011-03-02T00:15:14.823

@garyjohn, your suggestion works very well – drapkin11 – 2011-03-02T23:20:33.293

3

What you want are the > and < commands, see ":help shift-left-right".

You can use these commands in multiple ways, but since you specifically mentioned highlighting, you can just use your mouse or keyboard to highlight the lines you want to shift/unshift and press > or <.

Instead of using visual mode (highlight) you can provide a count and >> or <<. For example, 3>> will indent the current line and two lines below it.

Heptite

Posted 2011-03-01T22:48:11.993

Reputation: 16 267

Right, I did as you suggested in your 2nd statement, but for some reason when I press '<', my selected rows are just overwritten with the '<' character! Certainly not what I intend. – drapkin11 – 2011-03-01T23:33:26.993

It looks like you have mswin.vim sourced, which makes some things in Vim behave more like "standard" Windows editing dialogs/notepad. – Heptite – 2011-03-02T01:25:41.187

1@drapkin11, I believe @Heptite is correct about mswin. If you use V when highlighting rows you will not have this problem. – johnny – 2011-03-02T06:51:22.930

@johnny, you are correct, the issue goes away. @garyjohn suggested the same solution which works for me. – drapkin11 – 2011-03-02T23:23:49.343

3

The preferred approach is to let vim perform auto-indentation. Don't forget this in your .vimrc:

set ai
filetype indent on

Then, if you open a file badly indented, you can then use the = command (in conjunction with a motion, e.g. gg=G to reindent the whole file, == to reindent the current line, =i{ to reindent the current {} block, etc.).

>> and << exist indeed, since the old and plain vi, but they are really cumbersome for real and long term editing.

Luc Hermitte

Posted 2011-03-01T22:48:11.993

Reputation: 1 575