URL:         http://www.math.fu-berlin.de/~guckes/vim/howto/indent.html
URL:         https://www.vim8.org/indent.html (mirror)
Created:     Fri Jun 12 12:12:12 CEST 1998
Last update: Fri Jun 12 12:45:50 CEST 1998

Vim HowTo - Formatting Text


Shifting

Shifting text means to indent or de-indent text. The shift itself is usually done with a TAB, but Vim can also use whitespace to (de)indent.

The command for shifting are described in the helpfile "change.txt" under "Simple changes". However simple, you might ant to take a look at the following examples:

Indenting the current during insert mode:
There are two special commands for insert mode which do indenting:
	^D	de-indent current line
	^T	   indent current line
Note:  The commands ^D and ^T are standard Vi commands!
These are quite useful while typing code.  Try them!
I use ^T to indent the current line if I have forgotton to do so
when I started typing the paragraph, eg when I type an example
which I usually put into my posts to comp.editors.
I also use "autoindent" to make it easier to type in that indented paragraph
which I can quickly set with "^O:se ai^M".
Indenting text from command mode:
Vi knows the commands '>' and '<' for shifting (deshifting) text.
Unfortunately, Vi does not have the concept of "text objects" as Vim does.
So indenting the current paragraph requires to jump to one end of the
paragraph and then apply the shift command by jumping to the other end:
	command: {>}
	description:
	{	jump onto empty line before current paragraph
	>	start shifting to right
	}	jump onto empty line after current paragraph
		thereby moving over the paragraph ans thus
		selecting it for shifting
But this means jumping around, moving the cursor off the current line.
You must then jump back to the previous current line within the paragraph
yourself.  If that paragraph was long you'll quite some jumps.  Bad.
Of course you could have marked that line prior to the shifting ..
	command: ma
	description:
	m	mark current line with following letter
	a	set the mark 'a' for the current line
... and return to the line using the command "'a".
But for this you must have remembered to do this *before*
jumping off that line.  I usually forget to do that. :-/
So I am very glad that Vim takes care of my jumps
and that Vim allows me to "jump back" to previous lines
using the command ^O (in command mode).
When I jump back to far then I can use ^I to "jump forward" again.
Back to the subject again:  Indenting.
Vim makes it easy to apply these commands to text objects,
eg the current paragraph:
	>ap	   shift current paragraph
	<ap	de-shift current paragraph
A number prefix N to these commands will apply the shift to N paragraphs:
	3>>ap	read: "three shift-right all paragraph"
			effect: shifts the current and the two
			following paragraph to the right
Note:  Using a number prefix for the text object "ip" (inner paragraph)
will just shift the current paragraph only, as you simply apply the command
to the *inner* part.  (I wish Vim would shift N times in this case...)
==== TODO:
As usual, there are two ways to make a comamnd work on some text:
- Specification of text prior to command.
- Specification of text after a  command.
"before": Use "visual selection", eg type "vip" to select the current
*inner* paragraph.  Then press '>' to indent the selected text.
"after":  Type '>' to start indenting text,
then type "ip" to specify the inner paragraph.
> I guess by a tabstop.. or by spaces, but I think
> there's already a way to tell it to use spaces.
Use ":set expandtab" to have TABs expanded to spaces.
Unfortunately, this option is *not* a VI standard.  :-(
> If I do a visual selection, I want it to shift all of the lines with
> any selected characters..  (So I want the "selection" to be by lines
> and not by chars..)
The "visual operator" '>' shift all affected lines.
Sounds like you are using 'v' for visual selction only -
there's also 'V' to select a range of full lines and
'^V' to select a block (rectangle) of text visually.
> I would think there's got to be something like this,
> but I can't find anything relevant in the online docs.
	:help expandtab
	:help v_>

Send feedback on this page to
Sven Guckes [email protected]