vi Primer - rharmonson/richtech GitHub Wiki

#vi Primer

vi /ˈviːˈaɪ/ is a screen-oriented text editor originally created for the Unix operating system. The portable subset of the behavior of vi and programs based on it, and the ex editor language supported within these programs, is described by (and thus standardized by) the Single Unix Specification and POSIX.

source: https://en.wikipedia.org/wiki/Vi

##Purpose vi is a text editor found on most Unix-like operating systems. Alternatively, vim or vi improved is used by many distributions as a replacement. Basic operations remains the same for both vi and vim--CentOS uses vim. The intent of this guide or primer is to provide the most basic use to new Linux users. At the conclusion of this article, additional references will be provided.

##Conventions I use < and > as a convention and for clarity to reference key inputs in the following paragraphs. For brevity, I may use a pair of brackets for multiple single key inputs, i.e. <:wq!> versus <:><!>.

##Premise As a text editor, vi's premise is it is faster to use character input versus use of a text menu or a GUI and mouse. As stated, previously, vi is typically installed by default, thus some familiarity with vi may be needed for initial configuration of a host prior to installing your preferred text editor. With that being said, I am not purporting it is the best or right text editor for you. With practice, it does become easier. Below is a wonderful comic from xkcd that accurately describes Linux users and their passion for their editor. The last "butterfly' editor is, well, total nonsense. It is still under development by me!

butterfly

source: http://xkcd.com/378/

##VI Operational Modes vi has two modes of operation, command and insert text. Command is the default and permits the user to enter one or more characters to execute the desired action or command. For example, key will move the cursor position down one line. To change from command to insert text mode use or the insert key. Insert text mode permits writing text. While in command mode, colon commands can be executed by using <:> followed by the colon command. For example to write a file, enter <:w>. In addition, you may execute multiple commands like <:wq> to write file then quit vi or <q!> to quit vi without saving changes.

###Changing Modes Default mode for vi is command mode. Enter insert text mode using either or and escape back to command mode using .

Input Output
i insert text mode
Insert insert text mode
esc exit insert mode (into command mode)
: colon commands

##Open - Exit To begin, you must either open or create a text file. Both are achieved by executing the following:

[root@www ~]# vi myfile.txt

At execution, vi will look for the file myfile.txt. If the file exists, it is opened for editing. If the file does not exist, a new file is created in memory and vi displays '[New File].' To begin, hit the key and begin adding text. Note vi does not use file extensions so the use of '.txt' or '.sh' or '.mydopetextfile' is a matter of personal preference. When done editing the text file, you use colon commands to either write then quit using <:wq> or quite <:q> then respond to the prompt to save. Alternatively, you can quit! <:q!> or exit <:e> to the text file without saving changes.

Colon File Commands

Input Output
:w write file
:q quit if unsaved changes, prompt
:q! quit and if unsaved changes, no prompt
:e exit
:wq write file then quit
:o open file

##Navigation When editing a document, you will need to navigate. While in command and text mode, page up, page down, arrow keys, home, and end keys work as expected. No surprises there, but as you advance further in your use and exploration of vi, you will find yourself less reliant on aforementioned keys for they are inefficient.

###Line navigation

Input Output
k line up
j line down
l right side
h left side

##Screen navigation

Input Output
H first line of current screen
M middle line of current screen
L last line of current screen
ctrl+f forward one full screen
ctrl+b backwards one full screen
ctrl+d down a half screen
ctrl+u up one half screen

##Word navigation

Input Output
e end of the current word
E end of the current WORD
b before the current word
B before the current WORD
w next word
W next WORD

Definition:

  • WORD consists of a sequence of non-blank characters, separated with white space, e.g. 192.168.1.111 = single WORD
  • word consists of a sequence of letters, digits and underscores, e.g. 192.168.1.111 = seven words.

##File navigation

Input Output
g beginning of file
G end of file
N% Nth percentage line of file
NG Nth line of file

##Editing Editing a text file encompasses all the tasks necessary to add, delete, copy, and paste text. Unless noted otherwise, you will need to be in command mode.

###Inserting Text

Input Output
i insert before cursor
I insert before line
a append after cursor
A append after line
o open a new line after current line
O open a new line before current line
r replace one character
R replace many characters

###Deleting Text Most deletion commands are performed by typing followed by a motion. For example, deletes a word. A few other deletes are:

Input Output
x delete character to the right of cursor
nx delete n characters starting with current; omitting n deletes current character only
X delete character to the left of cursor
nX delete previous n characters; omitting n deletes previous character only
D delete to the end of the line
d$ deletes from the cursor to the end of the line
dd or :d delete current line
ndw delete the next n words starting with current
ndb delete the previous n words starting with current
ndd delete n lines beginning with the current line
:n,md delete lines n through m
"1pu.u. scrolls through the delete buffer until the desired delete is retrieved (repeat u.)

###Yanking Text (Copy) Like deletion, almost all yank commands are performed by typing followed by a motion. For example, <y$> yanks to the end of the line.

Input Output
y$ yank to end of line
yy yank the current line
:y yank the current line

###Putting Text (Paste)

Input Output
p put after the position or after the line
P put before the poition or before the line

###Search & Replace No vi primer is complete without text search and replace. Search

Input Output
/string search forward for string
?string search back for string
n search for next instance of string
N search for previous instance of string
, repeats, in reverse direction, last / or ? search command

Replace The search and replace function is accomplished with the <:s> command. It is commonly used in combination with ranges or the <:g> command (below).

Input Output
:s/pattern/string/flags replace pattern with string according to flags
g (flag) replace all occurrences of pattern
c (flag) confirm replace
& repeat last :s command

##Miscellaneous The following are must haves or just cool.

Input Output
:read !program replace line with the output from program, e.g. :read ping -c 5 www.google.com.
~ toggle upper or lower case
J join lines
nJ joins the next n lines together
. repeat last command
u undo last change; try it with with <.>
U undo all changes to line
:N or :E you can open up a new split-screen window in (n)vi and then use ^w to switch between the two

##Done! I hope you found this article to be useful. It should be more than sufficient for casual vi use, but if you need further information and want to visit more advance topics such as buffers or regular expressions, see the references provided below.

Further Reading

vicheatsheet

  1. Visual Keyboard Reference
  2. vi Complete Key Binding List
  3. Mapping Keys in vim
  4. Showing ASCII
  5. Comprehensive vi Tutorial
  6. Text Editers Commands / Comparison
  7. vim & Regular Expressions
  8. vim & Buffers
⚠️ **GitHub.com Fallback** ⚠️