Published on

Text Editors

Authors
  • Name
    Jackson Chen

Vi Editor

Inserting text

insert before cursor, before line   i , I
append after cursor, after line     a , A
open new line after, line before    o , O
replace one char, many chars        r , R

Motion

left, down, up, right                       h , j , k , l
next word, blank delimited word             w , W
beginning of word, of blank delimited word  b , B
end of word, of blank delimited word        e , E
sentence back, forward                      ( , )
paragraph back, forward                     { , }
beginning, end of line                      0 , $
beginning, end of file                      1G , G
line n                                      nG or :n
forward, back to char c                     fc , Fc
forward, back to before char c              tc , Tc
top, middle, bottom of screen               H , M , L

Deleting text

Almost all deletion commands are performed by typing d (lowercase) followed by a motion. For example dw deletes a word. A number can also be prefixed to dd to delete multiple lines

character to right, left x , X
to end of line  D (shift d)  # delete from cursor to the end of line
line        dd
line        :d
3 lines     3dd and hit ENTER
dw          # delete the word that cursor is on
d0          # delete till the begin of the line where cursor is at
:[start],[end] d    Delete lines starting from 3 to 5
d<n>d       Cut and paste number of lines

Yanking text (Copy)

Like deletion, almost all yank commands are performed by typing y followed by a motion. For example y$ yanks to the end of line.

Two other yank commands are:
line                yy
line                :y
copy to end of line y$  (y shift)
copy the word       yw
number of lines     y<n>y   # y3y   yanking 3 lines

Changing text

The change command is a deletion command that leaves the editor in insert mode. It is performed by typing c followed by a motion. For example cw changes a word.

A few other change commands are:
to end of line      C   # capital c
line                cc

Putting text (Paste)

put after position or after line    p (lowercase)
put before position or before line  P (uppercase)

Visual Mode (V)

Press (shift V) capital v to enter visual mode. Visual mode allow you to select texts, block of text to delete or change

c       # change the highlighted text, it will delete the highlighted text and allow to type new text
d       # delete the highlighted text

Registers

Named registers may be specified before any deletion, change, yank, or put command. The general prefix has the form "c where c may be any lower case letter.

For example, "adw deletes a word into register a. It may thereafter be put back into the text with an appropriate put command, for example "ap.

Search for strings

search forward                              /string
search backward                             ?string
search line begin with <test>               /^<text>
    # Example line begin with "netwrok"    /^netowrk        
repeat search in same, reverse direction    n , N

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).

replace pattern with string             :s/pattern /string /flags
flags: all on each line, confirm each   g , c
repeat last :s command                  &

Regular expressions

any single character except newline     . (dot)
zero or more repeats                    *
any character in set                    [...]
any character not in set                [^ ...]
beginning, end of line                  ^ , $
beginning, end of word                  \< , \>
grouping                                \(. . . \)
contents of n th grouping               \n

Counts

Nearly every command may be preceded by a number that specifies how many times it is to be performed. For example 5dw will delete 5 words and 3fe will move the cursor forward to the 3rd occurance of the letter e. Even insertions may be repeated conveniently with this method, say to insert the same line 100 times.

Ranges

Ranges may precede most “colon” commands and cause them to be executed on a line or lines. For example :3,7d would delete lines 3−7.

Ranges are commonly combined with the :s command to perform a replacement on several lines, as with :.,$s/pattern/string/g to make a replacement from the current line to the end of the file.

lines n-m :n ,m
current line :.
last line :$
marker c :’c
all lines :%
all matching lines :g/pattern /

Files

write file (current file if no name given)  :w file
append file (current file if no name given) :w >>file
read file after line                        :r file
read program output                         :r !program
next file                                   :n
previous file                               :p
edit new file                               :e file
replace line with program output            :.!program

Other

toggle upper/lower case                 ~
join lines                              J
repeat last text-changing command       .
undo last change, all changes on line   u , U
go to end of file                       Press Esc, then press Shift + G 
go to beginning of file                 press g twice
go to line number                       :<line number>

How to select the required text

  1. In vi, navigate to the required first character
  2. Press "v" to start the begining of the highlight
  3. Press ">" or "<", left arrow or right arrow to continue select or hightlight the characters
  4. Presss "Delete" key to delete the selected or hightlighted texts