Open In App

How to Indent Code in Vim Editor?

Last Updated : 02 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Vim is an open-source text editing software that is mostly used by and among developers to create a simple readme file or documentation, or even write scripts for basic purposes. Vim gives us various customisability options, and shortcuts to make documentation and text editing easier and efficient.

Proper code indentation and formatting are necessary to make the code more readable and maintainable. Every code editor has its methods of formatting, and in this article, we will learn how to indent code written in the Vim code editor properly.

How to Indent Code in Vim Editor

Vim gives us different options or methods to control the indentations present in a file that is being edited using the Vim editor. Let’s look at different methods and their examples of how we can indent code in vim.

1. Manual Indentation

Manual indentation in Vim involves pressing keystrokes to apply indentation manually to each line in the file. We press the “>>” command in the view mode to indent a line, and “<<“ to de-indent, or remove the indentation from the line.

Example

JavaScript
// without indentation
function main() {
console.log("Hello, World!");
console.log("This is an article to indent code!");
}

// with indentation
function main() {
        console.log("Hello, World!");
        console.log("This is an article to indent code!");
}


Output:

vim

Automatic Indentation

We can also auto-adjust the indentation settings inside the vim editor, so that every new line added is indented automatically, without the need of pressing any manual additional keystrokes. We can do this by performing the following steps:

  • open the file in vim editor using “vim main.js”, and press “Esc” to enter command mode.
  • Type “:set autoindent” and press Enter
  • Now, enter any line in the editor, and vim will place the same indentations on the new line, as is present on the previous line.

Output:

vim-2

Visual Mode Indentation

Vim also allows us to indent multiple lines of code at once. To indent multiple lines in the editor, we can place the cursor on the line, and then enter the Visual mode using “V”. In the mode, then select the lines, and then press `>` to indent the selected lines.

Output:

vim-3

Indentation commands

Vim also gives us some indentation commands, like “==” or “=G” to indent specific lines or the end of file.

Example, using “==” to indent specifc lines:

vim

Conclusion

We learned how to format and indent code in vim code editor. Code formatting and indentation is necessary to make the code more readable and maintainable. Vim editor gives us various options to indent our code, from manual editing the code using “>>” or “==”, to automatically editing the code using “:set autoindent” command, or indenting multiples lines at once using the “>” keystroke in the view mode.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads