Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question
    
Why are you making measurements in "bytes" in a text editor? Text is by the "character" or "glyph". –  Ben Voigt May 5 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. –  Loki Astari May 5 at 7:25

3 Answers 3

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.

share|improve this answer

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.

share|improve this answer
    
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 at 21:04
    
Thanks Walter A, updated my answer. –  Alexander May 4 at 21:05

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/^/    /
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.