How to insert a block of white spaces starting at the cursor position in vi?

前端 未结 8 1584
渐次进展
渐次进展 2020-12-23 16:53

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

8条回答
  •  -上瘾入骨i
    2020-12-23 17:42

    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.

提交回复
热议问题