If you spend your Linux time in the command-line, you probably use Vim as your default text editor. Vim is a powerful and modern text editor with many features suitable when working in the terminal. Although Vim is an incredible text editor, getting started and using it to perform basic operations can be daunting. Therefore, getting the basic concepts will help reduce the overwhelming feeling when using Vim.

This guide focuses on one essential Vim feature: performing indentation when editing files.

How to Turn On Auto Indent in Vim

To automatically indent when editing a file in Vim, enable the auto indenting feature using the :set autoindent flag in command mode:

Press Enter, and this will auto-indent the file you are currently editing.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image1-25.png" data-lazy- height="435" src="data:image/svg xml,” width=”580″>

You can also set the indent feature using the command:

If you set the auto-indent feature in Vim in command mode, it does not persist upon closing the editor.

To add persistent to the settings, edit the vimrc file in /etc/vim/vimrc, and add the entry:

$ filetype indent on

$ filetype plugin indent on

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image3-24.png" data-lazy- height="231" src="data:image/svg xml,” width=”535″>

This setting will automatically indent files based on the file type. To check if the filetype is supported, enter:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image2-24.png" data-lazy- height="362" src="data:image/svg xml,” width=”533″>

Once you get the file type, make sure it is supported by navigating to /usr/share/vim/vim82/indent

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image4-22.png" data-lazy- height="305" src="data:image/svg xml,” width=”495″>

You can change vim82 to your Vim version.

NOTE: If the file type you are using is not available by default, you can add one.

Vim has four methods of indentation, namely:

Autoindent – this method uses indent from the previous line for the file type you are editing.

smartindent – smartindent works similarly to autoindent but recognizes the syntax for some languages such as C language.

cindent – cindent is slightly different from autoindent and smartindent as it is more clever and is configurable to various indexing styles.

indexexpr – is the most efficient and flexible. It uses expressions to calculate the indent of a file. When enabled, indexexpr overrides other indenting methods.

NOTE: If Vim encounters an unrecognized file type, it might not indent correctly. To resolve this, you can enable smartindent and autoindex.

Edit the vimrc file and add the entries:

To modify the indentation spacing, enter the value in command mode as:

The shiftwidth value which describes the level of indentation is the number of whitespace cloumns. Vim indentation methods (cindent and autoindent) rely on this setting to determine indentation levels.

How to Turn off Auto Indent

To disable the auto-indenting feature in Vim, you can disable the entries or set the mode to paste. However, a more efficient way to disable auto indenting on the current file is to set the following in command mode.

$ :set noautoindent

$ :set indentexpr=

$ :set nocindent

$ :set nosmartindent

Conclusion

Understanding the basics of Vim can help improve your workflow and edit files quickly with Vim. Check out our other Vim tutorials to help expand your knowledge.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/john-150×150.png60f2fc946ffd8.jpg" height="112" src="data:image/svg xml,” width=”112″>

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list