2018-11-14 18:09:25 +00:00
|
|
|
---
|
|
|
|
layout: page
|
|
|
|
author: Alessandro Luini
|
|
|
|
category-page: advanced
|
|
|
|
category-title: Advanced commands
|
2018-11-14 21:15:38 +00:00
|
|
|
tags: advanced text editor hell quit help escape sos
|
2018-11-16 12:47:48 +00:00
|
|
|
title: vi
|
|
|
|
|
2018-11-14 18:09:25 +00:00
|
|
|
---
|
2018-11-14 21:15:38 +00:00
|
|
|
The default editor that comes with the UNIX operating system is called
|
2018-11-16 12:47:48 +00:00
|
|
|
<code>vi</code> (visual editor).<br>
|
2018-11-14 18:09:25 +00:00
|
|
|
|
2018-11-14 21:15:38 +00:00
|
|
|
The UNIX vi editor is a full screen editor and has two modes of operation:
|
2018-11-16 12:47:48 +00:00
|
|
|
|
2018-11-14 21:15:38 +00:00
|
|
|
<ol>
|
|
|
|
<li>Command mode commands which cause action to be taken on the file</li>
|
|
|
|
<li>Insert mode in which entered text is inserted into the file.</li>
|
|
|
|
</ol>
|
|
|
|
|
|
|
|
In the command mode, every character typed is a command that does
|
|
|
|
something to the text file being edited; a character typed in the
|
|
|
|
command mode may even cause the vi editor to enter the insert mode.<br>
|
|
|
|
|
|
|
|
In the insert mode, every character typed is added to the text in the
|
|
|
|
file; pressing the {Esc} (Escape) key turns off the Insert mode.<br>
|
|
|
|
|
|
|
|
While there are a number of vi commands, just a handful of these
|
|
|
|
is usually sufficient for beginning vi users. To assist such users,
|
|
|
|
this Web page contains a sampling of basic vi commands. The most
|
|
|
|
basic and useful commands are marked with an asterisk (* or star)
|
|
|
|
in the tables below. With practice, these commands should become
|
|
|
|
automatic.<br>
|
|
|
|
|
|
|
|
Both UNIX and vi are <b>case-sensitive</b>. Be sure not to use a
|
|
|
|
capital letter in place of a lowercase letter; the
|
|
|
|
results will not be what you expect.<br>
|
|
|
|
|
|
|
|
<h3>How to start <code>vi</code></h3>
|
|
|
|
To use vi on a file, type in vi filename. If the file named
|
|
|
|
filename exists, then the first page (or screen) of the file
|
|
|
|
will be displayed; if the file does not exist, then an empty
|
|
|
|
file and screen are created into which you may enter text.</p>
|
|
|
|
|
|
|
|
<pre>
|
|
|
|
vi filename.txt
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
<h3>How to exit <code>vi</code></h3>
|
|
|
|
|
|
|
|
Usually the new or modified file is saved when you leave vi.
|
|
|
|
However, it is also possible to quit vi without saving the file.<br>
|
|
|
|
|
|
|
|
The cursor moves to bottom of screen whenever a colon (:) is
|
|
|
|
typed. This type of command is completed by hitting the
|
2018-11-18 17:39:51 +00:00
|
|
|
<Return> (or <Enter>) key.<br>
|
2018-11-14 21:15:38 +00:00
|
|
|
|
|
|
|
<pre>
|
|
|
|
:q
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
Quit vi without saving
|
|
|
|
|
|
|
|
<pre>
|
|
|
|
:x
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
Quit vi, writing out modified file to
|
|
|
|
file named in original invocationt modified file to file named in origin.
|
|
|
|
|
|
|
|
<pre>
|
|
|
|
:wq
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
Quit vi, writing out modified file to file named in original invocation.
|
|
|
|
|
|
|
|
<pre>
|
|
|
|
:q!
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
Quit vi even though latest changes have not been saved for this vi call.
|
|
|
|
|
|
|
|
|
|
|
|
<h3>Recover from a crash</h3>
|
|
|
|
|
|
|
|
Open vi using the <code>-r</code> flag to recover a file that was being edited when a crash
|
|
|
|
happened.
|