Intro To The Vim Text Editor
These are my notes on an introduction to Vim.
Why should you learn Vim? Vim is on almost every Linux system. When you have to
be on different Linux systems regularly, it is by far the best thing to do. Vim
is also just powerful and will do just about anything a developer or writer
would want it to do. I do not switch systems very much and I still prefer it
over word processors and other editors. However, everyone's situation is
different and sometimes it just makes sense to use something else for your
workflow.
Vim is very lightweight and fast. These are attributes that I value in an
editor. I do not want a piece of software that is so large it makes a noticeable
dent in my drive space. Those word processors and editors are much slower too. I
want speed. I want to be able to do things as fast as I can think or type it.
That is one of the advantages of Vim.
Of course, every developer and writer will have particular needs. I write my
articles using Vim and a couple of features I want is distraction free writing
and highlighting the paragraph I am in. Vim has plugins that do just that. So I
installed those 2. That's it. I did not have to put up with 40 other features I
do not want in order to use Vim. That makes Vim flexible and extensible. I can
modify it for whatever writing purpose I need and nothing more. It stays fast
and small, just the way I like software.
Opening Vim
To open Vim, we just go to our command line and type:
vim
This will give us our opening Vim page with how to access help and other things.
The next thing to know about Vim is that it is a modal editor. This means it has
different modes to work in. Command mode is where you start at. Insert mode lets
enter in text. There is also visual and other modes. We will get to those
eventually.
To close Vim, we type :q and then press enter.
:q then press enter
Since we have not typed anything, we can just exit like this. Now, let us open a
new file, type some stuff, and save. Type from the command line:
vim sample.py
You will get a blank screen. You start in command mode. To enter in text, we
must get to insert mode. Type:
i then press enter
Now you will notice you can type text. Also, at the bottom left of your screen,
you will see the mode indicated --insert--. This will let you know what mode you
are in. If you do not see anything listed there, you are in command mode.
So type some text, it does not matter what at this point. We are just learning.
Now, here is an important part to remember. To exit any mode, hit the escape
button to go back to command mode. Then you will hit :w then press enter to save
your document.
escape
:w then press enter
You can also type :q after that to quit or you can do it at the same time.
escape
:w
then
:q
Or
escape
:wq
Vim has many options and you can learn them as needed but this will get you
started.
Moving Around Files
While in command mode, Vim lets you use certain keys to quickly move around your
document. This is important when your document or configuration file is large.
You will appreciate it then. It is much faster than using the mouse to move
around or scroll with.
- l right one character
- h left one character
- j down one line
- k up one line
- 0 go to beginning of current line
- $ go to end of current line
- ^ go to first non-whitespace character on current line
- w go to the beginning of the next word
- b go to beginning of previous word
- ctrl-F down one page
- ctrl-B up one page
For more fun, most of these commands can be prefixed with a number to indicate
how many times a certain command is to be carried out. Try that to see how it
works.
So, now that we know some of the basics, let's think about editing. Most editing
consists of inserting text, deleting text, and moving text to different places
in your document.
If you press 'u' while in command mode, vim will undo the last change that you
made.
u - undo last change you made
This will come in handy as we try some of the basic editing commands.
Vim has several different ways of entering insert mode. We have already used the
'i' command to insert text. If we wanted to add some text to the end of a
sentence, we would use the 'a' command. If we move the cursor to the end of the
line and type 'a', the cursor will move past the end of the line and vim will
enter insert mode. This will allow us to add some more text.
move cursor to end of line
press 'a'
enter more text
Remember to press the 'esc' key to exit insert mode. Because we will almost
always want to append text to the end of a line, vim offers a shortcut to move
to the end of the current line and start appending. It is the 'A' command. This
moves your cursor to the end of the line and automatically puts you in insert
mode.
We can also insert text by opening a line. This inserts a blank line between two
existing lines and enters insert mode.
o - opens a line below the current line
O - opens a line above the current line
Vim also offers a variety of ways to delete text. The 'x' command will delete a
character at the cursor location. It may be preceded by a number specifying how
many characters are to be deleted.
The 'd' command is more general purpose. Like 'x', it may be preceded by a
number specifying the number of times the deletion is to be performed. In
addition, 'd' is always followed by a movement command that controls the size of
the deletion.
- x current character
- 3x the current character and the next two characters
- dd the current line
- 5dd the current line and the next four lines
- dw from the current cursor position to the beginning of the next word
- d$ current cursor location to the end of the current line
- d0 current cursor location to the beginning of the line
- d^ current cursor location to the first non-whitespace character
- dG current line to the end of the line
- d20G current line to the 20th line of the file
The 'd' command not only deletes text, it also cuts text. Each time we use the
'd' command, the deletion is copied into a paste buffer that we can later recall
with the 'p' command to paste the contents of the buffer either before or after
the cursor.
The 'y' command is used to yank or copy text in much the same way the 'd'
command is used to cut text.
- yy current line
- 5yy current line and the next 4 lines
- yW from current cursor position to beginning of next word
- y$ from current cursor location to end of the current line
- y0 from current cursor location to beginning of the line
- y^ from current cursor location to first non-whitespace character in line
- yG from current line to the end of the line
- y20G from current line to the 20th line of the file
Vim is rather strict about its idea of a line. Normally, it is not possible to
move the cursor to the end of a line and delete the end of line character to
join one line with the one below it. Because of this, vim provides a specific
command, 'J', to join lines together.
Vim also has the capability to move the cursor to locations based on searches.
It can do this either on a single line or over an entire file. It can also
perform text replacements with or without confirmation from the user.
The 'f' command searches a line and moves the cursor to the next instance of a
specified character. For example, the command 'fa' would move the cursor to the
next occurrence of the character 'a' within the current line. After performing a
character search within the line, the search may be repeated by typing a
semicolon.
To move the cursor to the next occurrence of a word or phrase, the '/' command
is used. This works the same way as we learned earlier in the 'less' program.
When you type the '/' command, a '/' will appear at the bottom of the screen.
Next, type the word or phrase to be searched for, followed by the 'enter' key.
The cursor will move to the next location containing the search string. A search
may be repeated using the previous search string with the 'n' command.
Vim uses an 'ex' command to perform search and replace operations over a range
of lines or the entire file. To change the word Line to line for the entire
file, we would enter the following command:
:%s/Line/line/g
: colon character starts an 'ex' command
% specifies the range of lines for the operation
s specifies the operation, search and replace
g means global in the sense that the search and replace is performed on
ever instance of the search string in the line.
It is often useful to edit more than one file at a time. You might need to make
changes to multiple files, or you might need to copy content from one file into
another. With vim, we can open multiple files for editing by specifying them on
the command line.
To switch from one file to the next, use this 'ex' command:
:bn
To move back to the previous file:
:bp
While we can move from one file to another, vim enforces a policy that prevents
us from switching files if the current file has unsaved changes. To force vim to
switch files and abandon your changes, add an exclamation point to the command.
In addition to the switching method already described, vim provides some
'ex' commands that make multiple files easier to manage. We can view a list of
files being edited with the :buffers command. Doing so will display a list of
the files at the bottom of the display.
:buffers
To switch to another file type, :buffer followed by the number of the buffer you
want to edit. For example, to switch from buffer 1 containing a file to buffer
2 containing another file, we would type this:
:buffer 2
Our screen now displays the second file. Another way we can change buffers is to
use the :bn and :bp commands mentioned earlier.
It is also possible to add files to our current session. The 'ex' command ':e',
short for edit, followed by a filename will open an additional file. Let us end
our current editing session and return to the command line.
:e sample.py
Often while editing multiple files, we will want to copy a portion of one file
into another file that we are editing. This is easily done using the usual yank
and paste commands we used earlier.
It is also possible to insert an entire file into one we are editing. To see
this in action, let us end our vim session and start a new one with just a
single file.
vim sample.py
The :r command inserts the specified file below the cursor position.
Like everything else in vim, there are several different ways to save our edited
files. We have already covered the ex command :w but there are some others we
may also find useful. In command mode, 'zz' will save the current file and exit
vim. Likewise, the ex command :wq will combine the :w and :q commands into one
that will both save the file and exit.
The :w command may also specify an optional filename. This acts like save as.