41

I am using the Vim editor. Here is my situation:

1111111111111
2222222222222
3333333333333
4444444444444

Above is the original code, I want to make them like below. What should I do to shift them all to the right?

    1111111111111
    2222222222222
    3333333333333
    4444444444444
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
Yongwei Xing
  • 12,983
  • 24
  • 70
  • 90

9 Answers9

57

In command mode, you can use >> to indent a single line. 4>> will indent the current and next three lines.

If you don't know how many lines in advance (it may be quite large), you can use ranges. Go to the first line of the range and enter ma to place marker A. Then go to the last line and enter >'a to indent from here to marker A. You can do all sorts of wonderful things with ranges.

How they're indented depends on a couple of things like your shiftwidth settings. I always have my shiftwidth and tabstop settings the same to avoid problems:

:set ts=4 sw=4

(for example).

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 7
    Nitpick: You use `>>` in *normal* mode; command mode is when you have typed `:` and are typing a command. – too much php Aug 02 '10 at 11:43
  • 3
    Actually, I'm going to dissent on that one. Vi has _always_ referred to command and insert modes, wikipedia errors notwithstanding :-). The colon commands are simply `ex` commands allowed in command mode by prefixing them with `:`. – paxdiablo Aug 02 '10 at 12:05
  • Vim does make a distinction between normal and command *line* mode, and it's important to remember that they are different, for purpose of mappings, etc. To avoid confusion, vim help will always refer to "normal mode". However `:help command-mode` will tell you about normal mode, and "this is also known as command mode". – nicholas a. evans Aug 02 '10 at 18:16
30

If you've already selected the four lines in visual mode: > will shift them shiftwidth to the right. After they are shifted, the visual selection will be gone, but you can indent again via . (repeat last command).

If you are normal mode, with your cursor anywhere on the first line:

  • >> will indent that line,
  • 4>> will indent all four lines,
  • >3j will do the same thing in a different way (indent from this line to three lines down),
  • >} will indent all of the lines until the end of the paragraph (i.e. to the first empty line, see :help object-motions), and
  • >ap will indent all of the lines for a p-aragraph (see :help text-objects), even if your cursor isn't on the first line.

Again, you can repeat these commands via . for deeper indentation levels (or you can set shiftwidth appropriately).

If your file is nicely composed of "paragraphs" (and most of my code and prose is), I think you'll find the ap text-object to be the most common way to work on blocks of text like this. You can also use text-objects to speed up visual selection.

nicholas a. evans
  • 2,163
  • 1
  • 17
  • 17
10

Hit >

That's all.

Alok Singhal
  • 93,253
  • 21
  • 125
  • 158
6

Use v to select the block and then press > key.

Rumple Stiltskin
  • 9,597
  • 1
  • 20
  • 25
3

Use the > key.

Michael Ulm
  • 802
  • 1
  • 7
  • 11
1
  1. Press Ctlt+v to select multiple lines.
  2. Type :
  3. Type sed substitute pattern matching s/^/ /g (e.g: for adding 4 spaces)
  4. Press enter

vim indent multiple lines

Vim follows sed to remove/add extra spaces from the start of lines like this:

sed -i 's/^/    /g'  test.txt
sed -i 's/^    //g'  test.txt
Afshin
  • 21
  • 1
  • 3
1
  1. Ctl+v # for the Visual Block mode
  2. Move with arrows up or down to select the block
  3. Once block is selected you can do >
  4. OR while block is selected click on 'I'. Cursor will move to the first selected line in Insert mode.
  5. Start moving it by adding spaces. Dont worry that only first line moves.
  6. Once first line is aligned as you expected hit Esc and then arrow down
Zaur
  • 431
  • 4
  • 5
0

Not mentioned here but very useful:

The :> and :< commands take a range, and additional > or < can be used. For example, :12,20>>> indents lines 12 to 20 inclusive three times

Source https://vim.fandom.com/wiki/Shifting_blocks_visually

-1

For me the number need to be after, like >>4, to move to right, or before, like 4<<, to move to left. I use Vim 7.4.52.

trad
  • 1
  • 1