sponsor Vim development Vim logo Vim Book Ad

basic Tip #83: how to indent (useful for source code)

 tip karma   Rating 668/241, Viewed by 16857 

created:   June 22, 2001 14:51      complexity:   basic
author:   Eugene Huang      as of Vim:   5.7

Here is the most useful vim command that I know of and I'm surprised that it's not yet in the tips list.

I use the indent features of vim all the time. Basically, it lets you indent your source code.

SETUP: To make indentation work nicely I have the following in my .vimrc file:
set et
set sw=4
set smarttab

these make vim behave nicely when indenting, giving 4 spaces (not tabs) for each "tabstop".

HOW TO USE:
in command mode, == will indent the current line
selecting a range of lines (with shift-v) then == will indent your selection
typing a number then == will indent that many lines, starting from your cursor
(you get the idea, there are many other things you can do to select a range of lines)

Tell me that isn't great?

 rate this tip  Life Changing Helpful Unfulfilling 

<<letting variable values be overwritten in a script | Changing the behaviour of . to include visual mode >>

Additional Notes

Debian@austin dot rr dot com, June 26, 2001 16:51
The "=" command operates on lines ranges in the same way as "d", "y", ">".  This duplicates the functionality of typing your code in the first place after using the ":set autoindent" command.  HOWEVER, it is far preferrable to use ":set noexpandtab tabstop=4 shiftwidth=4" rather than ":set expandtab" since it gives others editing the same code the options of setting the indentation level to 2, 3, 5, 8, ... per personal taste rather than forcing everyone to live with your particular choice.  If you prefer your cursor motion to behave as if tabs were actually spaces you can use the virtual edit mode instead.  Fortunately the "=" command will allow you to reformat code to include proper tab formatting if someone has made the mistake of using ":set expandtab".
[email protected], June 28, 2001 0:39
Not to get into a holy way about tabs vs spaces to indent, but I think it's a bit blunt to say 'if someone made the mistake to set :expandtab'. Please read http://www.jwz.org/doc/tabs-vs-spaces.html for a whole discussion on why spaces are better.
[email protected], July 14, 2001 18:07
Well...

Go tabs!

http://derkarl.org/why_to_tabs.html !!!


rah! :))
[email protected], July 24, 2001 6:14
How do you unindent?
--se
eugenehuang @ canada.com, July 24, 2001 18:33
To unindent you can do this:

Select (in any way you choose) the lines you want to unindent, and then
type <<. You can do this repeatedly until everything is unindented.

(Also >> indents manually one tab stop)

Hope that helps.

Eug
[email protected], August 8, 2001 8:31
To indent all your code, you can type 'gg=G'; 'gg' to go to the top of the file and '=G' to indent from the current line to the last. 'G=gg' also works...
[email protected], September 25, 2001 1:22
to indent the whole file without having the cursor move to the beginning or the end I have the following
function and mapping:

fun BenIndent()
let oldLine=line('.')
normal(gg=G)
execute ':' . oldLine
endfun
map -- :call BenIndent()<cr>

just hit -- (conveniently close to ==) in normal mode to indent the whole file
[email protected], December 10, 2001 14:24
Vim 6.0 has a new "flexible indenting" feature so that indenting works in many different programming languages and filetypes.  To turn it on automatically for built-in filetypes, just add:

filetype indent on

to your vimrc.  See ":help new-indent-flex" and ":help indentexpr" for more details.
[email protected], May 14, 2002 18:18
You can use pretty much any selection method for indent "="
1. To indent within braces  do " =%"

2. To indent a block
select the beginning of the block by placing a bookmark   e.g.   " ma" where a is the name of the buffer (any character)
scroll to the end of the block and do " ='a "

3. multiple lines
    "n==" where n is the number of lines
[email protected], May 16, 2002 19:54
It is possible to add a line to a source file that says how Emacs should display a file such as:
/* -*- Mode: C; tab-width:8 -*- */

Is this possible in vi/vim (and if so, then how?)?
[email protected], May 20, 2002 0:39
It seems possible by adding this near the top:
ex: set (options):

So to set tabstops to 8 spaces in a C source file, you would do something like this:
/* ex: set ts=8: */

On my system, this causes vim to automatically use this option as if it were specified
in my .vimrc, but you may also need to do ":set modeline" for your editor to look for
these options in the source file.
ser at germane dash software dot com, September 8, 2002 18:18
All of these diverse ways of doing the same thing.  That is very cool.

Another way of cleaning the indentation of the entire file:

:%=
[email protected], June 20, 2003 21:25
I have to put in a plug for a script someone submitted that helps with the whole tabs/spaces indenting thing.

https://www.vim8.org/scripts/script.php?script_id=513

Upon loading a buffer, a python script looks through the file, determines what indent style was used, then adjusts your settings so that new modifications use that same style.

Pretty cool!
[email protected], October 10, 2003 13:49
This isn't great.  This is fantastic!  I've been trying to get this done for a while now to better indent some older code that has a mix of tabs and spaces and is inconsistent in the indentlevel.
[email protected], March 24, 2004 4:15
I have found in ":help tabstop" four main ways of indent my code.
I have this on mi ~/.vimrc and works perfect for me:

set noexpandtab
set softtabstop=4
set tabstop=8
set shiftwidth=4
set foldmethod=indent

That is:
- Don't replace TAB character with only spaces
- Use a mix of tabs and spaces when I press the TAB key (width 4)
- Default setting of tabstop as recommended in help (width 8) -- printing, etc
- Number of spaces to use for each step of indent (width 4)
- Lines with the same indent level form a fold

See help pages for each setting and :help ins-expandtab for more help.
Regards (and excuse my English :)
[email protected], March 28, 2004 15:43
Thank you, Thank you, Thank You
[email protected], May 6, 2004 10:06
How do you indent multiple files (without indenting them individually)

My scenario is that I have dozens of c files, which I want to open in one shot and indent them at once. Is it possible?

gvim `find . -name "*.c"`
indent one file by saying = and than shift-g. But how to do I execute it on multiple files?
[email protected], May 8, 2004 12:39
http://www.gnu.org/software/indent/indent.html

Vim is an editor not a batch job processor. Go use emacs for that.
Anonymous, May 26, 2004 4:55
One way to do this is to use the argument-list:
:args *.c
:argdo exe "normal 1G=G" | update

BTW, the "="-operator does not take a range, so using
:%=
as mentioned in a previous post does not work.
tom, June 9, 2004 8:40
I've been looking for a way to easily (but manually) indent blocks of code, for if I paste a control statement into a buffer, etc.  With guidance from comments above, this is what I've come up with- I put these lines in my .vimrc file:

"indent/ unindent
"Do this by hitting the mapped key, then clicking the bottom of the block
"that you want indented.
map <F4> >
map! <F4> <ESC><F4>
map <F5> <
map! <F5> <ESC><F5>

Put the cursor on the first line you want indented.  Hit F4,  then click the end of the code you want to indent and it will indent the whole block by one tab.  Hitting F5 will un-indent one tab the same way.  Super easy.  
[email protected], July 23, 2004 9:13
For "normal" tab and shift-tab behavior:

map <Tab> >0
map <S-Tab> <0
imap <S-Tab> <Esc> < i
[email protected], July 30, 2004 21:24
i frequently use `==" to indent my matlab code. seems to work okay, but let's say I remove a for-end construct, visually select the lines in between and hit `=", the block moves to the left, as it should, but if there's an `end", it doesn't budge. going to the line with the `end" and hitting `==" doesn't work either. The matching `for" indents correctly though. help appreciated!

[email protected], September 8, 2004 2:06
Using = or == on visual highlited lines, it will indent them, but leaves comments (lines starting with #) at the beginning of the line.
How to indent comments lines too ?
[email protected], September 8, 2004 11:10
Tabs blow!  Shame on ANY of you for thinking that they are preferable to spaces....

[email protected], September 12, 2004 3:10
I don't get the difference between ">" or "<" and "==" ("=" works for me too)? What is the difference?
The first method (">") works better for me, so i don't know what advantages "==" has.
Dave (email not given), October 4, 2004 18:16
Hi,

While you're here reading about tabs and spaces I thought you might want to hear about :insert.  For example, if you normally paste into a terminal window some code from some other gui window, vim will intent the paste as if you typed it, which turns out bad on already indented code.  So, you may want to re-indent the file using one of the methods above.  But, if you remember to do the following before you paste you may not need to re-indent: do <Esc>:insert<cr> and then paste (however your gui pastes into the terminal) and then press <Esc> more to say you're done pasting.

Sorry if someone feels that's off topic, but I think it's still in the realm of indentation issues.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge Logo