I want to move a line by 4 bytes using vi editor. What command should I use?

I tried ">>" and it does move but it moves by 8 bytes..

  • Why are you making measurements in "bytes" in a text editor? Text is by the "character" or "glyph". – Ben Voigt May 5 '14 at 6:44
  • 1
    Why are you manually inserting space. Are you trying to format code. If so there are probably better ways. If you could explain what you are trying to achieve at a higher level that may be more useful. – Martin York May 5 '14 at 7:25

Default shiftwidth in vi is 8 spaces, from :help shiftwidth:

'shiftwidth' 'sw'       number  (default 8)
                        local to buffer
        Number of spaces to use for each step of (auto)indent.  Used for
        'cindent', >>, <<, etc.

So you should set shiftwidth to 4:

set shiftwidth=4

Add above line to .exrc file to make it persistence.


In normal mode, type 4I then press space and then escape.

This will insert 4 spaces at the beginning of the line and move the line to the right. I assume this is what you want, since you mentioned that >> almost works for you.

  • When you are in the middle of the line, this solution works with a slight modification: use 4I (capital-i) for inserting the spaces at the beginning of the line. – Walter A May 4 '14 at 21:04

When you are familiar with sed, you can also try sed-like commands. Suppose you just added an if statement and want to indent the lines until the next }. Then try

:.,/}/-1 s/^/    /

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.