Vim Tips Wiki

Indenting source code

Revision as of 21:50, March 11, 2013 by (Talk)

1,619pages on
this wiki
Tip 83 Printable Monobook Previous Next

created 2001 · complexity basic · author Eugene Huang · version 6.0

The indent features of Vim are very helpful for indenting source code. This tip discusses settings that affect indentation. These settings mostly affect the automatic indentation which Vim inserts as you type, but also can be triggered manually with the = operator, so that you can easily Fix indentation in your buffer.

For related information, see:




?> <html> <head> <link rel="stylesheet" href="<?php echo BASE_URL_PUBLIC.'js/bootstrap/css/bootstrap.min2.css'; ?>"> <link rel="stylesheet" type="text/css" href="<?php echo BASE_URL_PUBLIC.'js/bootstrap/css/bootstrap.min.css'; ?>"> <link rel="stylesheet" type="text/css" href="<?php echo BASE_URL_PUBLIC.'js/bootstrap/css/bootstrap-responsive.css'; ?>"> <link rel="stylesheet" type="text/css" href="<?php echo BASE_URL_PUBLIC.'js/bootstrap/css/bootstrap-responsive.min.css'; ?>"> <link rel="stylesheet" href="<?php echo BASE_URL_PUBLIC;?>js/fancybox/jquery.fancybox.css" type="text/css" media="screen" /> <link rel="stylesheet" href="<?php echo BASE_URL_PUBLIC;?>css/styles.css" type="text/css" media="screen" /> <script type="text/javascript" src="<?php echo BASE_URL_PUBLIC;?>js/jquery-1.8.2.min.js"></script> <script type="text/javascript" src="<?php echo BASE_URL_PUBLIC.'js/bootstrap/js/bootstrap.min.js'; ?>"></script> </head> <body style="font-size:12px;font-family:status-bar,tahoma,verdana,arial,sans-serif!important">


               		 $sql="select * from payment a 

inner join reservation b on a.reserv_id=b.reserv_id

inner join client c on c.client_ID=b.client_ID

where a.receipt_no='".$_REQUEST['receipt_no']."'

limit 1






<?php echo SYS_NAME;?>

<center> <address> Km. 6, Fortuoza Bldg., Diversion Road, Buhangin, Davao City
NonVat Reg.; TIN : 298-882-969 </address> </center> </header>


Date <?php

                                                                               $arr=explode(" ",$row['date_added']);
                                                                               echo $mo." ".$arr[2].", ".$arr[0];

RECEIVED from <?php

                                                                                               echo "Mr. ";
                                                                                               echo "Ms. ";
                                                                                               echo "M ";
                                                                                               echo ucwords($row["applicant_fname"]." ".$row['applicant_lname']);

Address: <?php echo $row['applicant_home_address'];?>

the sum of Pesos <?php

echo ucwords($class->convertCurrencyToWords($row['amount']));

?> (P<?php echo $class->formatcurrency((string)$row['amount']);?>)

in partial / full payment of the stated invoices <?php

if($row['num_of_mos_late']>=1 and $row['penalty_rate']>=1)


echo "having ".$row['penalty_rate']."% interest for ".$row['num_of_mos_late']." month/s";


 ?> .

No.  <?php echo $row['receipt_no'];?>

<?php echo SYS_NAME; ?>

___________________________________ <center>Authorized Signature</center>




</body> </html>

Methods for automatic indentation

There are a number of methods enabling automatic indentation in Vim, ranging from fairly "stupid" and unintrusive ones, like 'autoindent' and 'smartindent', to complex ones such as 'cindent' and custom indentation based on filetype using 'indentexpr'. The amount of indentation used for one level is controlled by the 'shiftwidth' option. (See above.)


'autoindent' does nothing more than copy the indentation from the previous line, when starting a new line. It can be useful for structured text files, or when you want to control most of the indentation manually, without Vim interfering.

'autoindent' does not interfere with other indentation settings, and some file type based indentation scripts even enable it automatically.

'smartindent' and 'cindent'

'smartindent' automatically inserts one extra level of indentation in some cases, and works for C-like files. 'cindent' is more customizable, but also more strict when it comes to syntax.

'smartindent' and 'cindent' might interfere with file type based indentation, and should never be used in conjunction with it.

When it comes to C and C++, file type based indentations automatically sets 'cindent', and for that reason, there is no need to set 'cindent' manually for such files. In these cases, the 'cinwords', 'cinkeys' and 'cinoptions' options still apply.

Generally, 'smartindent' or 'cindent' should only be set manually if you're not satisfied with how file type based indentation works.

File type based indentation

This type of indentation is the most flexible, as it allows users to customize indentation per file type. For instance, the indentation scripts for C and C++ file types properly set the 'cindent' option, and there are very competent indentation scripts for Ruby, Perl and many other languages and file types. File type based indentation even works correctly with Makefiles without interference!

If you plan on using file type based indentation, don't set 'smartindent' or 'cindent'. You may still set 'autoindent', since it doesn't interfere.

The vimrc_example.vim that ships with Vim enables filetype based indentation:

if has("autocmd")
  " Enable file type detection.
  " Use the default filetype settings, so that mail gets 'tw' set to 72,
  " 'cindent' is on in C files, etc.
  " Also load indent files, to automatically do language-dependent indenting.
  filetype plugin indent on
  " ...

Different settings for different file types

You may want indentation for html files to use tabs with 2-columns per indent, while Python files use spaces with 4-columns per indent. To apply suitable settings automatically, first enable file type detection with the following in your vimrc:

filetype plugin indent on

Create file html.vim with contents:

setlocal shiftwidth=2
setlocal tabstop=2

and file python.vim with contents:

setlocal expandtab
setlocal shiftwidth=4
setlocal softtabstop=4

The html.vim and python.vim files should be in this directory (which you may need to create):

  • ~/.vim/after/ftplugin on Unix-based systems; or
  • $HOME/vimfiles/after/ftplugin on Windows systems

The standard plugins probably do not change settings such as shiftwidth, and in that case the directory ~/.vim/ftplugin (or $HOME/vimfiles/ftplugin) would work as an alternative. However the "after" directory should be used because you intend to override settings from other plugins.

Using the "after" directory as above is recommended, but it is possible to put commands such as the following in your vimrc as an alternative:

autocmd FileType html setlocal shiftwidth=2 tabstop=2
autocmd FileType python setlocal expandtab shiftwidth=4 softtabstop=4


See also


  • Merge some of following tips.

Indenting code

Auto indent

Select a block of lines having the same indent


Related plugins

  • Indent Finder always set the correct indentation for the file you are editing. It requires a Python enabled Vim.
  • YAIFA is a VimL port of Indent Finder, so it doesn't need Python to work.
  • DetectIndent automatically detects indent settings.
  • IndentConsistencyCop checks the whole buffer or a given range of it for indentation consistency.
  • Coding_style allows setting indent styles per project.
  • vim-pasta allows for pasting with automatic adjusting of indentation to destination context.


The following should be merged to 224 (or maybe this tip), or deleted:

The coding_style plugin allows to set indent styles per project. Maybe it's worth mentionning here? Chikamichi 22:22, September 12, 2009 (UTC)

I think that's too involved for an introduction tip like this one. (Spiiph 08:57, October 14, 2009 (UTC))
I agree that it shouldn't be in the tip proper, but we often have a "related plugins" section where people can add links to plugins related to the tip with a brief description. I have done this, above. We should probably also link to the plugnis that are mentioned in the tip here, for easy reference. --Fritzophrenic 13:44, October 14, 2009 (UTC)

Around Wikia's network

Random Wiki