VIM - an Advance Text Editor
Overview
Introducing vim
- The newer version of
vi
text editor is calledvim
(Vi IMproved). vim
is the standard Linux and UNIX text editor.- The default text editor is
vi
, unless explicitly changed by the system administrator.
Advantages
- Speed: Do more with fewer keystrokes
- Simplicity: Not Dependence on Mouse/GUI
- Availability: Included with most UNIX like OSes
Disadvantages
- Difficulty: Difficult for beginners.
- Patience and persistence will lead to many great benefits on your way to becoming a Linux user.
Graphical vim
gvim
is the Graphical version of vim.- Application -> Programming -> Vi IMproved
- Provided by vim-X11 package
A Model Text Editor vim
- Keystroke behavior is dependent upon vim’s mode.
Three Main Modes of vim
Command Mode
Default Mode of vim. Move cursor, cut/copy/paste text, change modeInsert Mode
Insert/Modify text-
Ex Mode
Save, Save As, Quit, etc Esc
Exit current modeEscEsc
Always returns to command mode
Basics of vim
- Open/Create file
- Modify file (Insert Mode)
- Save file (Ex Mode)
Opening a file in vim
vim filename
- If the file exists, the file is opened and the contents are displayed
- If the file does not exist, vim creates it when the edits are saved for the first time
Examples:
- This will create the file named /tmp/file, once you save it.
NOTE!: Until you save the file, all changes are kept in a temporary file.
The temporary file, in this case, is /tmp/.file.swp
(vim add a dot to the file name and adds the .swp extension).
- Once you save and quits the vim, it will create this file, write your changes to it and remove the temporary file.
- If the machine should crash while you are writing a file, this temporary file should still exist, when the machine is working again, and it should contain your changes.
Insert Mode
Many commands will take you into insert mode.
i
: Insert before the cursor-
a
: Append after the cursor I
: Insert at beginning of the line-
A
: Append at end of the line o
: Open a new line below the current lineO
: Open a new line above the current line
NOTE!: The letters i, a, I, A, o and O will not appears in your document, but all other characters that you type will appears, until you exit insert mode. To exit insert mode, hit the Esc key.
Ex Mode
- Enter Ex Mode with
:
-
Creates a command prompt at bottom-left of the screen
- Common write/quit commands
/-----------------------------------------------------------------------\ | :q | quit | | :q! | quits, even if changes are lost | | | | | :w | writes (saves) the file to disk | | :wq | writes and quits | | | | | :w filename | save as funactionality | \-----------------------------------------------------------------------/
Command Mode
- Default mode of vim.
- Keys describe Movements and Text manipulation Commands.
- Commands repeat when preceded by a number:
Examples:
- Right Arrow moves right one character
- 5+Right Arrow moves right five character
Moving Around (Command Mode)
^ k < h l > j v
/---------------------------------------------------------------\ | Moves by characters | Arrow Keys | | | h j k l | | | Left Down Up Right | | | | | | | | Moves by words | w b | | | | | Moves by sentence | ( ) | | | | | Moves by paragraph | { } | | | | | Jump to line x | xG :x | | Jump to end | G | \---------------------------------------------------------------/
Manipulating Text (Command Mode)
/-------------------------------------------------------------------------------\ | | Change | Delete | Yank | | | replace | cut | copy | --------------------------------------------------------------------------------- | | | Line | cc | dd | yy | | Letter | cl | dl | yl | | Word | cw | dw | yw | | | | Sentence Above (Up) | c( | d( | y( | | Sentence Below (Down) | c) | d) | y) | | | | Paragraph Above (Up) | c{ | d{ | y{ | | Paragraph Below (Down) | c} | d} | y} | \-------------------------------------------------------------------------------/
Search and Replace (Command Mode)
Search as in less
/
/dog
search downwards in the file for the string dog?
?mouse
search upwards in the file for the string mousen/N
next/previous match
Search/Replace Operation
vi
andvim
can perform search and replace operations, much like thesed
command.- The primary difference between the
sed
andvim
is that -- Absent an Address,
sed
works on the entire file - Absent an Address,
vi
andvim
works only on the current line, the line on which the cursor resides
- Absent an Address,
- The default substitution delimiter is the
/
character. - However,
vi
treats whatever character follows the's'
command as the delimiter.
Address Range
- Use
1,$
or
%` for every line - Use
x,y
for specific no of line
Examples:
Put/Paste (Command Mode)
- Use p or P to put (paste) copied or deleted data.
For Line Oriented Data (Line or Paragraph)
p
Puts the data below the current lineP
Puts the data above the current line
For Character Oriented Data (Letter Word Sentence)
p
Puts the data after the cursorP
Puts the data below the cursor
Undo (Command Mode)
u
Undo most recent changes-
U
Undo all the changes to the current line since the cursor landed on the line Ctrl+r
Redo last ‘undone’ changes
Visual Mode
- It is possible to highlight characters, lines, or even block and then take actions on them (change, delete, yank etc)
-
To begin visual mode, thus highlighting text, type:
v
Start character oriented visual modeV
Start line oriented visual modeCtrl+v
start block oriented visual mode
NOTE!: Visual mode is activated with Mouse in gvim
.
Use Arrow Keys or Shortcuts Keys of vim such as h j k l w b ( ) { } to highlight the text.
Using Multiple Windows
- The
vim
provides many features beyond those ofvi
. - Multiple Documents can be viewed in a single vim screen.
/---------------------------------------------------------------------------------------\ | | | Ctrl+w n | Creates New Window With Empty Buffer (Horizontally) | | | | Ctrl+w s | Split The Screen Horizontally (Open Same File) | | Ctrl+w v | Split The Screen Vertically (Open Same File) | | | | Ctrl+w +/- | Increse/Decrese Size Of The Window | | Ctrl+w Arrow | Moves Between The Windows (Works With h j k l) | | | \---------------------------------------------------------------------------------------/
NOTE!: The standard open command (such as :e
filename) can be used to change the file being edited in a window.
- To start vim in windowing mode use
-o
options
- This will open both files Horizontally,
.bashrc
in the top of the window..bash_profile
in the bottom of the window.
Configuring vi and vim
- Dozens of configuration items exist for
vi
andvim
. -
To examine current configuration, run
:set
List a small number of important configuration items.:set all
List all the configuration items:help option-list
Complete options lists
Common Configuration Items
Show line Numbers :set number
Highlight The Matching Brackets :set showmatch
Cause New Line To Inherit Independent Level Of The Previous Line :set autoindent
Search Case-Insensitive :set ignorecase
Cause Text To Wrap When It Reaches 15 Characters From The Right Margin :set wrapmargin=15
Cause Text To Wrap When The Text Exceeds 65 Characters :set textwidth=65
NOTE!: vim only
Create Folds As Per Defined Methods :set foldmethod
List Of Fold Commands :help fold
Configuring Permanently
- Save all your changes in following file
Learning More
- Built-in vi/vim help
vimtutor
command is a great way to explore the functions of vim and get practice.
NOTE!: Use :q
to exit help
Newsletter
Get updated when I create new content.
Unsubscribe whenever. Never any spam.