35

Suppose I have the piece of text below with the cursor staying at the first A currently,

AAAA
BBB
CC
D

How can I add spaces in front of each line to make it like, and it would be great if the number of columns of spaces can be specified on-the-fly, e.g., two here.

  AAAA
  BBB
  CC
  D

I would imagine there is a way to do it quickly in visual mode, but any ideas?

Currently I'm copying the first column of text in visual mode twice, and replace the entire two column to spaces, which involves > 5 keystrokes, too cumbersome.

Constraint:

Sorry that I didn't state the question clearly and might create some confusions.

The target is only part of a larger file, so it would be great if the number of rows and columns starting from the first A can be specified.

Edit:

Thank both @DeepYellow and @Johnsyweb, apparently >} and >ap are all great tips that I was not aware of, and they both could be valid answers before I clarified on the specific requirement for the answer to my question, but in any case, @luser droog 's answer stands out as the only viable answer. Thank you everyone!

9 Answers 9

53

I'd use :%s/^/ /

You could also specify a range of lines :10,15s/^/ /

Or a relative range :.,+5s/^/ /

Or use regular expressions for the locations :/A/,/D/>.

For copying code to paste on SO, I usually use sed from the terminal sed 's/^/ /' filename


Shortcut

I just learned a new trick for this. You enter visual mode v, select the region (with regular movement commands), then hit : which gives you this:

:'<,'>

ready for you to type just the command part of the above commands, the marks '< and '> being automatically set to the bounds of the visual selection.

To select and indent the current paragraph:

vip>

or

vip:>

followed by enter.

Edit:

As requested in the comments, you can also add spaces to the middle of a line using a regex quantifier \{n} on the any meta-character ..

:%s/^.\{14}/& /

This adds a space 14 chars from the left on each line. Of course % could be replaced by any of the above options for specifying the range of an ex command.

3
  • And to add blankspace after 14 characters in all lines of file?
    – HenioJR
    Jun 6, 2017 at 18:34
  • This should do it: :%s/^.\{14}/& / Jun 7, 2017 at 4:43
  • Er, why vip>? Just >ip Jul 10, 2019 at 23:00
39

When on the first A, I'd go in block visual mode ctrl-v, select the lines you want to modify, press I (insert mode with capital i), and apply any changes I want for the first line. Leaving visual mode esc will apply all changes on the first line to all lines.

Probably not the most efficient on number of key-strokes, but gives you all the freedom you want before leaving visual mode. I don't like it when I have to specify by hand the line and column range in a regex command.

4
  • 3
    Oh! This is actually very great trick! Whatever change for the first line will end up being applied to the rest of the selected block! Thanks for sharing!
    – nye17
    Sep 29, 2011 at 6:36
  • That's what's fun about VIM, there's always something to discover. This was new to me. Sep 29, 2011 at 18:46
  • This actully does ident from the most left of the file,not the original position if it was already indented May 18, 2018 at 6:22
  • This is the best and shortest solution Nov 21, 2021 at 22:24
18

I'd use >}.

Where...

  • >: Shifts right and
  • }: means until the end of the paragraph

Hope this helps.

5
  • you mean in visual mode right? I only use this when the number of spaces is the same as what I specified in vimrc for the indentation....
    – nye17
    Sep 29, 2011 at 4:08
  • 1
    This is in normal mode. You didn't specify the shift-width (or at least you hadn't when I answered).
    – johnsyweb
    Sep 29, 2011 at 4:11
  • 1
    @nye17: you don't set shiftwidth=4 softtabstop=4 expandtab ? Sep 29, 2011 at 4:12
  • @luserdroog yep, I did have them set.
    – nye17
    Sep 29, 2011 at 4:18
  • @nye17: cool. just checking. I didn't learn about expandtab until very recently. Sep 29, 2011 at 4:33
16
  1. Ctrl + v (to enter in visual mode)
  2. Use the arrow keys to select the lines
  3. Shift + i (takes you to insert mode)
  4. Hit space keys or whatever you want to type in front of the selected lines.
  5. Save the changes (Use :w) and now you will see the changes in all the selected lines.
7

I would do like Nigu. Another solution is to use :normal:

  1. <S-v> to enter VISUAL-LINE mode
  2. 3j or jjj or /D<CR> to select the lines
  3. :norm I<Space><Space>, the correct range ('<,'>) being inserted automatically

:normal is probably a bit overkill for this specific case but sometimes you may want to perform a bunch of complex operations on a range of lines.

2
  • Wow, you already told us about this! I +1-ed it, but I must not have read it very closely. :normal is my new friend. Mar 22, 2013 at 0:41
  • :normal will be your friend forever.
    – romainl
    Mar 22, 2013 at 5:52
4

You can select the lines in visual mode, and type >. This assumes that you've set your tabs up to insert spaces, e.g.:

setl expandtab
setl shiftwidth=4
setl tabstop=4

(replace 4 with your preference in indentation)

If the lines form a paragraph, >ap in normal mode will shift the whole paragraph above and below the current position.

3

Let's assume you want to shift a block of code:

  • setup the count of spaces used by each shift command, :set shiftwidth=1, default is 8.
  • press Ctrl+v in appropriate place and move cursor up k or down j to select some area.
  • press > to shift the block and . to repeat the action until desired position (if cursor is missed, turn back with h or b).

move block of code

2

Another thing you could try is a macro. If you do not know already, you start a macro with q and select the register to save the macro... so to save your macro in register a you would type qa in normal mode.

At the bottom there should be something that says recording. Now just do your movement as you would like.

So in this case you wanted 2 spaces in front of every line, so with your cursor already at the beginning of the first line, go into insert mode, and hit space twice. Now hit escape to go to normal mode, then down to the next line, then to the beginning of that line, and press q. This ends and saves the macro

(so that it is all in one place, this is the full list of key combinations you would do, where <esc> is when you press the escape key, and <space> is where you hit the space bar: qai<space><space><esc>j0q This saves the macro in register a )

Now to play the macro back you do @ followed by the register you saved it in... so in this example @a. Now the second line will also have 2 spaces in front of them.

Macros can also run multiple times, so if I did 3@a the macro would run 3 times, and you would be done with this.

I like using macros for this like this because it is more intuitive to me, because I can do exactly what I want it to do, and just replay it multiple times.

0

I was looking for similar solution, and use this variation

VG:norm[N]I

N = numbers of spaces to insert. 
V=Crtl-V 
*** Notice ***  put space immediate after I.

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.