Nano VI Emacs VI VI (File) Vim (File) VI View (File) Vim View
Nano VI Emacs VI VI (File) Vim (File) VI View (File) Vim View
While nano is great for simple edits, vi and emacs have more advanced and powerful features.
There is a learning curve to using these editors as they are not exactly intuitive. It will require a bit
of a time investment to become proficient. Let's start by looking at vi .
vi [file]
- Edit file.
vim [file]
view [file] - Starts vim in read-only mode. Use view when you want to examine a file but not
make any changes.
Vim stands for "Vi IMproved." It is compatible with the commands found in vi . Some of the
additional features of vim include syntax highlighting, the ability to edit files over the network,
multi-level undo/redo, and screen splitting. On many Linux distributions when you invoke vi , you
are actually running vim .
One advantage of knowing vi is that vi or a vi variant like vim is always available on the
system. Another advantage is that once you learn the key mappings for vi you can apply them to
other commands like man , more , less , view , and even your shell.
Vi Modes
Command Mode
Vi has the concept of modes. You are always working in one of three modes: command mode,
insert mode, or line mode. When vi starts you are placed into command mode. To get back to
command mode at any time hit the escape key ( Esc ). Letters typed while in command mode are
not sent to the file, but are rather interpreted as commands. Command mode allows you to
navigate about the file, perform searches, delete text, copy text, and paste text.
- Up one line.
Note that commands are case sensitive. For example, if you want to move down one line type the
lowercase j . The uppercase J joins lines together. The original vi editor did not employ the use
of arrow keys, However vim does, so you may find that you can use arrow keys on your system.
The advantages of learning the original key bindings are 1) they always work and 2) it's faster
since your hand does not have to leave the home row.
Insert mode
After entering into insert mode, type the desired text. When you are finished, type Esc to return to
command mode.
Line mode
To enter line mode you must start from command mode and then type a colon ( : ) character. If
you are in insert mode, type Esc to get back to command mode and then type a colon for line
mode. Here are some of the most common line mode commands you will want to know.
:w
:w! - Forces the file to be saved even if the write permission is not set. This only works on files
you own.
:q
- Quit. This will only works if there have not been any modifications to the file.
:q!
:wq!
- Write and quit. After modifying a file this command ensures it gets saved and closes vi .
:x
- Same as :wq.
:n
- Positions the cursor at line n . For example, :5 will place the cursor on the fifth line in the file.
:$
:set nu
:set nonu
:help [subcommand]
- Get help. If you want more information on the :w command type :help :w .
Mode
Key
Description
Command
Esc
Insert
i I
a A
Line
Also called command-line mode. Save the file, quit vi, replace text, and
You can repeat commands in vi by preceding them with a number. For instance, if you would
like to move the cursor up 5 lines type 5k . If you would like to insert a piece of text 80 times, type
80i and start entering the text. Once you hit Esc to return to command mode the text you typed
will be repeated 80 times. If you would like to make a line of asterisks, you could type 80i*Esc .
Can you start to see how vi is more powerful than an editor like nano ?
Deleting Text
x
- Delete a character.
- Delete a word. To delete five words, type d5w . The repeating concept in vi shows up in
many places.
dw
dd
D
Changing Text
r
cw
cc
c$
- Change the text from the current position to the end of the line.
- Same as c$ .
y<position>
type y3w .
p
- Yank the <position>. For example, to yank a word type yw . To yank three words
Undo / Redo
u
- Undo.
Ctrl-r
- Redo.
Searching
/<pattern>
?<pattern>
http://www.LinuxTrainingAcademy.com