Linux Fundamentals
Lesson 5 vim: An Advanced Text
Editor
Lesson Objectives
In this lesson, you will learn:
▪ Introduction to Vim
▪ Modes of Vim
▪ Opening and Editing files in Vim
© 2018 Capgemini. All rights reserved. 2
Introducing vim
▪ Newer version of vi, the standard Unix text editor.
▪ Executing vi runs vim by default.
gvim: Graphical version of vim.
• Applications + Programming -> Vi iMproved
• Provided by vim-X11 package.
▪ Advantages:
• Speed: Do more with fewer keystrokes.
• Simplicity: No dependence on mouse/GUI.
• Availability: Included with most Unix-like Operating systems.
© 2018 Capgemini. All rights reserved. 3
vim: A Modal Editor
Keystroke behavior is dependent upon vim's “mode”.
Three main modes:
• Command Mode (default): Move cursor, cut/paste text, change mode.
• Insert Mode: Modify text.
• Ex Mode: Save, quit, etc.
Esc exits current mode.
© 2018 Capgemini. All rights reserved. 4
Modes of Vim
▪ Three Modes of Vi Editor are:
Command
i, I, a, A, Mode
r, R, o, O, :
s, S
<Enter>
sh
<Esc>
<ctrl d>
or exit
Input ex
Mode Mode
© 2018 Capgemini. All rights reserved. 5
vim Basics
To use vim, you must at least be able to
Open a file.
Modify a file (insert mode).
Save a file (ex mode).
© 2018 Capgemini. All rights reserved. 6
Opening a file in vim
To start vi:
$vim filename.
If the file exists, the file is opened and the contents are displayed.
If the file does not exist, vi creates it when the edits are saved for the first
time.
© 2018 Capgemini. All rights reserved. 7
Modifying a File
Insert Mode:
When file is opened, press i to input text.
Many other options exist
A - append to end of line
I - insert at beginning of line
o - insert new a line (below)
O - insert new line (above)
© 2018 Capgemini. All rights reserved. 8
Saving a File and Exiting vim
Ex Mode:
▪ Enter into Ex Mode with ‘:’.
Creates a command prompt at bottom-left of screen.
Common write/quit commands:
▪ :w - writes (saves) the file to disk.
▪ :wq - writes and quits.
▪ :q! - quits, even if changes are lost.
© 2018 Capgemini. All rights reserved. 9
Manipulating Text Command Mode
Change (replace) Delete (cut) Yank (copy)
Line cc dd yy
Letter cl dl yl
Word cw dw yw
© 2018 Capgemini. All rights reserved. 10
Summary
In this lesson you have learnt
▪ Introduction to Vim
▪ Modes of Vim
▪ Opening and Editing files in Vim
© 2018 Capgemini. All rights reserved. 11
Review Questions
Question 1: What command is used to copy the lines in vi editor?
Question 2: ____ is used to move from command mode to execute
mode.
© 2018 Capgemini. All rights reserved. 12