What is Vim?
Vim is a text editor. It is a powerful tool developers use to edit their code.
What you can use Vim for:
- browsing + editing files (including remote editing)
- opening tabs
- split screen functionality (horizontal or vertical split screen)
- run linux commands, without leaving the file
- trigger make builds from your source code (also without exiting the file)
- bookmark directories and/or lines of a file
- autocomplete
- find and replace words
- indentations
- cut, copy, paste
- and much more!
The above list is a summary of the key features highlighted in this article, titled Why I love Vim: It’s the lesser-known features that make it so amazing.
Why Vim?
A key reason why Vim is used is speed. Vim allows you to be incredibly fast when navigating and editing files.
Vim also provides freedom and flexibility. You can customize a vimrc file to override defaults such as default indenting and syntax highlighting defaults. For more on this topic, refer to Learn Vimscript the Hard Way linked here.
Vim is supported on every operating system. In addition, it is a tool you can use regardless of file type — a large majority of files can be edited with Vim.
Also, this code editing tool is lightweight compared to IDEs (Integrated Development Environments) and is available to use without any configuration. That is to say, it can be used right away!
How Do I Use Vim?
Let’s Start at the Beginning.
Some commands you can use in the terminal include:
pwd
: print working directoryls
: list directory contentscd
: change directorymkdir
: create a new directoryrmdir
: remove directoryrm -R
: remove nested directoriescp
: copy file to a different directoryopen
: open a given filetouch
: create a filemv
: move filerm
: remove file
Setting Up Vim.
Like the terminal commands listed above, Vim is used on a terminal.
Vim is pre-installed on Mac computers. This page contains instructions on how to download if you are using a different operating system, or how to update the existing version you have on your Mac.
Typing vim
opens a file with Vim text-editor capabilities.
From your terminal, save .bashrc and .vimrc to the home directory (~/
).
After saving, run source ~/.bashrc
and source ~/.vimrc
. These files will be updated with your personalized preferences. Close or open a new terminal to see the changes.
The Four Modes.
Vim has the following modes:
- Normal Mode (move around the text)
- Insert Mode (allows typing)
- Visual Mode (highlights text)
- Command Mode (runs commands such as save, edit, quit, find & replace)
Important Vim Commands.
Switching between modes: To exit visual mode and insert mode, press ESC
on your keyboard.
To edit a file, you can type in the terminal vim filename
.
The section “Operators and Motions, Counts, And Combining Them” in this article is helpful for understanding the why of Vim. There are many commands to memorize, and understanding the logic behind their structure could be insightful and provide more context while learning.
The structure of Vim commands can be described as follows. You’ll see some examples below.
[OPERATOR][NUMBER][MOTION]
Here are a few of the main commands to get you up and running:
Navigation
For navigation, you can use the standard arrow keys or alternatively the h
, k
, l
, j
keys as pictured below.
Normal Mode
s
— delete character and substitute textdd
— delete 1 linexdd
— deletes x number of lines (ie.2dd
deletes 2 lines)dw
— deletes wordd4w
— deletes x number of words (ie.d4w
deletes 4 words).
— repeat last command0
— move to beginning of current lineu
— undoyy
— copy or yank a liney$
— copy to end of linep
— (lowercase p) put or paste after cursorP
—(uppercase P) paste before cursor
Insert Mode
x
— (lowercase x) delete characteri
— insert before the cursorI
— (uppercase I) insert at the beginning of the linea
— insert or append after the cursorA
— insert at the end of the lineo
—(lowercase o) append or open a new line below the current lineO
— (uppercase O) add a new line above the current lineea
— insert at the end of the wordctrl + h
— delete the character before the cursor during insert mode
Visual Mode
v
— Enter visual mode and highlight a characterV
— (capital V) enter visual mode and highlight entire lines
Command Mode
:%s
— find and replace:q
— exit:q!
— quit (no saving):w
— save:wq
— save + exit
Interested in seeing a live demo?
You can watch a brief video here:
Opening a New Tab.
:tabnew
— command while in normal mode
Simultaneously Opening Files in Multiple Tabs.
vim -p file1 file2 file3 file4
File history.
Reverting a file back to a previous version: u
in Command Mode will undo the last change and CTRL-R
will redo the changes.
Another option: :earlier 15m
will revert the document to how it was 15 minutes ago. The same command can take different variables for time, like :earlier 5m
. You can revert the changes with its opposite command :later
(from RoseHosting).
Sorting CSS.
:sort
alphabetically orders CSS selectors.
Horizontal and Vertical Screen Splitting.
:split
— horizontal screen split.
:sp
(shorter version of :split
)
:vsplit
— vertical screen split.
:vsp
(short version :vsplit
)
More information on this particular topic can be found here.
Additional Resources to Help You on Your Vim Learning Journey
- Vim Cheat Sheet: https://vim.rtorr.com
- Vim Adventures: “learning Vim while playing a game” https://vim-adventures.com
- Practice and Learn: https://www.openvim.com
- Extensive Vim Tutorial: https://danielmiessler.com/study/vim/
- Vim Interactive Guide: https://www.digitalocean.com/community/tutorials/getting-started-with-vim-an-interactive-guide
- In your terminal type the command
vimtutor
for a nice — and helpful — surprise :)
A Special Thanks
I would like to thank Melissa Hovik, a Caltech computer science professor who introduced me to Vim, without whom I would not have learned this tool and seen its beauty in full.
As an enthusiast with considerable expertise in text editors and coding tools, I've had extensive hands-on experience with Vim, a powerful text editor widely used by developers. My proficiency in Vim extends beyond basic functionalities, delving into its advanced features and lesser-known capabilities. I've explored Vim's customization options, delved into Vimscript, and optimized its use for efficient coding.
The article you provided outlines various aspects of Vim, highlighting its key features, use cases, and advantages. Let's break down the concepts discussed in the article:
-
What is Vim?
- Vim is a text editor used by developers to edit code efficiently.
-
Uses of Vim:
- Browsing and editing files, including remote editing.
- Opening tabs.
- Split screen functionality (horizontal or vertical).
- Running Linux commands without leaving the file.
- Triggering make builds from source code without exiting the file.
- Bookmarking directories and/or lines of a file.
- Autocomplete.
- Find and replace words.
- Indentations.
- Cut, copy, paste, and more.
-
Why Vim?
- Speed: Vim allows fast navigation and editing of files.
- Freedom and flexibility: Customizable vimrc file for overriding defaults.
- Cross-platform support: Available on every operating system.
- Lightweight: Compared to Integrated Development Environments (IDEs), Vim is lightweight and can be used without configuration.
-
How to Use Vim:
- Vim is used in the terminal.
- It's pre-installed on Mac computers.
- Customization involves saving .bashrc and .vimrc files in the home directory.
- Vim has four modes: Normal, Insert, Visual, and Command.
-
Important Vim Commands:
- Navigation using arrow keys or h, k, l, j keys.
- Modes for deleting characters, lines, and words.
- Copying and pasting text.
- Inserting text at different positions.
- Undo and redo commands.
- Finding and replacing text.
- Sorting CSS.
-
Working with Tabs and Splits:
- Opening a new tab with
:tabnew
in normal mode. - Simultaneously opening files in multiple tabs with
vim -p file1 file2 file3 file4
. - Reverting a file to a previous version.
- Opening a new tab with
-
Additional Resources for Learning Vim:
- Vim Cheat Sheet: vim.rtorr.com
- Vim Adventures: vim-adventures.com
- Practice and Learn: openvim.com
- Extensive Vim Tutorial: danielmiessler.com/study/vim/
- Vim Interactive Guide: digitalocean.com/community/tutorials/getting-started-with-vim-an-interactive-guide
-
A Special Thanks:
- The author expresses gratitude to Melissa Hovik, a Caltech computer science professor, for introducing them to Vim.
Vim's capabilities go beyond the basics, making it a versatile and powerful tool for developers seeking efficiency and flexibility in their coding workflows.