VI/VIM Text Editors
VI/VIM Overview
• VI is an interactive text-based editor which can be used to
create and/or change files, it is the standard in UNIX systems
for several decades.
• VI is hard to master but once mastered, it becomes a very
powerful and quick editing tool.
• The Linux version is called VIM (VI Improved) which makes
editing easier as it contains some in-editor navigation and
editing tools which make the user’s life easier.
• Some alternatives to VI/VIM are:
 EMACS
 Pico
 Nano
 gedit
The VI Command
• Once started, VI will fill the entire terminal screen until exited
and returned to the shell prompt.
• VI is old, it originated from a predecessor called “ed” which
was used on printer terminals hence its commands and
shortcuts are different than the keyboard shortcuts we know
in modern systems, such as CTRL-[letter].
• Starting VI:
 vi [filename(s)] – enters the editor while editing the file(s) specified.
 view [filename(s)] – opens the file(s) in read-only mode.
 vi –r [filename] – recovers a crashed file.
VI Modes
• VI has three work modes:
 Insert mode – used for adding / editing text; hit ESC in insert mode to
return to command mode.
 Command mode – this is the default mode that VI enters once it is
executed; the keys we hit will not be displayed on screen but this
mode does allow us to use VI’s commands, which are short sequences
of characters and key-combinations.
 Last-line mode – displays a cursor in the bottom line of VI, allowing us
to see the commands we type in; hit <CR> in order to return to
command mode.
• Note that in order to move from Insert mode to Last-line
mode, we must first pass through Command mode.
Insert Mode
• In order to append or insert new text we can use one of the
following options:
 a – Append text after cursor.
 A – Append text at the end of the current line.
 i – Insert text before the cursor.
 I – Insert text at the beginning of the line.
 o – Open a new line below the cursor.
 O – Open a new line above the cursor.
• There are many ways to achieve the same goal so there is no
need to remember all of the shortcuts.
Cursor Movement
• VIM supports moving the cursor around with the keyboard
arrows; VI only allows arrow navigation in newer versions.
• We can use the following alternatives to move around:
 0 – go to the beginning of the line.
 $ - go to the end of the line.
 [n]G – jump to line number [n].
 G – jump to last line in the file.
 gg – jump to the first line in the file.
 b – jump to the beginning of the word.
 e – jump to the end of the word.
 w – jump to the beginning of the next word.
 ^L – refresh the screen
• These are just some of the basic controls, there are many
more.
Command Mode
• Pressing ESC returns VI from insert to command mode.
• Basic command structure in VI:
 [count][command][scope]
• Let’s break down the command 10dw for example:
 10 – this is the count of objects we wish the command to affect.
 d – “d” refers to delete.
 w – “w” is the scope, in this case – words.
• The above command will delete 10 words, beginning at the
current location of the cursor.
Delete Commands
• x – delete 1 character right of the cursor.
• X – delete 1 character left of the cursor.
• dd – delete the entire current line
• dw – deletes to the beginning of the next word
• d3l – deletes the next 3 characters. (l is lower-case L)
• d0 – deletes to the beginning of the line.
• d$ - deletes to the end of the line.
• d1G – deletes to the beginning of the file.
• dG – deletes to the end of the file.
• D - deletes to the end of the line.
• Note that “d” commands delete to a buffer, as long as we
have not quit VI we can always use undo/redo.
Change Commands
• r – replace (overwrite) under the cursor.
• R – enter Replace mode (overwrites any existing text)
• cc – changes a complete line.
• cw – changes to the beginning of the next word.
• c3l – changes the next 3 characters.
• c0 – changes to the beginning of the line.
• c$ – changes to the end of the line.
• c1G – changes to the beginning of the file.
• cG – changes to the end of the file.
• C – changes to the end of the line.
Cut, Copy & Paste Commands
• y – yanks (copies) text to buffer
• yy - yanks a complete line
• yw – yanks to the beginning of the next word
• y0 – yanks to the beginning of the line
• y$ – yanks to the end of the line
• The “d” commands are used to cut text.
• p – paste everything in the buffer after the cursor location.
• P – paste everything in the buffer before the cursor.
Misc Commands
• u – undo last change
• J – join current and next line
• . – repeat last command
• ~ - invert letter case and move on character to the right.
• ZZ – Save changes and exit VI
Last-Line Mode
• Search in VI:
 /[pattern] – search forward for pattern matches
 ?[pattern] – search backwards for pattern matches
 n – repeat last search
 N – repeat last search in the opposite direction
• Search & Replace:
 :s/search-string/replace-with-string/g – this command will search for
“search-string” throughout the file and replace it with “replace-with-
string”.
 :10,20s/search-string/replace-with-string/g – same as above but the
search will be performed only between lines 10 and 20.
 :s/search-string/replace-with-string/gc – this command will search
for “search-string” throughout the file and replace it with “replace-
with-string” but ask for confirmation before each replacement.
File Commands
• :r [filename] – read the file and place its contents under the
cursor.
• :w – write (saves changes).
• :w! – override write-protection mode and save changes.
• :w [filename] – save into a file with the specific name.
• :wq – save changes and quit VI
• :x – save changes and quit VI
• :q – quit without saving
• :q! – force quit without saving
• :![command] – execute a shell command
• :r![command] – execute a shell command and place its output in the file
being edited.
VI Options
• VI has many options that control its looks and behaviour.
• For the full list of options, type: “:set all”
• To enable an option, such as line numbering for example, use:
 :set number
• To disable an options, use:
 :set nonumber
• The above rule is true for any one of VI’s options.

07 vi text_editor

  • 1.
  • 2.
    VI/VIM Overview • VIis an interactive text-based editor which can be used to create and/or change files, it is the standard in UNIX systems for several decades. • VI is hard to master but once mastered, it becomes a very powerful and quick editing tool. • The Linux version is called VIM (VI Improved) which makes editing easier as it contains some in-editor navigation and editing tools which make the user’s life easier. • Some alternatives to VI/VIM are:  EMACS  Pico  Nano  gedit
  • 3.
    The VI Command •Once started, VI will fill the entire terminal screen until exited and returned to the shell prompt. • VI is old, it originated from a predecessor called “ed” which was used on printer terminals hence its commands and shortcuts are different than the keyboard shortcuts we know in modern systems, such as CTRL-[letter]. • Starting VI:  vi [filename(s)] – enters the editor while editing the file(s) specified.  view [filename(s)] – opens the file(s) in read-only mode.  vi –r [filename] – recovers a crashed file.
  • 4.
    VI Modes • VIhas three work modes:  Insert mode – used for adding / editing text; hit ESC in insert mode to return to command mode.  Command mode – this is the default mode that VI enters once it is executed; the keys we hit will not be displayed on screen but this mode does allow us to use VI’s commands, which are short sequences of characters and key-combinations.  Last-line mode – displays a cursor in the bottom line of VI, allowing us to see the commands we type in; hit <CR> in order to return to command mode. • Note that in order to move from Insert mode to Last-line mode, we must first pass through Command mode.
  • 5.
    Insert Mode • Inorder to append or insert new text we can use one of the following options:  a – Append text after cursor.  A – Append text at the end of the current line.  i – Insert text before the cursor.  I – Insert text at the beginning of the line.  o – Open a new line below the cursor.  O – Open a new line above the cursor. • There are many ways to achieve the same goal so there is no need to remember all of the shortcuts.
  • 6.
    Cursor Movement • VIMsupports moving the cursor around with the keyboard arrows; VI only allows arrow navigation in newer versions. • We can use the following alternatives to move around:  0 – go to the beginning of the line.  $ - go to the end of the line.  [n]G – jump to line number [n].  G – jump to last line in the file.  gg – jump to the first line in the file.  b – jump to the beginning of the word.  e – jump to the end of the word.  w – jump to the beginning of the next word.  ^L – refresh the screen • These are just some of the basic controls, there are many more.
  • 7.
    Command Mode • PressingESC returns VI from insert to command mode. • Basic command structure in VI:  [count][command][scope] • Let’s break down the command 10dw for example:  10 – this is the count of objects we wish the command to affect.  d – “d” refers to delete.  w – “w” is the scope, in this case – words. • The above command will delete 10 words, beginning at the current location of the cursor.
  • 8.
    Delete Commands • x– delete 1 character right of the cursor. • X – delete 1 character left of the cursor. • dd – delete the entire current line • dw – deletes to the beginning of the next word • d3l – deletes the next 3 characters. (l is lower-case L) • d0 – deletes to the beginning of the line. • d$ - deletes to the end of the line. • d1G – deletes to the beginning of the file. • dG – deletes to the end of the file. • D - deletes to the end of the line. • Note that “d” commands delete to a buffer, as long as we have not quit VI we can always use undo/redo.
  • 9.
    Change Commands • r– replace (overwrite) under the cursor. • R – enter Replace mode (overwrites any existing text) • cc – changes a complete line. • cw – changes to the beginning of the next word. • c3l – changes the next 3 characters. • c0 – changes to the beginning of the line. • c$ – changes to the end of the line. • c1G – changes to the beginning of the file. • cG – changes to the end of the file. • C – changes to the end of the line.
  • 10.
    Cut, Copy &Paste Commands • y – yanks (copies) text to buffer • yy - yanks a complete line • yw – yanks to the beginning of the next word • y0 – yanks to the beginning of the line • y$ – yanks to the end of the line • The “d” commands are used to cut text. • p – paste everything in the buffer after the cursor location. • P – paste everything in the buffer before the cursor.
  • 11.
    Misc Commands • u– undo last change • J – join current and next line • . – repeat last command • ~ - invert letter case and move on character to the right. • ZZ – Save changes and exit VI
  • 12.
    Last-Line Mode • Searchin VI:  /[pattern] – search forward for pattern matches  ?[pattern] – search backwards for pattern matches  n – repeat last search  N – repeat last search in the opposite direction • Search & Replace:  :s/search-string/replace-with-string/g – this command will search for “search-string” throughout the file and replace it with “replace-with- string”.  :10,20s/search-string/replace-with-string/g – same as above but the search will be performed only between lines 10 and 20.  :s/search-string/replace-with-string/gc – this command will search for “search-string” throughout the file and replace it with “replace- with-string” but ask for confirmation before each replacement.
  • 13.
    File Commands • :r[filename] – read the file and place its contents under the cursor. • :w – write (saves changes). • :w! – override write-protection mode and save changes. • :w [filename] – save into a file with the specific name. • :wq – save changes and quit VI • :x – save changes and quit VI • :q – quit without saving • :q! – force quit without saving • :![command] – execute a shell command • :r![command] – execute a shell command and place its output in the file being edited.
  • 14.
    VI Options • VIhas many options that control its looks and behaviour. • For the full list of options, type: “:set all” • To enable an option, such as line numbering for example, use:  :set number • To disable an options, use:  :set nonumber • The above rule is true for any one of VI’s options.

Editor's Notes

  • #3 VI and Emacs are the oldest - Emacs users vs VI users Learn at least one of them
  • #4 Switch ‘man vi’