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

前端 未结 8 1542
渐次进展
渐次进展 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:46

    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 讨论(0)
  • 2020-12-23 17:54
    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.
    0 讨论(0)
提交回复
热议问题