How to Use Auto-indent in Vim

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.

You can also set the indent feature using the command:

$ :filetype indent on

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

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

$ :set filetype

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

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:

$ set si

$ set ai

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

$ :set shiftwidth=2

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.



from Linux Hint https://ift.tt/3reLhnc

Post a Comment

0 Comments