Upload
shiwang-kalkhanda
View
468
Download
0
Embed Size (px)
Citation preview
INTODUCTION TO VIM
• vi → The standard Unix text editor
• vim → vi improved is a powerful text editor
• gvim → Graphical version of vim
• Advantages of Vi / Vim :– Speed : Do more with fewer keystrokes– Simplicity : No dependence on mouse/GUI– Availability : Included with most Unix / Linux Distro
• Disadvantages of Vi/Vim :– Difficult : Steeper learning curve than simpler editors
VI MODES OF WORKING
• Keystroke behavior is dependent upon vim's "mode"
• Three main modes:– Command Mode (default) : Move cursor, cut / copy / paste /
or delete text in file – Insert Mode : Modify text in file– Ex Mode : Save, Quit, Customize etc
• <Esc key > : exits current mode into command mode.
• NOTE : When in doubt press Escape key, you will be back in command mode
OPEN A FILE IN VI / VIM
• To start vi / vim :– # vim filename– # vi +number filename ( open file and put cursor at line number
specified )
• 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
• By default vi has unnamed temporary buffer where file is edited
MODIFY A FILE : INSERT MODE• INSERT MODE → To insert the text in file we should be in Insert
mode– i → inserts the text before cursor location– I → insert at beginning of line– a → inserts text after cursor location– A → insert at end of line– I → insert at beginning of line– o → insert new line (below)– O → insert new line (above)– S → deletes the character under cursor and get into insert
mode
MODIFY A FILE : REPLACING TEXT
• ONE CHARACTER AT A TIME → Replacing text also happens in insert mode as follows :
– r → replace a single character– R → replace multiple char( will be in replace mode )
SAVE & EXIT FILE : EX MODE• Enter Ex Mode by pressing → :
– Creates a command prompt at bottom-left of screen
• Common write/quit commands :
– :q <enter key> → quits the vi only if no changes have been made to the file being edited
– :q! <enter key> → quits the vi without making any changes in file
– :w <enter key> → writes (saves) the file to disk only
– :wq <enter key> → writes the buffer and quits vi
– :ZZ <enter key> → writes the buffer and quits vi
– :x <enter key> → writes and quits
WRITE TO FILE : EX MODE• Common write commands :
– :w <enter key> → writes (saves) the file to disk only
– :wq <enter key> → writes the buffer and quits vi
– :f <filename> → renames current file to filename
– :w <filename> → write the file to path / filename given
– :w>> filename → append current file to the filename given
– :5,10w filename → write lines 5 through 10 to the filename given
– :5,10w>>filename→ append lines 5 through 10 to the filename given
– :r <filename> → read a copy of file into current file at cursor positon
– :e <filename> → opens another file with filename
– :e# <enter key> → switch between the open vi windows ******** (this works after saving the file once on disk using :w )
USING COMMAND MODE
• Default mode of vim
• Keys describe movement and text manipulation commands
• Commands repeat when preceded by a numberCommands repeat when preceded by a number
• Example– Right Arrow : moves right 1 character– 5 followed by Right Arrow : moves right 5 characters
MOVING AROUND : COMMAND MODE
• Move by character : Arrow Keys, h, j, k, l
( Non-arrow keys useful for remote connections to older systems )
MOVING AROUND : COMMAND MODE
• MOVE BY WORD
• w : moves the cursor forward one word
• b : moves cursor back one word
• e : moves cursor to the end of current word
• MOVE BY LINE
• ^ : moves cursor to the beginning of current line
• $ : moves to the end of current line
• MOVE BY SENTENCE
• ( : moves to the beginning of previous sentence
• ) : moves to the end of next sentence
MOVING AROUND : COMMAND MODE
• MOVE BY PARAGRAPH
• { : moves to the beginning of previous paragraph
• } : moves to the end of next paragraph
• MOVE BY SCREEN
• <ctrl>f : moves forward one screen
• <ctrl>b : moves back one screen
• <ctrl>d : moves down half screen
• <ctrl>u : moves up half screen
• H : first line on screen
• M : middle line on the screen
• L : last line on the screen
MOVING AROUND : COMMAND MODE
• MOVE INSIDE WHOLE DOCUMENT
• nG : move to nth line of file (****in command mode*****)
• :n : move to nth line of file (****in ex mode****)
• 1G : first line of the document / file
• G : last line of the file
• 25G : 25th line of the file ( in command mode )
• :25 : 25th line of the file ( in ex mode )
SEARCH : EX MODE
• Same implementation as in less editor
• /<pattern> – search forward in buffer for next occurrence of the pattern of text.
• ?<pattern> – search backwards
• n – Repeats the last search command
• N – Repeats the search command in opposite direction
• We can use regular expression in searches
SUBSTITUTION : EX MODE• By default substitute the first occurrence of text on current line
– : s/file/book/ ( first occurrence of file with book on current line )
– :s/file/book/g ( all the occurrence of file with book on current line )
• Use x,y ranges – :1,5s/cat/dog/ ( first occurrence of cat with dog between
line 1 to line 5 )– :1,5s/cat/dog/g ( all occurrence of cat with dog between line
1 to line 5 )
• Use % for whole file– :%s/cat/dog/ ( substitute first occurrence of cat with dog in
full file )– :%s/cat/dog/g ( substitute all occurrence of cat with dog in
full file )– :%s/cat/dog/gc ( c → prompt before each substitution )
DELETING TEXT : COMMAND MODE
• DELETING SINGLE CHARACTERS– x → Deletes a character at current cursor position– 3x → Deletes the character currently under cursor
followed bye two– Nx → Deletes n-1 characters on right of cursor position– X → Deletes a character to the left of the cursor
• NOTE : Deleting puts the text in unnamed temporary buffer which can be used for paste operation at other place, and thus becomes cut paste *******
DELETING TEXT : COMMAND MODE• DELETING LARGER CHUNKS
– dw → deletes a word (or part of a word) from the cursor to the next space or to the next punctuation.
– db → delete one word backwards
– d$ → deletes the current line from the cursor to the end of the line
– d^ → deletes the current line from the cursor to the beginning of the line
– d0 → same as d^
– d) → delete one sentence forward
– d( → delete one sentence backwards
– dG → delete from current line to the end of file
– dgg → delete from current line to the beginning of file
– dd → deletes the current line *******
– ndw → deletes n words from current cursor position
– ndd → deletes n lines from current line *******
COPY TEXT : COMMAND MODE• COPY / YANKING → yank command puts the text in temp buffer for copy
– yw → yank a word forward
– yb → one word backwards
– y$ → yank the current line from the cursor to the end of the line
– y^ → yank the current line from the cursor to the beginning of the line
– y0 → same as d^
– y) → yank one sentence forward
– y( → yank one sentence backwards
– yG → yank from current line to the end of file
– ygg → yank from current line to the beginning of file
– yy → yank the current line *******
– nyw → yank n words from current cursor position
– nyy → yank n lines from current line *******
– 20yy → yank 20 lines from current line *******
• P → 'PUT' comman for paste operation, place the contents of the unnamed buffer back into the file. ( Buffered content of delete & yank command )
– p → Paste the content below the current line– P → Paste the content above the current line
PASTE TEXT : COMMAND MODE
UNDO CHANGES : COMMAND MODE
• u → undo most recent change
• U → undo all changes to the current line since the cursor landed on the line
• Ctrl-r → redo last "undone" change
USING MULTIPLE “WINDOWS”
• View multiple documents in a single vim screen.– Ctrl-w followed by s → s splits the screen horizontally– Ctrl-w followed by v → v splits the screen vertically– Ctrl-w followed by arrow → Arrow moves between windows
– Ctrl-w followed by q → quit / close open windows
• Ex-mode instructions always affect the current window
• :help windows displays more window commands
CUSTOMIZING VI SESSION• A few common configuration items
– :set all - Display all options– :set - Display current setting of
options– :set number / :set nu - Display line numbers
– ( This can be useful when deleting or substituting selected text as follows )
– :2,10d - Delete line 2 to 10– :1,10s/foo/bar/g - Substitute foo with bar
from line 1 to 10
– :set nonumber / :set nonu - Hide line numbers
CUSTOMIZING VI SESSION• A few common configuration items
– :set all - Display all options– :set - Display current setting of
options– :set autoindent or :set ai - Turn on autoindenting– :set textwidth=65 (vim only)– :set wrapmargin=15 / :set wm=15 - Set wrap margin 15
spaces from right edge of screen
– :set wrapmargin=0 / :set wm=0 - Turn off Wrap margin– :set ignorecase or :set ic - Set ignore case during
searches– :syntax on - Turn on syntax
highlighting– :syntax off - Turn off syntax
hightlighting
CUSTOMIZING VI SESSION• Options can be set in the following ways
– During a vi session• :set nu / :set nonu ...etc ( preserve setting for that session)
– For permanent setting for a user.• Create either ~/.vimrc or ~/.exrc file in user home directory
Sample contents of .exrc are →
set nuset aiset wm=10
ABBREVIATIONS • ABBREVIATIONS → are text strings which automatically expand
to larger string when used in Insert Mode ( use .vimrc for permanent changes )
• To add an abbreviation– :ab UW University of Delhi
(Now if in insert mode I enter UW it be expanded to University of Delhi on entering any non-alphanumeric character)
• To list currently defined abbreviations – :ab
• To disable / delete an abbreviation use the :unab command – :unab UW → clears the given abbreviation– :abc → clear all the set abbreviation
MAPPING• MAPPING → any key can be mapped as shortcut for command.
( For making permanent changes use .vimrc )
• To add a key map – :map – dd (it creates a key map that works in command mode)– :map! @ dd (it creates a key map that works in insert mode)
• To list currently defined map keys – :map– :map!
• To remove a keymap– :unmap -– :unmap @
USEFUL TIPS• Launch vi and begin editing <filename> at line 125
– # vi +125 <filename>
• Launch vi and edit multiple files– # vi <file1> <file2> (switch to next file using :n)
• Change case of character under cursor– ~ ( press tilde key at current cursor positon)
• Join the current line and the next line– J ( capital J in command mode )
• Remove null lines in the file– : g/^$/d
• List all occurrences of the word 'foo' with line numbers– :g/foo/#
• Read output from a Unix shell command into current text– :r !<command>
USEFUL TIPS• Get the line number of current cursor positon
– Ctrl + g
• Repeats the action performed by last command– . ( press . In command mode )
• Temporarily returns to the shell to perform shell commands– :sh ( Type exit to return to vi )
• Execute a command from vi editor ex mode – :! <command>
LEARNING MORE
• vi/vim built-in help
• :help topic
• :help
• Use :q to exit help
• vimtutor command
• NOTE → USERS WITH UIDs > 200 WHEN TYPE VI WILL GET VIM EDITOR ( Because of alias of vi )