Working With Vi

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

*** Working With Vi ***

General Commands
vi [filename]
Starts the vi editor (if filename is given open that file into the editor).
vi -R <filename>
Open a file in read-only mode.
view <filename>
[Esc]
Return to the command mode.
:!<command>Execute shell commands within vi (Press to return to vi).
:sh
Create a shell within vi. Type [Ctrl]+D or exit to return to vi.
^Z
Suspend vi and return shell prompt. Type fg to return to vi.
:pre / :preserve
Force the system to save current buffer to disk.
Keystroke

Action

ZZ / :wq / :x

Save changes and quit from editor

:w [filename] *

Save changes (to a given filename if provided)

:w!

Overwrite the existing file

:q

Quit if no unsaved changes

:q!

Force quit discarding unsaved changes

:e!

Return to the last saved version of the file

* Can give a filename with path as well.


Moving the Cursor
Keystroke

Movement

h j
k
l
n{h,j,k,l} where n=1,2,3,...

Single cursor movement


Prefix with n to multiply the command

:set wm=n

Automatically insert newline character after n characters are typed


(n=0 no wrap margin)

:set number / :set nu

View line numbers

:set nu!

Switch off line numbers

Move to beginning of the line

Move to end of the line

Move cursor to next word counting symbols & punctuation


equivalent to words

Move cursor to next word not counting symbols & punctuation

Move cursor to previous word counting symbols & punctuation


equivalent to words

Move cursor to previous word not counting symbols & punctuation

G
nG

Go to last line of the file


Go to nth line of the file
1 of 5

Simple Editing Commands


Keystroke

Action

Insert text at the cursor position 1

Insert text at beginning of line 1

Append text after the cursor position 2

Insert text at end of current line 2

Delete character under the cursor

Delete character before the cursor

d*

Delete text
w word after the current cursor position (including the space before next word)
e to end of the word
E to end of word including punctuation
b word before the current cursor position
d current line
0 to the beginning of the line from previous character
$ - to the end of line from current cursor position

D (d$)

Delete text to end of line from current cursor position (multiply by n also work)

c*

Change text
w word
$ - to the end of line from current cursor position
0 from previous character to beginning of the line
l current letter
c current line

C (c$)

Change text to the end of line from current cursor position

Replace character 3

Enters the overstrike mode (Press to add a new line or [Esc] to exit the
overstrike mode)

s / c[SAPCE]

Substitute text (multiply by n also work)

S / cc

Substitute entire lines (multiply by n also work)

Yank (copy) text


y current line
) - current sentence
} - current paragraph

Y (yy)

Yank current line

Paste text
- for words/letters paste after cursor position
- for lines of text paste below the current line

Paste text
- for words/letters paste before cursor position
2 of 5

- for lines of text paste above the current line


.

Repeat commands

Toggle case (multiply by n also work)

Undo last command (including U)

Restore current line to original state before changed (if the cursor is on that line)

Insert blank line below the cursor

Insert blank line above the cursor

Join two lines (multiply by n also work)

^@

repeat the most recent insertion in insert mode

^R

Redo an undo operation

n{x,X,d,c,Y,j,h,k,l}{w,b,d,...} - Multiply command n times


* to change/delete part of a word place the cursor at desired position and issue the command.
* ''np paste the nth buffered text line (not work with portions of text)
* xp transpose two characters
1

ni*[Esc] insert n asterisks


na*[Esc] append 2n characters (pairs of * and -)
3
2r& - replace n number of characters with a repeated instance of a single character
e.g.: || 2r& &&
** ea append text to the end of a word
2

Moving Around within the File


^F

Scroll forward one screen

^B

Scroll backward one screen

^D

Scroll forward half screen (down)

^U

Scroll backward half screen (up)

^E

Scroll the screen up one line *

^Y

Scroll the screen down one line *

* Does not move the cursor position


z

Move current line to top of screen and scroll *

z.

Move current line to center of screen and scroll *

z-

Move current line to bottom of screen and scroll *

^L

Redraw screen by clearing interrupt messages appear

Move to home top line of the screen


3 of 5

Move to middle line of the screen

Move to last line of the screen

nH

Move to nth line below top line

nL

Move to nth line above last line

OR + Move to first character of next line **


-

Move to first character of previous line **

^ (hat)

Move to first non-blank character of current line

n|

Move to column n of current line

Move to end of word

Move to end of word (ignore punctuation)

Move to beginning of current sentence ***

Move to beginning of next sentence ***

Move to beginning of current paragraph ***

Move to beginning of next paragraph ***

[[

Move to beginning of current section ***

]]

Move to beginning of next section ***

* Numeric prefix has no meaning in this context. If specified it is considered as a line number.
e.g.: nz move line n to the top of screen
** Ignore spaces or tabs
*** Vi find end of a sentence by . ? !. It locates end of sentence when the punctuation is followed by
at least two spaces or when it appears as the last non-blank character on a line.
*** A paragraph is defined as text up-to the next blank line or default paragraph macros. Sections
are defined as default section macros.
e.g.: 3) moves ahead three paragraphs
d) delete to end of current sentence
2y} yank two paragraphs ahead
Movement with Searches
/text

Search forward for first occurrence of text

?text

Search backward for first occurrence of text

Repeat search in same direction

Repeat search in opposite direction

Repeat search forward (once)

Repeat search backward (once)

Recall previous/next search term


4 of 5

* :set nowrapspan disable wrap around file when the search fails
* c/d commands can combined with searches to delete/change text on character basis.
e.g.: d?text delete from before the cursor up-to word text
fx

Move cursor to the next occurrence of x in the line

Fx

Move cursor to previous occurrence of x in the line

tx

Move cursor to character before next occurrence of x in the line

Tx

Move cursor to character after previous occurrence of x in the line

Repeat previous find command in the same direction

Repeat previous find command in the opposite direction

* f and t can use with multiply by n.


e.g.: dfx delete up-to x including it.
ct. - change text up-to the end of a sentence, leaving the period.
Movement by Line Number
nG

Go to nth line

Go to last line

``

Return to original position where G command issued. If any editing done return to
the last edit.
For / or ? return to where the search started.

'' (two quotes)

Return to cursor beginning of line instead of exact position

* set nu - Display line numbers


* ^G Display total number of lines in the file, current line number and % of the current line
number for the total lines.

5 of 5

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy