Indent multiple lines quickly in vi

2019-01-04 04:23发布

Should be trivial, and it might even be in the help, but I can't figure out how to navigate it. How do I indent multiple lines quickly in vi?

30条回答
贼婆χ
2楼-- · 2019-01-04 04:31

As well as the offered solutions, I like to do things a paragraph at a time with >}

查看更多
该账号已被封号
3楼-- · 2019-01-04 04:31

do this

$vi .vimrc

and add this line

autocmd FileType cpp setlocal expandtab shiftwidth=4 softtabstop=4 cindent

this is only for cpp file you can do this for another file type also just by modifying the filetype...

查看更多
Rolldiameter
4楼-- · 2019-01-04 04:32

There is one more way that hasn’t been mentioned yet - you can use norm i command to insert given text at the beginning of the line. To insert 10 spaces before lines 2-10:

:2,10norm 10i 

Remember that there has to be space character at the end of the command - this will be the character we want to have inserted. We can also indent line with any other text, for example to indent every line in file with 5 underscore characters:

:%norm 5i_

Or something even more fancy:

:%norm 2i[ ]

More practical example is commenting Bash/Python/etc code with # character:

:1,20norm i#

To re-indent use x instead of i. For example to remove first 5 characters from every line:

:%norm 5x
查看更多
三岁会撩人
5楼-- · 2019-01-04 04:32

I dont know why its so difficult to find a simple answer like this one...

I myself had to struggle a lot to know this its its very simple

edit your .vimrc file under home directory add this line

set cindent

in you file where you want to indent properly

in normal/command mode type

10==   (this will indent 10 lines from the current cursor location )
gg=G   (complete file will be properly indented)
查看更多
该账号已被封号
6楼-- · 2019-01-04 04:34

I like to mark text for indentation:

  1. go to beginning of line of text then type ma (a is the label from the 'm'ark: it could be any letter)
  2. go to end line of text and type mz (again z could be any letter)
  3. :'a,'z> or :'a,'z< will indent or outdent (is this a word?)
  4. Voila! the text is moved (empty lines remain empty with no spaces)

PS: you can use :'a,'z technique to mark a range for any operation (d,y,s///, etc) where you might use lines, numbers, or %

查看更多
Fickle 薄情
7楼-- · 2019-01-04 04:35
  • For block of code {}: = + %

  • For selected line: Shift + v select using up/down arrow key then press =.

  • For entire file: gg + = + G

Note: 'gg' means go to line 1, '=' is the indent command, and 'G' moves the cursor to the end of file.

查看更多
登录 后发表回答