This post may contain affiliate links, please read our affiliate disclosure to learn more.
vim: tabstop, shiftwidth, softtabstop, expandtab (Explained)

vim: tabstop, shiftwidth, softtabstop, expandtab (Explained)

 By Charles Joseph | Cybersecurity Researcher
 Published on December 26th, 2023
This post was updated on December 29th, 2023

Vim is a highly configurable, powerful text editor widely used for programming and file editing in command-line environments. The .vimrc file is a configuration file used by Vim, where users can set preferences and customize settings to tailor the editor’s behavior to their needs.

This following was summarized from a post by Ari Sweedler on Medium, which discusses the .vimrc file and provides guidance on using it to customize your tab and indentation settings:

NordVPN 67% off + 3-month VPN coupon

Stay One Step Ahead of Cyber Threats

Want to Be the Smartest Guy in the Room? Get the Latest Cybersecurity News and Insights.
We respect your privacy and you can unsubscribe anytime.
  1. tabstop — Determines the width of a tab character in columns. This setting affects the display of tabs in the editor. For example, a tab could be set to equal 4, 2, or 8 columns. The value must be more than 0, as it’s impractical for tabs to have no width.
  2. shiftwidth — Defines the amount of whitespace used for one level of indentation, in columns. This setting influences various Vim commands and behaviors, such as shift-left-right commands, formatting commands, and how Vim operates with cindent or autoindent. A shift command adjusts the indentation level, and formatting commands determine the required indentation layers. For example, if Vim decides a line needs 3 levels of indentation, it will insert 3 times the shiftwidth columns of whitespace.
  3. expandtab — This is a Boolean setting. It determines whether Vim should use spaces or tabs when executing a whitespace command or when the tab key is pressed.
  4. softtabstop — Affects the behavior of the tab and backspace keys. It specifies the amount of whitespace to be inserted when the tab key is pressed and the amount to be removed when the backspace key is pressed.

Example of a .vimrc File

Creating a .vimrc file with the mentioned settings is quite straightforward. Below is an example of what this file might look like:

In this example, tabstop, shiftwidth, and softtabstop are all set to 4, which is a common configuration for maintaining consistency in indentation. expandtab is set to convert tabs to spaces.

Location of the .vimrc File in Ubuntu

In Ubuntu, the .vimrc file typically resides in the user’s home directory. You can find or create it at this path:

Here’s how you can create or edit this file:

  1. Open Terminal: Press Ctrl + Alt + T to open a terminal window.
  2. Create/Edit .vimrc File: Type vim ~/.vimrc and press Enter. This command opens the .vimrc file in Vim. If the file doesn’t exist, Vim will start with a new, blank file.
  3. Edit and Save: You can then insert the settings as shown above. To save and exit, press Esc, type :wq, and then press Enter.

Remember, replace “yourusername” with your actual username on your Ubuntu system. If you can’t see the file in your file manager, it might be because it’s a hidden file (files starting with a dot . are hidden in Unix-like systems). You can usually view hidden files by pressing Ctrl + H in the file manager.

"Amateurs hack systems, professionals hack people."
-- Bruce Schneier, a renown computer security professional
Scroll to Top