UNIX-Vim Commands PDF
UNIX-Vim Commands PDF
UNIX-Vim Commands PDF
Vim was made available in 1991 and is a free, open source software. Available both as a command line interface
and as a standalone program with a GUI, Vim is a text editor that is a modal version of the vi editor created for
Unix in the 1970s; Vim stands for vi improved. While it was designed with Unix in mind, versions of it are
available for most operating systems and Vim is also available for Android and iOS smartphones.
While you may be familiar with the concept of a text editor, the modal part may throw you. A modal editor is one
that allows you to edit text in different modes, and in the case of Vim, the mode determines what the
alphanumeric keys on your keyboard do and how Vim editor commands work.
For example, in insert mode, your keyboard behaves normally, so what you type in is what you see, just like with
a standard text editor. However, if you switch to command mode, the letters on your keyboard will allow you use
Vim commands to move within the text. If you play video games that use the left-hand keys on the keyboard to
move your character around, this concept is probably familiar to you.
To open a file using Vim you can use the following command (simply replace filename.css with your actual file
name).
vim filename.css
The idea behind having a modal text editor is that it allows you to write and edit text, including code, without
requiring your hands leave the keyboard. Vim isn't for everyone, and it requires you to learn a variety of Vim editor
commands to get the most out of it. That said, many people who have started using it and become comfortable
with it won't even consider another editor. The image below shows an exampe of what a css file looks like when
viewed using Vim.
This text editor is particularly well-suited for people who are programmers, coders, system administrators or
individuals looking for a streamlined way to edit text. The editor allows you to edit text in multiple windows,
which can be helpful to programmers and editors alike. If you're interested in giving Vim a shot, the following is a
basic explanation of Vim modes and a list of frequently used Vim commands, along with a definition of what
they do.
Vim modes
There are some arguments as to how many modes that Vim has, but the modes you're most likely to use are
command mode and insert mode. These modes will allow you to do just about anything you need, including
creating your document, saving your document and doing advanced editing, including taking advantage of
search and replace functions.
Command mode
This is the default mode that you'll be in once you open Vim. If you're in a different mode and want to go back to
command mode, just hit the Escape key. This mode allows you to use Vim commands and move through your
document. From command mode, you can also use last-line commands, which generally start with the use of a
colon. For example, :w saves your file and :q allows you to exit Vim.
Insert mode
This mode allows you to enter text into your document. You can enter insert mode by pressing the i key. Keep
in mind that to save your document, you'll need to go back to command mode since only text input is allowed in
this mode.
Installing Vim
There are a few ways to install Vim and the one you end up using will sometimes depend on which system you're
using.
According to Vim themselves, install Vim via Git is the simplest and most efficient method. Simply use the
following commands:
If you already have Vim installed but want to update to the latest version, you may need to use additional Git
commands which can be found here.
Install Vim on Ubuntu/Debian:
If you're using Ubuntu or Debian use apt-get to install Vim, like so:
If you want a more advanced set of features on CentOS/Fedora, you'll need to install vim-enhanced , to do this,
run the following command instead:
Vim commands
The following is a list of frequently used commands and what they do. Many of the commands can be made to
repeat by adding a number to the command. This is not an exhaustive list because more advanced commands,
such as how to use multiple buffers, are not included. However, just about all of the basic commands for
opening, editing and saving documents are included as well as commands that enable you to find and replace
text and work with multiple documents.
:help [keyword] - Performs a search of help documentation for whatever keyword you enter
:e [file] - Opens a file, where [file] is the name of the file you want opened
:w [filename] - Allows you to save your file with the name you've defined
:q! - Quit without first saving the file you were working on
# - Where # is the number of a line, this command takes you to the line specified
You also have two options for how to select text. You can either use commands like dd , which deletes a single
line, and yy , which copies a single line, or you can highlight text and then copy it to the unnamed register. The
paste commands work the same whether you've highlighted text or used a command to automatically copy it.
As with movement commands, putting a number in front of the command can increase the number of times a
task is completed. For instance, putting a number in front of yy will increase the number of lines copied, so
5yy will copy five lines.
yy - Copies a line
yw - Copies a word
V - Highlights one line, and movement keys can allow you to highlight additional lines
dw - Deletes a word
D - Deletes everything from where your cursor is to the end of the line
d0 - Deletes everything from where your cursor is to the beginning of the line
dgg - Deletes everything from where your cursor is to the beginning of the file
dG - Deletes everything from where your cursor is to the end of the file
/[keyword] - Searches for text in the document where keyword is whatever keyword, phrase or string of
characters you're looking for
?[keyword] - Searches previous text for your keyword, phrase or character string
n - Searches your text again in whatever direction your last search was
:%s/[pattern]/[replacement]/g - This replaces all occurrences of a pattern without confirming each one
:sp [filename] - Opens a new file and splits your screen horizontally to show more than one buffer
:vsp [filename] - Opens a new file and splits your screen vertically to show more than one buffer
Ctrl + wj - Moves your cursor to the window below the one you're in
Ctrl + wk - Moves your cursor to the window above the one you're in
v - starts visual mode, you can then select a range of text, and run a command
ab - a block with ()
aB - a block with {}
Once you've selected a particular range of text, you can then run a command on that text such as the following:
7. Tab pages
Just like any browser, you can also use tabs within Vim. This makes it incredibly easy to switch between multiple
files while you're making some code changes instead of working in one single file, closing it, and opening a new
one. Below are some useful Vim commands for using tab pages:
:tabedit file - opens a new tab and will take you to edit "file"
#gt - move to a specific tab number (e.g. 2gt takes you to the second tab)
2 Type i to switch into insert mode so that you can start editing the file.
4 Once you're done, press the escape key Esc to get out of insert mode and back to command mode.
Of course, there is so much more you can do with Vim, however as a beginner, the above steps are what a
simple Vim workflow looks like.
Summary
Vim is quite easy to use, it just involves memorizing Vim editor commands and remembering what mode you're
in. If you're used to using keyboard shortcuts like Ctrl + C and Ctrl + S , you shouldn't have too much
difficulty getting used to the way that Vim works. While there is a bit of a breaking in period with the editor, you
don't have to worry too much about accidentally deleting large swathes of text without being able to recover
them since you can use the undo command multiple times.
While not for everyone, functionality like being able to work on more than one document at a time in windowed
screens and the ability to do major editing without a mouse is what makes Vim so popular. You can download
the editor for free, and there are a variety of plugins and extensions that can improve its functionality and add
additional Vim commands.
TAGS tools SHARE
Get started
Comments
Nice summary to get started with Vim, but I have a few corrections for you (so we don't confuse people
trying to use vim for the first time):
Start Vim using vim filename instead of vi . Usually, vi will run vim with vi compatibility mode,
which is less user friendly.
90j would move the cursor down 90 lines, not 9. For that, you would use 9j .
Yanking text does not necessarily go to the clipboard. When you yank normally with yw or similar, it
goes into the "unnamed register", ready to be put with the next p command. To copy to the clipboard
(so that you can paste it in another program), specify the + register as in +yy and paste from the
clipboard with +p (See :help registers).
Deleted text also goes into the unnamed register by default (if you don't specify a register for it). So you
can delete a line with dd and then put it with p .
Replacing text uses regex patterns (see :help pattern). Replace all occurrences of a pattern without
confirming each one ( g for all occurrences in a line):
:%s/pattern/replacement/g
:%s/pattern/replacement/gc