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

17,828

Solution 1

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<)

Solution 2

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.

Solution 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.

Share:
17,828
drapkin11
Author by

drapkin11

Updated on September 17, 2022

Comments

  • drapkin11
    drapkin11 over 1 year

    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

    • Admin
      Admin about 13 years
      As 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.
  • drapkin11
    drapkin11 about 13 years
    Yes, 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
    drapkin11 about 13 years
    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.
  • FJ de Brienne
    FJ de Brienne about 13 years
    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 ;)
  • garyjohn
    garyjohn about 13 years
    @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.
  • Heptite
    Heptite about 13 years
    It looks like you have mswin.vim sourced, which makes some things in Vim behave more like "standard" Windows editing dialogs/notepad.
  • drapkin11
    drapkin11 about 13 years
    @garyjohn, your suggestion works very well
  • drapkin11
    drapkin11 about 13 years
    @johnny, you are correct, the issue goes away. @garyjohn suggested the same solution which works for me.