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

前端 未结 8 1541
渐次进展
渐次进展 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条回答
  • 2020-12-23 17:28

    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.

    0 讨论(0)
  • 2020-12-23 17:33

    I'd use >}.

    Where...

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

    Hope this helps.

    0 讨论(0)
  • 2020-12-23 17:33

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

    0 讨论(0)
  • 2020-12-23 17:35

    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.

    0 讨论(0)
  • 2020-12-23 17: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.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题