Wednesday, 26 October 2016

My VI cheat sheet


The VI editor is an indispensable tool for any UNIX system administrator. In this post I'd like to share some of the common vi commands & options that I use or try to use on a routine basis.


Leaving vi:

shift +zz -> write & quit.
wq -> write & save
q! -> quit without saving.
:w filename -> save ac copy of the currently open file with a new name.


vi modes:

command mode -> aAiIoOc -> inset mode
command mode  <- ESC <- insert mode
command mode -> : ! / ? -> promtpt/ex mode
command mode <- press enter <- promtpt/ex mode


Copying command sequences:

Copy an example command sequence: jhhkkkkll
The commands get copied to OSs' clipboard
Open a file in vi & while in command mode right click to paste the copied command sequence.
The result is that vi executes the command sequence.

Open multiple files for editing in a single vi command:

:n -> go to next file
:N -> go to previous file
:rew -> rewind to first file.
:r -> read a file into current file.



Basic cursor movement:

h -> move cursor left
l -> move cursor right
j -> down 1 line from current character
k -> up 1 line from current character

Moving around b/w text:

w -> forward a word
b -> backward a word
e -> forward to end of current word
) -> forward a sentence
( -> backward a sentence
} -> forward a paragraph
{ -> backward a paragraph

Note:
vi assumes end of sentences by . & paragraph changes by blank lines.
You can prefix a number before the movement commands


Moving by lines:

^ -> move cursor to beginning of current line
0 -> move cursor to beginning of current line
$ -> move cursor to end of current line
1G -> move to first line
G -> move to end of file
nG -> move to nth line of file
% -> matching paren/braces
ctrl+g -> display file status & line number


Scrolling:

ctrl+d -> scroll down half a screen
ctrl+u -> scroll up half a screen
ctrl+f -> scroll forward by a screen
ctrl+b -> scroll backward by a screen


Inserting text:

i -> insert before cursor
I -> insert at beginning of line
a -> append after cursor
A -> append at the end of line
o -> open a new line below current line
O -> open a new line above current line

File Management:

:w -> write without quitting
:q -> quit without writing.
:q! -> quit without saving changes.


Deleting text:

x -> delete current character
dd -> delete current line
dw -> delete current word
d^ -> delete to beginning of line
d$ -> delete to end of line

Note:
You can prefix a number before the deletion commands


Changing text:

r -> r followed by character replaces current character with the character typed with r
R -> override mode i.e the characters you type replace the characters already present on the cursor prompt.
~ -> changes case of current characters on the cursor


Undo, redo & repeat:

u -> undoes last change
ctrl+r -> redoes last change (undoes undo)
U -> undoes all changes on current line
. -> repeats last change


Delete, yank & put:

dd -> delete (cut) the line
yy -> copy the line
dx -> delete an amount
yx -> copy an amount
y$ -> copy from cursor position to end of the line
y^ -> copy from cursor position to beginning of the line
p -> put after cursor
P -> put before cursor

Note:
delete & yank go into vi's buffers & the buffer holds only the most recent yank/delete
As with delete, y can use a number or movement command


Searching text:

/ -> search for a pattern

Using regex in search patterns:

. -> a search for a.c yields abc or aec i.e. the . (dot) replaces exactly one character.
\ -> the backslash is used to remove special meaning of special characters like ., $ & ^
^string -> search for lines beginning with the searched string.
$string -> search for lines ending with the searched string.
[] -> matches a character set. The expression in the brackets stands for a single character in the set.


Search & replace:

:%s/old/new/g -> replaces every occurance of old with new in the entire file.
:%s/the[ym]/(&)/g -> enclose the search results in parenthesis throughout the file.
:%s/\(he\) \(is\)/\2 \1/ -> replace all occurances of 'he is' with 'is he'.

Indenting:

>> -> incdents current line (inserts a tab)
<< -> outdents current line (removes a tab)

Note:
Both >> & << can use a number prefix to indent/outdent multiple lines.


Filtering through shell commands:

!! ->  filters current line through shell command
n!! -> filters n lines
!% -> filters to matching pattern
!{ -> filters previous paragraph
!} -> filters next paragraph

Although this is a cheat sheet but I feel the need to elaborate on filtering. Vi allows the usage to any shell command that accepts some type of text as input, manipulate the text & provide output.
This way we can use the text manipulation capabilities of awk, sed & tr from within vi.

Using lines & line ranges in colon commands:

: line [,line] command -> performs commands on lines specified.
n -> line number
. -> current line
$ -> last line of file
/regex/ -> regular expression
% -. all lines.


Here are a couple of examples:

:%>> -> indent all lines.
:.,$ ! tr a-z A-Z -> change all lines between current line & last line of the file to upper case.
: /the/, /end/ d -> delete everything between the strings 'the' & 'end'

No comments:

Post a Comment

Using capture groups in grep in Linux

Introduction Let me start by saying that this article isn't about capture groups in grep per se. What we are going to do here with gr...