How to Indent Code in Vim Editor?
Last Updated :
25 May, 2024
Vim is a popular text editor used by many developers for writing code, creating readme files, and writing scripts. It's open-source and offers a lot of ways to customize your experience. This makes it easier and more efficient to work with. Properly formatting and indenting your code is important because it makes your code easier to read and manage. In this guide, we will learn three simple ways to auto-indent code in Vim editor.
When you format code in Vim editor, it helps to keep things neat and organized. We will cover how to indent in Vim using different commands. You'll learn the indent command in Vim editor and find out about the Vim auto indent shortcut. Additionally, we’ll show you the Vim command to indent code and how to use Vim auto-indent for all lines. With these tips, you can improve your coding workflow in Vim.
Methods to Auto Indent Code in Vim Editor
How to Indent Code in Vim Editor (3 Ways)
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.
Method 1: Indent Code using 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:

Method 2: Auto Indent Code using 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:

How to Indent multiple lines of Code at once?
Vim also allows us to indent multiple lines of code at once using Visual Mode Indentation method. 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:

Method 3: Indent Code using 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:

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.
Similar Reads
How to Copy Paste in Vim Editor Copying and pasting in Vim Editor is an essential operation for efficient text editing. Vim offers several methods to copy and paste content, including visual mode selection, yanking, and putting. Understanding these techniques can significantly enhance your productivity when working with Vim. In th
3 min read
How to Delete Multiple Lines in Vi editor Linux was developed by Linus Torvalds in 1991 as a hobby project. It is an open-source (source code that can be used by anyone freely) kernel that is most popular and widely used in the industry as well as in personal systems. There are various operating systems based on the Linux kernel, some of th
5 min read
How to Install Vim Editor in MacOS? Vim editor is an open-source software developed by "Bram Moolenaar" in 1991. Vim, short for "Vi IMproved," is a highly customizable and powerful text editor that is most commonly used by developers, system administrators, writers, and anyone who works extensively with text files. Vim is created as a
3 min read
How to undo in Vim editor in Linux Vim (short for Vi Improvised) is a popular text editor mainly used in Linux and Unix-based operating systems but due to having a CLI (command line interface), it might be difficult for new users to make use of basic features such as undo in Vim. So in this article, we will learn different ways and t
3 min read
How to Install Vim Editor in Ubuntu? Vim is a highly configurable and powerful text editor used by developers and system administrators on Ubuntu and other Linux distributions. Whether youâre coding, writing scripts, or editing configuration files, Vim offers efficiency with its vast features and keyboard shortcuts. In this guide, we'l
3 min read