Back in 2007, I had a rough beginning as a design engineer at a semiconductor company in terms of using software tools. Linux command line, Vim and Perl were all new to me. I distinctly remember progressing from dd (delete current line) to d↓ (delete current line as well as the line below) and feeling happy that it reduced time spent on editing. Since I was learning on the job, I didn't know about count prefix or the various ways I could've deleted all the lines from the beginning of the file to the line containing a specific phrase. Or even better, I could've automated editing multiple files if I had been familiar with sed
or progressed that far with Perl.
I also remember that we got a two-sided printed cheatsheet that we kept pinned to our cabins. That was one of the ways I kept adding commands to my repertoire. But, I didn't have a good insight to Vim's philosophy and I didn't know how to apply many of the cheatsheet commands. At some point, I decided to read the Vim book by Steve Oualline and that helped a lot, but it was also too long and comprehensive for me to read it all. My memory is hazy after that, and I don't recall what other resources I used. However, I'm sure I didn't effectively utilize built-in help. Nor did I know about stackoverflow or /r/vim until after I left my job in 2014.
Still, I knew enough to conduct a few Vim learning sessions for my colleagues. That came in handy when I got chances to teach Vim as part of a scripting course for college students. From 2016 to 2018, I started maintaining my tutorials on Linux command line, Vim and scripting languages as GitHub repos. As you might guess, I then started polishing these materials and published them as ebooks. This is an ongoing process, with Vim Reference Guide being the twelfth ebook.
Why Vim?You've probably already heard that Vim is a text editor, powerful one at that. Vim's editing features feel like a programming language and you can customize the editor using scripting languages. Apart from a plethora of editing commands and support for regular expressions, you can also incorporate external commands. To sum it up, most editing tasks can be managed from within Vim itself instead of having to write a script.
Now, you might wonder, what is this need for complicated editing features? Why does a text editor require programming capabilities? Why is there even a requirement to learn how to use a text editor? Isn't it enough to have the ability to enter text, use keys like Backspace/Delete/Home/End/Arrow/etc, menubar, toolbar, some shortcuts, a search and replace feature and so on? A simple and short answer — to reduce repetitive manual task.
What I like the most about Vim:
There's a huge ecosystem of plugins, packages and colorschemes as well, but I haven't used them much. I've used Vim for a long time, but not really a power user. I prefer using GVim, tab pages, mouse, arrow keys, etc. So, if you come across tutorials and books suggesting you should avoid using them, remember that they are only subjective preferences.
Here are some more opinions by those who enjoy using Vim:
InstallationShould everybody use Vim? Is it suitable for all kinds of editing tasks? I'd say no. There are plenty of other well established text editors and new ones are coming up all the time. The learning curve isn't worth it for everybody. If Vim wasn't being used at job, I probably wouldn't have bothered with it. Don't use Vim for the wrong reasons article discusses this topic in more detail.
I use the following command on Ubuntu (a Linux distribution):
sudo apt install vim vim-gui-common
Ice BreakerSee https://github.com/vim/vim for source code and other details.
Open a terminal and follow these steps:
gvim ip.txt
opens a file named ip.txt
for editing
vim
if you prefer terminal instead of GUI, or if gvim
is not availablei
, not some alien key)cat ip.txt
— sanity check to see what you typed was saved or notPhew, what a complicated procedure to write a simple line of text, isn't it? This is the most challenging and confusing part for a Vim newbie. Here's a brief explanation for the above steps:
If you launched GVim, you'll likely have Menu and Tool bars, which would've helped with operations like saving, quitting, etc. Nothing wrong with using them, but this book will not discuss those operations. In fact, you'll learn how to configure Vim to hide them in the Customizing Vim chapter.
Don't proceed any further if you aren't comfortable with the above steps. Take help of youtube videos if you must. Master this basic procedure and you will be ready for Vim awesomeness that'll be discussed in the coming sections and chapters.
Material presented here is based on GVim (GUI), which has a few subtle differences compared to Vim (TUI). See this stackoverflow thread for more details.
Built-in tutorOptions and details related to opening Vim from the command line will be discussed in the CLI options chapter.
gvimtutor
command that opens a tutorial session with lessons to get started with Vim
vimtutor
if gvim
is not availableNext step is :h usr_02.txt, which provides enough information about editing files with Vim.
Built-in helpSee also vimtutor-sequel, which provides advanced lessons.
Vim comes with comprehensive user and reference manuals. The user manual reads like a text book and reference manual has more details than you are likely to need. There's also an online site with these help contents, which will be linked as appropriate throughout this book.
Help
menuHere's a neat table from :h help-context:
WHAT PREPEND EXAMPLE Normal mode command:help x
Visual mode command v_
:help v_u
Insert mode command i_
:help i_<Esc>
Command-line command :
:help :quit
Command-line editing c_
:help c_<Del>
Vim command argument -
:help -r
Option '
:help 'textwidth'
Regular expression /
:help /[
Vim learning resourcesYou can go through a copy of the documentation online at https://vimhelp.org/. As shown above, all the :h hints in this book will also be linked to the appropriate online help section.
As mentioned in the Preface chapter, this Vim Reference Guide is more like a cheatsheet instead of a typical book for learning Vim. In addition to built-in features already mentioned in the previous sections, here are some resources you can use:
Tutorials
Books
Interactive
Modes of OperationSee my Vim curated list for a more complete list of learning resources, cheatsheets, tips, tricks, forums, etc.
As mentioned earlier, Vim is a modal editor. This book will mainly discuss these four modes:
This section provides a brief description for these modes. Separate chapters will discuss their features in more detail.
For a complete list of modes, see :h vim-modes-intro and :h mode-switching. See also this comprehensive illustration of navigating modes.
Insert mode
This is the mode where the required text is typed. There are also commands available for moving around, deleting, autocompletion, etc.
Pressing the Esc key takes you back to the Normal mode.
Normal mode
This is the default mode when Vim is opened. This mode is used to run commands for operations like cut, copy, paste, recording, moving around, etc. This is also known as the Command mode.
Visual mode
Visual mode is used to edit text by selecting them first. Selection can either be done using mouse or visual commands.
Pressing the Esc key takes you back to the Normal mode.
Command-line mode
This mode is used to perform file operations like save, quit, search, replace, execute shell commands, etc. An operation is completed by pressing the Enter key after which the mode changes back to the Normal mode. The Esc key can be used to ignore whatever is typed and return to the Normal mode.
The space at the bottom of the screen used for this mode is referred to as Command-line area. It is usually a single line, but can expand for cases like auto completion, shell commands, etc.
Identifying the current mode|
cursor
-- INSERT --
can be seen on the left hand side of the Command-line area-- VISUAL --
or -- VISUAL LINE --
or -- VISUAL BLOCK --
according to the visual command usedVim philosophy and featuresSee also :h 'showmode' setting.
Commands discussed in this section will be covered again in later chapters. The idea here is to give you a brief introduction to modes and notable Vim features. See also:
- Best introduction to Vi and its core editing concepts explained as a language (this stackoverflow thread also has numerous Vim tips and tricks)
- Seven habits of effective text editing
As a programmer, I love how composable Vim commands are. For example, you can do this in Normal mode:
d
is the delete command awaiting further instructionG
is a motion command to move to the last line of the filey
is the yank (copy) command awaiting further instructionMost Normal mode commands accept a count prefix. For example:
3
to the number under the cursorThere are context aware operations too. For example:
{}
including the {}
charactersIf you are a fan of selecting text before editing them, you can use the Visual mode. There are several commands you can use to start Visual mode. If enabled, you can even use mouse to select the required portions.
1
for the first line, by 2
for the second line, by 3
for the third line and so onThe Command-line mode is useful for file level operations, search and replace, changing Vim configurations, talking to external commands and so on.
call
animal
with mammal
only for the lines containing cat
3
to 8
(uses an external command sort
)Changes to Vim configurations from the Command-line mode are applicable only for that particular session. You can use the vimrc
file to load the settings at startup.
8
)teh
to the
in Insert modeThere are many more Vim features that'd help you with text processing and customizing the editor to your needs, some of which you'll get to know in the coming chapters.
Finally, you can apply your Vim skills elsewhere too. Vim-like features have been adopted across a huge variety of applications and plugins, for example:
See Where Vim Came From if you are interested in knowing Vim's history that traces back to the 1960s with qed
, ed
, etc.
Here's a list of remaining chapters:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4