VIM - an Advance Text Editor
Overview
Introducing vim
- The newer version of
vitext editor is calledvim(Vi IMproved). vimis 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
gvimis 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 ModeDefault Mode of vim. Move cursor, cut/copy/paste text, change modeInsert ModeInsert/Modify text-
Ex ModeSave, Save As, Quit, etc EscExit current modeEscEscAlways 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:
# Open a file named /etc/passwd in vim
[mitesh@Matrix ~]$ vim /etc/passwd
# Creates a file with vim
[mitesh@Matrix ~]$ vim /tmp/file- 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
//dogsearch downwards in the file for the string dog??mousesearch upwards in the file for the string mousen/Nnext/previous match
Search/Replace Operation
viandvimcan perform search and replace operations, much like thesedcommand.- The primary difference between the
sedandvimis that -- Absent an Address,
sedworks on the entire file - Absent an Address,
viandvimworks only on the current line, the line on which the cursor resides
- Absent an Address,
- The default substitution delimiter is the
/character. - However,
vitreats whatever character follows the's'command as the delimiter.
:%s/\/dev\/hda/\/dev\/sda/gi
:%s'/dev/hda'/dev/sda'giAddress Range
- Use
1,$or%` for every line - Use
x,yfor specific no of line
Examples:
:%s/cat/dog/gi
:1,$s/cat/dog/gi
:1,5s/cat/dog/giPut/Paste (Command Mode)
- Use p or P to put (paste) copied or deleted data.
For Line Oriented Data (Line or Paragraph)
pPuts the data below the current linePPuts the data above the current line
For Character Oriented Data (Letter Word Sentence)
pPuts the data after the cursorPPuts the data below the cursor
Undo (Command Mode)
uUndo most recent changes-
UUndo all the changes to the current line since the cursor landed on the line Ctrl+rRedo 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:
vStart character oriented visual modeVStart line oriented visual modeCtrl+vstart 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
vimprovides 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
-ooptions
vim -o .bashrc .bash_profile- This will open both files Horizontally,
.bashrcin the top of the window..bash_profilein the bottom of the window.
Configuring vi and vim
- Dozens of configuration items exist for
viandvim. -
To examine current configuration, run
:setList a small number of important configuration items.:set allList all the configuration items:help option-listComplete options lists
Common Configuration Items
:set number
:set showmatch
:set autoindent
:set ignorecase
:set wrapmargin=15
:set textwidth=65 (vim only)Show line Numbers :set number
:set number :set nu :se nu
:set nonumber :set nonu :se nonuHighlight The Matching Brackets :set showmatch
:set showmatch :set sm :se sm
:set noshowmatch :set nosm :se nosmCause New Line To Inherit Independent Level Of The Previous Line :set autoindent
:set autoindent :set ai :se ai
:set noautoindent :set noai :se noaiSearch Case-Insensitive :set ignorecase
:set ignorecase :set ic :se ic
:set noignorecase :set noic :se noicCause Text To Wrap When It Reaches 15 Characters From The Right Margin :set wrapmargin=15
:set wrapmargin=15 :set wm=15 :se wm=15
:set wrapmargin=0 :set wm=0 :se wm=0Cause Text To Wrap When The Text Exceeds 65 Characters :set textwidth=65
:set textwidth=65
:set textwidth=0NOTE!: vim only
Create Folds As Per Defined Methods :set foldmethod
# Methods
1. expr 3. manual 5. syntax
2. diff 4. marker 6. indentList Of Fold Commands :help fold
:set foldmethod=indent :set fdm=indent
:set foldmethod=syntax :set fdm=syntaxConfiguring Permanently
- Save all your changes in following file
~/.vimrc
~/.exrcLearning More
- Built-in vi/vim help
:help
:help topicvimtutorcommand 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.