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:
Stay One Step Ahead of Cyber Threats
- 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.
- 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.
- 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.
- 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:
" Example .vimrc file
" Set the width of a tab character
" Set the number of spaces for each step of (auto)indent
" Convert tabs to spaces
" Set the number of columns for a tab
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:
- Open Terminal: Press Ctrl + Alt + T to open a terminal window.
- 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.
- 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