set auto indent in vi editor

How to Use Auto Indent in Vi Editor

Vi editor is a popular text editor in Linux used by many developers and administrators around the world. One of the most common requirements, while using vi editor, is to be able to format code better using auto indent features. Vi editor supports auto indent that helps you avoid pressing space or tab key on every line. In this article, we will look at how to enable and customize auto indent in vi editor.


How to Use Auto Indent in Vi Editor

Here are the different ways to use auto indent in vi editor.


1. Turn on Auto Indent

If you are already in vi editor follow these steps at any time, to turn on auto indent feature.

  • press colon (:)
  • type set autoindent or set ai
  • press enter key

Now when you hit a new line, vi editor will automatically indent it as per the indentation of previous line. Also, please note, this auto indent will not persist after you close the editor. To enable auto indent permanently, go to point #3 below.


2. Turn off Auto Indent

If you want to turn auto indent at any point while you are in vi editor, follow these steps.

  • press colon(:)
  • type set noautoindent
  • press enter

Now when you create a new line, vi editor will not auto indent it. However, your previously indented lines will continue to remain as-is and will not be edited.


3. Turn on Auto Indent Permanently

If you want to permanently enable auto indent for all files then open ~/.vimrc file in a text editor.

$ sudo vi ~/.vimrc

and add the following line to it.

set autoindent

~/.vimrc file stores vi editor’s settings and configuration and auto loads it in ever session.


4. Customize Auto Indent

Typically, when you turn on auto indent then vi editor will indent by one space/tab based on the previous line. If you want to change indentation in editor such as auto indent 4 spaces, then follow these steps when you are in vi editor

  • press colon (:)
  • type set shiftwidth=4
  • press enter key

The above changes last only as long as your current vi session. If you want to make these changes permanent then add the following line to ~/.vimrc.

set shiftwidth=4

Now when you indent using space/tab the cursor will shift by 4 spaces.

Here are some more tricks to indent/outdent lines:

  • Press Ctrl+d to outdent or unindent a line
  • Press Tab or space at the beginning of line to indent it
  • Enter >> command to indent current line. If you precede >> with a number, such as 3>>, then vi editor will indent 3 lines starting with current line
  • Similarly, << command will unindent/outdent current line. If you precede it with a number, such as 3<<, then vi editor will outdent 3 lines starting with current line.

In this article, we have learnt many easy commands to enable, disable and customize auto indent in vi editor.

Also read:

How to Setup LogAnalyzer with Rsyslog and MySQL
How to Setup Rsyslog with MySQL
How to Upload & Download Files from FTP in Linux
How to Delete Files Older Than X Days in Linux
How to Check Inode Usage in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *