60

The default VIM indentation commands indent by shiftwidth spaces

e.g.

>>   Indent line by shiftwidth spaces
<<   De-indent line by shiftwidth spaces

Is there any way to indent with one or n (where n != shiftwidth) space(s)?

One way to do that is to vertically select a column in the block with Ctrl+V then, I to insert vertically and then type a space and <Esc>. But is there a better way?

2
  • 2
    Why do you want to avoid changing sw? That's what it's there fore.
    – Daenyth
    Feb 15, 2012 at 4:40
  • In visual line selection, {count}> will indent by {count}*'shiftwidth', whereas in normal mode, {count} is used to count lines that are indented. Perhaps this is what you want. Note that same for visual block selection, but spacing is inserted before first column of block instead of start of line.
    – Benoit
    Feb 15, 2012 at 6:23

8 Answers 8

50

I'm not sure that there is a better way. But, there are a few ways that you could do it (that I can think of anyway)...

Your Visual Block Solution

Like you said: press Ctl-V select the lines you want, press I to insert, and enter the number of spaces.

Search

Similar to the above but a little more flexible - you can use with with the 'select paragraph' vip command, or any range really: press v or vip or what have you to select the range, and the type :s/^/{n spaces} where {n spaces} is the number of spaces you want to insert.

Its a little more verbose, but works pretty well for pretty much any range. Heck, if you wanted to do the whole file you could do Ctl-A (OS dependent) and indent the whole file (or just skip the whole visual mode thing and just do it command mode...as in :1,$s/^/{n spaces}

Note that you don't have to include the third slash in s/// since you aren't putting any switches at the end of the search.

Global

Maybe you want to only indent lines that match some pattern. Say...all lines that contain foo. No problem: type :g/foo/s/^/{n spaces}

Global is especially handy if its multi-line sections with a similar pattern. You can just escape into normal mode land and select the lines you want and indent accordingly: :g/foo/norm Vjj:s/^/{n spaces}Ctl-V{Enter}. Little more complicated with that extra Ctl-V{Enter} at the end but useful under certain circumstances.

Use tabstop and shiftwidth

Yes, if your doing it a lot - I'd do :set ts=2 and :set et and :set sw=2 and use >> and << every which way...

Make a Function

Okay, so still not brief enough and for whatever reason you need to do this a lot and you can't abide messing with sw, et and ts settings. No problem, just write up a quick function and give it a localleader mapping:

function! AddSpace(num) range
  let s:counter = 0
  let s:spaces = ''
  while s:counter < a:num
    let s:spaces .= ' '
    let s:counter = s:counter + 1
  endwhile
    execute a:firstline .','. a:lastline .'s/^/'. s:spaces
endfunction

:map <LocalLeader>i :call AddSpace(3)Ctl-V{enter}

Maybe just knowing more than one way to do this is better than only knowing one? After all, sometimes the best solution depends on the problem :)

4
  • It appears when using the search method, blank space(s) will be inserted at the beginning of empty lines. Is there a clean and clever way of avoiding this? Also I found that I needed to visually select a block, then :, then '<,'>s/^/ / for one space. Thanks! Dec 11, 2013 at 17:11
  • +1 for visual block solution. It's perfect for quick modifications
    – verboze
    Jan 10, 2014 at 23:18
  • The visual block solution doesn't work for me, it inserts only to the single line, not to all of them. Aug 30, 2018 at 15:26
  • 2
    For those of you who also want to know what et, ts and sw means: expandtab, tabstop and shiftwidth
    – winklerrr
    Nov 8, 2018 at 15:58
32

Indent a block of code in vi by three spaces with Visual Block mode:

  1. Select the block of code you want to indent. Do this using Ctrl+V in normal mode and arrowing down to select text. While it is selected, enter ":" to give a command to the block of selected text.

  2. The following will appear in the command line: :'<,'>

  3. To set indent to 3 spaces, type le 3 and press enter. This is what appears: :'<,'>le 3

  4. The selected text is immediately indented to 3 spaces.

Indent a block of code in vi by three spaces with Visual Line mode:

  1. Open your file in VI.
  2. Put your cursor over some code
  3. Be in normal mode press the following keys:

    Vjjjj:le 3
    

Interpretation of what you did:

V means start selecting text.

jjjj arrows down 4 lines, highlighting 4 lines.

: tells vi you will enter an instruction for the highlighted text.

le 3 means indent highlighted text 3 lines.

4
  • This is awesome, except... when there is an empty line, it inserts blank spaces. Anyway to avoid that? Dec 11, 2013 at 16:51
  • 1
    This is the better answer. The chosen answer was too complicated, and in that answer Option 1 "Your Visual Block Solution" does not work on most systems.
    – Nike
    Dec 11, 2014 at 9:29
  • 9
    I disagree, because the solution with :le 3 indents from the left margin (column 1/0) but I think the OP wanted to (further) indent a block of text from the position it was at. For instance, for better vertical alignment of code/text that was autoindented just a few spaces off compared to a better position. Jan 13, 2015 at 8:49
  • 1
    I agree this is the best solution but it should probably be prepended by (0) navigate to the column where the lines should finally land, read and remember the column number (shown at the bottom right corner in my configuration), use it instead of 3 as the argument for the le command. Aug 30, 2018 at 15:36
19

To change the number of space characters inserted for indentation, use the shiftwidth option:

:set shiftwidth = <number>

Have a look here for more details.

You can also add that to your .vimrc file.

2
  • 1
    This saves ton of my time. Kudos!
    – fruqi
    Aug 18, 2015 at 5:27
  • 2
    1up. some of the suggested solution doesn't work as they claim without setting shiftwidth.
    – yantaq
    Oct 2, 2015 at 19:40
8

If I'm understanding correctly, you could use:

ctrl+V, jj then ':le n', where n is the number of spaces to indent.

http://vim.wikia.com/wiki/Shifting_blocks_visually

4

Place marks ('a' and 'b') between the code you want to indent:

<position cursor at top of block>
m a
<position cursor at bottom of block>
m b

Do a replace command such that each newline character between your marks is replaced with the number of spaces desired (in this example, 2 spaces):

:'a,'bs/^/  /g
1

If white space indentation already exists and you want to increase it further by one or more columns, then select a block of one or more white space columns using Ctrl-V, yank and paste it in the same place.

0

I had to dedent by a given number of spaces, amount, inside a vim script. This worked:

    let sw_setting = &shiftwidth
    set shiftwidth=1
    exe "normal v" . amount . "<"
    let &shiftwidth = sw_setting

A side-effect is that it resets the last visual mode selection. Instead, you may wish to edit the exe... line such that it executes "<<" repeated amount times in normal mode. That is: instead of normal v3<, make it normal <<<<<<, if amount is 3.

0

I like to use Space to indent a visual selection with a single space:

vnoremap <silent> <space> :s/^/ /<CR>:noh<CR>gv

And I couldn’t get Shift+Space to dedent, so I use z:

vnoremap <silent> z :s/^\s\=//<CR>:noh<CR>gv

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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