Introduction: What is Vim?
Vim is a highly configurable, powerful, console-based text editor that is an improvement over the older "vi" system program. It's loved by many developers for its speed, efficiency, and lower resource consumption. It's available for various platforms and is pre-installed on most Unix-based systems, including macOS and Linux.
Vim comes pre-installed on macOS and most Linux distributions. For Windows, you can download Vim from its official website.
- For Ubuntu or other Debian-based Linux distributions, use this command to install or update Vim:
sudo apt-get install vim
For macOS, you can use Homebrew to install Vim:
brew install vim
- For Windows, download Vim from its official website: https://www.vim.org/download.php
2. Basics: Navigating the Vim Interface
To start Vim, open your terminal and type
Once inside Vim, you are in what's called "Normal" mode, where you can navigate around the text, delete words, copy/paste, etc., but you can't directly input text. To start inputting text, you have to switch to "Insert" mode.
3. Vim Modes
Understanding the modes is crucial to using Vim effectively. Vim has several modes, but the ones you'll use most often are:
- Normal mode: Navigate and manipulate text.
- Insert mode: Insert text.
- Visual mode: Visually select text.
- Command-line mode: Enter commands.
To switch between modes:
- From Normal to Insert: Press
- From anything back to Normal: Press
- From Normal to Visual: Press
- From Normal to Command-line: Press
4. Basic Commands
Saving and quitting
:w- save (write) the file
:q- quit Vim
:wq- save and quit
:q!- quit without saving
h- move left
j- move down
k- move up
l- move right
i- enter insert mode
x- delete character under cursor
dd- delete line
u- undo last operation
Ctrl + r- redo last undo
5. More Advanced Commands
Y- yank (copy) the current line
p- paste the yanked text after the cursor
P- paste the yanked text before the cursor
/- search for a pattern in the text (press
nto go to the next occurrence,
Nfor the previous one)
%- move to matching parenthesis
:s/foo/bar/g- replace all occurrences of 'foo' with 'bar' in the current line
:1,$s/foo/bar/g- replace all occurrences of 'foo' with 'bar' in the entire file
6. Learning More
vimtutor: This is a built-in tutorial you can access by typing
vimtutorinto your terminal. It will walk you through the basics and some advanced features of Vim.
- Vim Help Files: Vim comes with extensive help files. Access them by typing
:helpin Command mode.
- Practice: The best way to get comfortable with Vim is to use it daily. Make it your primary text editor for a few weeks.
- Explore Plugins: Vim supports plugins that can provide you with more features and increased productivity. Some of the most popular plugins are NERD tree for file system exploration, Vim-airline for a fancy status bar, and YouCompleteMe for auto-completion.
7. Creating a Vim Configuration File
Vim allows you to create a configuration file (
vimrc) to customize your editing experience. The file resides in your home directory and can be created using
vim ~/.vimrc. Some useful settings to add to your
syntax on " Enable syntax highlighting
set number " Show line numbers
set tabstop=4 " A tab is four spaces
set expandtab " Use spaces instead of tabs
set autoindent " Keep the indent level when creating a new line
set smartindent " Automatically inserts additional indentation in some cases
Remember to save and close (
:wq) after making your changes, then re-open Vim to see them applied.
8. Vim Cheat Sheets
Consider using Vim cheat sheets available online as quick reference tools. They often categorize commands for easy retrieval.
I use vim.rtoor.com as my go-to cheat sheet for vim commands.
9. Learn Vimscript
If you want to extend Vim’s functionality, you might want to learn Vimscript, Vim's built-in scripting language. It allows you to create custom functions, commands, and much more.
Vim has a steep learning curve but offers a lot of power and efficiency in return. The most important part is to be patient and persistent. It will take some time to get used to, but once you've become familiar with it, you'll be able to edit text very efficiently. Good luck on your Vim journey!