VI (pronounced as “vee-eye”) was the default text editor for the original UNIX operating system. VIM, an improved version of VI, has been developed by Bram Moolenaar since 1991. It is a cross-platform, powerful, terminal-based text editor.
VIM holds a pivotal role in my daily computing endeavors. Since its initial release in 1991, it has maintained its relevance and impact. Here’s why VIM stands out:
- Freedom and Licensing:
VIM is free software, generously licensed under a GNU-like license. This openness empowers users to explore and customize it without constraints.
- Speed and Versatility:
VIM’s speed is legendary. Whether I’m editing code, or tweaking configuration files, VIM responds swiftly. Its cross-platform nature ensures consistency across different operating systems.
- Terminal-Based Efficiency:
VIM's distinctive feature lies in its multimodal editing style. Keystrokes become powerful commands, allowing me to navigate, edit, and manipulate text seamlessly. The absence of menus keeps my focus sharp, and the terminal interface feels like home. In summary, VIM isn’t just an editor; it’s my trusted companion—a bridge between thought and action in the digital realm.
Vim Modes
- Normal Mode:
Normal mode is where Vim starts by default. In this mode, you perform actions like navigation, text substitution, cut, copy, paste, and deletion. You cannot directly write text in Normal mode; it’s primarily for executing commands.
- Insert Mode:
Insert mode is what you’d expect from most text editors. Here, you can directly edit the contents of your files by typing alphanumeric characters. To switch to Insert mode from Normal mode, press the i key. To return to Normal mode, press Esc.
- Visual Mode:
Vim lacks mouse-based text selection, so Visual mode fills that gap. Use Visual mode to select text using arrow keys. Once selected, you can perform various operations (cut, copy, paste, delete, etc.). Unlike Normal mode, Visual mode allows you to select any portion of text, not just whole words or lines.
- Command-Line Mode:
In Command-Line mode, you can execute Vim commands via its own command-line prompt. To enter Command-Line mode, press : while in Normal mode. From there, you can issue commands.
- Normal Mode:
- Purpose: Navigate, manipulate, and execute commands.
- Keystroke to Enter: Start Vim in Normal mode.
- Actions: Move cursor (hjkl). Delete, copy, paste, and more.
- Exit to Other Modes: Press i to enter Insert mode or : for Command-Line mode.
- Insert Mode:
- Purpose: Edit text directly.
- Keystroke to Enter: Press i in Normal mode.
- Actions: Type and edit text.
- Exit to Other Modes: Press Esc to return to Normal mode.
- Visual Mode:
- Purpose: Select text visually.
- Keystroke to Enter: Press v in Normal mode.
- Actions: Select text (arrow keys). Perform operations on selected text.
- Exit to Other Modes: Press Esc to return to Normal mode.
- Command-Line Mode:
- Purpose: Execute Vim commands.
- Keystroke to Enter: Press
: in Normal mode. - Actions: Issue commands (e.g., save, quit, search).
- Exit to Other Modes: Press Esc to return to Normal mode.
Navigating in VIM: Beyond the Basics
In Vim (cursor movement within the buffer traditionally relies on the h, j, k, and l keys in normal mode. However, Vim also supports navigation using the arrow keys, which can be helpful for users accustomed to modern text editors. By mastering these navigation techniques, you can keep both hands on the keyboard and enhance your coding or writing efficiency.
You can try Vim's movements and keyboard shortcuts on the online Vim simulator at this link.
Using the Mouse in Vim
While Vim encourages keyboard-centric navigation, you can still use the mouse if you prefer. To enable mouse support, type the following command in normal mode:
:set mouse=a
Press Enter to apply the setting. Now you can use the mouse to move the cursor or select text.
Closing Windows
To close a help page or any other window in Vim, use the following command:
:q
The mnemonic for this command is “quit.” Press Enter, and Vim will return you to your initial buffer.
Saving Content
In normal mode, you can save the content of your buffer to a file. Type the following command:
:w filename
Replace filename with the desired name for your file. Vim will save the buffer content to a file in the current directory of the underlying shell.
Running Shell Commands
Vim’s normal mode allows you to execute shell commands directly. For example, to display the current directory, type:
:!pwd
Press Enter, and Vim will execute the shell command and show the result.
Exiting Vim
Exiting Vim has often been cited as an example of its steep learning curve. While Vim lacks a conventional exit button, several commands achieve the same result. Here are the most common ones:
Command | Description |
---|---|
:q | Quit Vim (if no unsaved changes) |
:q! | Force quit (discard unsaved changes) |
:w | Save changes without quitting |
:wq or ZZ | Save changes and quit |
:x | Save changes and quit (only if changes exist) |
:e! | Revert to the last saved version (discard changes) |
:qa | Quit all open buffers (useful for multiple files) |
Accessing Vim’s Internal Help System
Vim’s internal help system is a treasure trove of information. To get help on a specific topic, use the :help command followed by the relevant keyword. For example:
:help buffers
Press Enter, and Vim will provide detailed information about buffers, including how to manage them effectively.
Useful Commands
- Undo: Vim provides a powerful undo feature. Press u repeatedly to progressively undo your editing, starting from the most recent change.
- Search for Text: To search for text within the buffer, press / followed by the text you want to find. Repeating / will locate the next occurrence of the string. Remember that search is case-sensitive. To search backward, use ? instead of /.
- Substitution: Vim supports a similar command to sed’s “s/foo/bar/g”. In normal mode, type the following to replace all instances of the string “foo” with “bar”:
:%s/foo/bar/g
The % represents the current buffer.
- Splits
Horizontal Splits: Sometimes you need to work with multiple files simultaneously. Vim allows you to split the screen horizontally into two separate buffers. Use the following command:
:split [filename]
If you specify a filename, the new buffer at the top will display that file.
Vertical Splits: Similarly, you can split the current buffer vertically using:
:vsplit [filename]
Navigate between splits in normal mode using CTRL-w. Mouse support is also available if enabled.
- Tabs
Vim’s tabs differ from web browser tabs. Each Vim tab is a collection of windows or viewports. To create a new tab (which is essentially a separate buffer), use:
:tabnew
You can switch between tabs using gt (next tab) and gT (previous tab).
Mastering Vim’s Visual Mode
Vim’s visual mode is a powerful tool for selecting and manipulating text within the buffer. Whether you need to copy, cut, or reorganize content, visual mode streamlines the process. Let’s dive into the details:
Activating Visual Mode
- Character-wise Selection (v): In normal mode, press v to enter character-wise visual mode. Now you can use the cursor keys (arrow keys) to highlight the desired text.
- Line-wise Selection (V): For line-wise selection, press V in normal mode. This selects entire lines, making it easy to copy or cut entire paragraphs or code blocks.
- Block-wise Selection (CTRL-v): To select columns or blocks of text, use CTRL-v in normal mode. This is particularly useful for working with tabular data or code indentation.
Copying and Pasting
- Copying (Yank): Once you’ve selected the desired text, hit y (short for “yank”) to copy it. Move to a new location in your text and press p to paste the copied text.
- Cutting (Delete): To cut the selected text, hit d (mnemonically for delete). Afterward, navigate to the new position in your document and press p to “put” the text you’ve just deleted.
- Whole Line Selection: Hold down SHIFT-v in visual mode to select entire lines. This is handy for quickly copying or moving entire paragraphs.
- Column Selection: Use CTRL-v to select columns. This allows you to manipulate specific columns of text, such as adjusting indentation or replacing content in a structured manner.
Customizing Vim: The .vimrc File
Vim allows you to customize its behavior by creating a .vimrc file in your home directory. This file contains a sequence of initialization commands that Vim executes at startup. You can tailor Vim to your preferences by adding settings, key mappings, and plugins to this file
Some of the most important keybindings used to toggle different modes in Vim:
Customizing Vim with Plugins
While Vim comes with powerful built-in functionality, its true strength lies in the ability to customize it using user-defined plugins. These plugins enhance Vim’s capabilities by adding specific functions and features.
What Are Vim Plugins?
A Vim plugin is essentially a script file that extends Vim’s functionality. When you drop a plugin into the right directory (usually $HOME/.vimrc/plugins), Vim loads it automatically during startup.
Vim’s plugin ecosystem has evolved significantly. Nowadays, Vim is not just a text editor; it’s a powerful platform for extending functionality through plugins. Explore the vast array of available plugins, experiment, and find the ones that enhance your Vim experience!
Vim 9.0
Since version 9.0, Vim has introduced a new scripting language that significantly improves Vimscript in terms of performance, readability, and flexibility. This new language, allows users to write more concise and expressive scripts. It provides better error handling, enhanced data structures, and improved memory management. Many Vim enthusiasts, myself included, have welcomed this upgrade. This new language empowers users to write more concise and expressive scripts, and to create more powerful customizations and plugins while maintaining the efficiency that Vim is renowned for.
Easy Mode
Vim offers an easy mode that simplifies its behavior, making it more familiar to users accustomed to regular click-and-type editors. Here’s how to use Vim in easy mode:
- Enabling Easy Mode:
To activate easy mode, launch Vim from the terminal with the -y argument. For example:
vim -y
In easy mode, Vim behaves like a typical text editor, allowing you to use familiar shortcuts.
- Standard Shortcuts:
- Ctrl + S: Save (equivalent to :w)
- Ctrl + C: Copy (yank) text
- Ctrl + V: Paste (put) text
- Ctrl + Z: Undo
- Ctrl + Y: Redo
- Ctrl + Q: Quit
Easy mode is especially helpful for beginners transitioning to Vim, as it bridges the gap between traditional editors and Vim’s powerful features