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?
|
|
Use the > command. To indent 5 lines, 5>>. To mark a block of lines and indent it, Vjj> to indent 3 lines (vim only). To indent a curly-braces block, put your cursor on one of the curly braces and use >%. If you’re copying blocks of text around and need to align the indent of a block in its new location, use ]p instead of just p. This aligns the pasted block with the surrounding text. Also, the |
|||||||||||||||||||||
|
|
This answer summarises the other answers and comments of this question, and adds extra information based on the Vim documentation and the Vim wiki. For conciseness, this answer doesn't distinguish between Vi and Vim-specific commands. In the commands below, "re-indent" means "indent lines according to your indentation settings." General Commands
You can replace Also, remember that
, so indentation commands can be easily and conveniently repeated. Re-indenting complete files Another common situation is requiring indentation to be fixed throughout a source file:
You can extend this idea to multiple files:
Or multiple buffers:
In Visual Mode
In insert mode These commands apply to the current line:
Ex commands These are useful when you want to indent a specific range of lines, without moving your cursor.
Indenting using markers Another approach is via markers:
...move cursor to end location
Variables that govern indentation You can set these in your .vimrc file.
Vim has intelligent indentation based on filetype. Try adding this to your .vimrc:
References |
|||||||||||||||||||||
|
|
A big selection would be
it is really fast, and everything gets indented ;-) |
|||||||||||||||||
|
|
Also try this for C-indenting indentation, do
That will auto-indent the current code block you're in. Or just:
to auto-indent the current line. |
|||||
|
|
Key-Presses for more visual people:
|
|||||||||||||||||||||
|
|
In addition to the answer already given and accepted, it is also possible to place a marker and then indent everything from the current cursor to the marker. Thus, enter |
|||||||||
|
|
The master of all commands is
This indents the entire file! And below are some of the simple and elegant commands used to indent lines quickly in Vim or gVim. To indent the current line
To indent the all the lines below the current line
To indent
For example, to indent 4 lines below the current line
To indent a block of code, go to one of the braces and use command
These are the simplest, yet powerful commands to indent multiple lines. |
|||||||||||||||||||||
|
|
As well as the offered solutions, I like to do things a paragraph at a time with |
|||||
|
|
when you select a block and use > to indent, it indents then goes back to normal mode. I have this in my .vimrc :
It lets you indent your selection as many time as you want. |
|||||||||
|
|
Go to the start of the text
|
|||||
|
|
The beauty of vim's UI is that it's consistent. Editing commands are made up of the command and a cursor move. The cursor moves are always the same:
So, in order to use vim you have to learn to move the cursor and remember a repertoire of commands like, for example, > to indent (and < to "outdent"). If, instead of typing >H, you type dH then you are deleting the same block of lines, cH for replacing it, etc. Some cursor movements fit better with specific commands. In particular, the % command is handy to indent a whole HTML or XML block. This is how vim works: one has to remember only the cursor movements and the commands, and how to mix them. |
|||||
|
|
do this
and add this line
this is only for cpp file you can do this for another file type also just by modifying the filetype... |
||||
|
|
|
Suppose you use 2 spaces to indent your code. Type:
Then:
You get the idea. (Empty lines will not get indented, which I think is kind of nice.) I found the answer in the (g)vim documentation for indenting blocks:
|
|||
|
|
|
A quick way to do this using This is useful if you would prefer not to change your
|
|||
|
|
|
I like to mark text for indentation:
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 % |
|||
|
|
|
|
||||
|
|
|
For me, the MacVim (Visual) solution was, select with mouse and press ">", but after putting the following lines in "~/.vimrc" since I like spaces instead of tabs:
Also it's useful to be able to call MacVim from the command-line (Terminal.app), so since I have the following helper directory "~/bin", where I place a script called "macvim":
And of course in "~/.bashrc":
Macports messes with "~/.profile" a lot, so the PATH environment variable can get quite long. |
|||
|
|
|
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
in you file where you want to indent properly in normal/command mode type
|
||||
|
|
|
|
||||
|
|
|
Using Python a lot, I find myself needing frequently needing to shift blocks by more than one indent. You can do this by using any of the block selection methods, and then just enter the number of indents you wish to jump right before the Eg. |
|||
|
|
|
There is one more way that hasn’t been mentioned yet - you can use
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:
Or something even more fancy:
More practical example is commenting Bash/Python/etc code with # character:
To re-indent use x instead of i. For example to remove first 5 characters from every line:
|
|||||
|
|
I didn't find a method I use in the comments, so I'll share it (I think vim only):
This is useful when you don't want to change ident/tab settings in vimrc or to remember them to change it while editing. To unindent I use the same ctrl-v block select to select spaces and delete it with d. |
||||
|
|
How to indent highlighted code in vi immediately by a # of spaces:Option 1: Indent a block of code in vi to three spaces with Visual Block mode:
Option 2: Indent a block of code in vi to three spaces with Visual Line mode:
Option 3: use Visual Block mode and special insert mode to increase indent:
|
||||
|
|
e.g.
Increase the '>' symbol for more tabs e.g.
|
|||
|
|
|
In
This works in |
|||||
|
|
To indent every line in a file type, esc then G=gg |
||||
|
|
|
To indent all file by 4:
|
|||||
|
protected by Samuel Liew Oct 5 '15 at 9:17
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?