vi, vim

vi is an old Unix text editor. Nowdays, it is usually just an alias for vim (vi improved), that is upwards compatible with vi. Online help is available through :help command.

vi [options] [files]

If filename is missing, ~ starts with an empty buffer. You can also use view (starts ~ in read only mode), gvim, gview (GUI versions), rvim, rview, rgvim, rgview (modes with restrictions: no shell cmds, no suspend). Besides old vi cmds, vim accepts many std key combinations available with other editors (like MS Notepad).

Some options

--help    --version

-V verbose;
+[n] position cursor on line n (or last line, if n is missing);

+/pattern

position cursor on the first occurrence of pattern;

-b binary mode; allows to edit binary or executable files;
-C force compatibility with vi;
-d start in diff mode (2 or 3 files must be specified);
-e start in ex mode (ancient Unix editor);
-h help (cmd line args and options);
-m disable file modification;
-n do not use a swap file (useful when file is on a very slow media); recovery after crash will be impossible;

-o[n]

open n windows stacked; if n is missing, open one window for each file;

-O[n]

open n windows side by side; if n is missing, open one window for each file;

-R read-only mode; can be reset with :set noro;
-r list swap files and info about using them for recovery;

-r file

recovery mode; use swap file to recover a crashed editing session; swap file has the same name as the text file with extension .swp appended;

-x use encryption when writing files (you'll be prompted for a crypt key);
General commands
ESC switch to the command mode;
Cursor control

/ / /

move cursor left, right, down, up (h / l / j / k);

w / e

to the beginning / end of the next word;

b to the beginning of the prev word;

Home / End

to the beginning / end of line (^ / $);

G to the EOF;
1G to the BOF;
nG to the line number n;

H / M / L

to the top / middle / bottom line;

CTRL + G

output current line number;

CTRL + F

page down;

CTRL + B

page up;

CTRL + D

half-screen down;

CTRL + U

half-screen up;

Add, Insert (command mode)
a add text;
i insert text;
o insert an empty line below current position;
Delete (command mode)
x delete char;
dw delete from curr pos to the end of word;
dd delete current line;
D delete from curr pos to the EOL;
nx delete n characters;
ndd delete n lines;
ndw delete n words;
dG delete from curr pos to the EOF;
d1G delete from curr pos to the BOF;
u undo;
U undo all changes in this line;
:e! restart editing from last save;
Copy, Paste (command mode)
yy copy line to the nameless buffer;
nyy copy n lines to the nameless buffer;
yw copy word to the nameless buffer;
nyw copy n words to the nameless buffer;

"ayy

copy line to the buffer "a";

"byw

copy word to the buffer "b";

"Byw

add word to the contents of the buffer "b";

p paste from the nameless buffer;
"ap paste from the buffer "a";

"add

delete line to the buffer "a";

dw delete word to the nameless buffer;
Find, Search, Replace (command mode)
fc find next c char;
Fc find prev c char;
";" repeat last search;
"," reverse last search;

/str

find next str;

?str

find previous str;

n repeat last '/' or '?' cmd;
N reverse last '/' or '?' cmd;

:start,finish s/find/replace/g

general cmd syntax;

:1,$s/the/The/g

replace the with The (global, all file);

:%s/the/The/g %

like previous;

:.,5s/^.*//g

remove all from curr pos to the 5th line;

:%s/the/The/gc

replace the with The, ask for acknowledgement;

:%s/^....//g

remove first 4 characters (global);

:1,5s/help/&ing/g

replace help with helping in first 5 lines;

:%s/ */&&/g

double number of spaces between words;

Exit / Save commands
:w write (save);

:w file

write to file;

:r file

read file;

:q quit;
ZZ same as :wq (write and quit);

visudo

edits the sudoers file (default is /etc/sudoers) in a safe fashion.

visudo [options]

The file is locked at the start and checked at the exit. If a syntax error is found, user gets a warning message and the following alternatives:

The last option is dangerous and should not be used in normal situations!

There is a hard-coded list of editors to be used (set at the compile time). Env vars VISUAL, EDITOR are ignored unless they set an editor from that list.

Options

-h    --help    -V    --vesrion

-c enable check-only mode (the existing sudoers file will be checked for syntax and a message detailing its status will be sent to stdout);
-f specify an alternate sudoers file location; with this option ~ will edit (or check) the file of your choice, instead of /etc/sudoers;
-q quiet mode (details about syntax errors are not printed);
-s enable strict checking of the sudoers file;

vmstat

reports info about processes, memory, paging, block I/O, traps and CPU activity. The first report gives averages since the last reboot, additional reports give info on a sampling period. The proc and memory reports are instantaneous in either case. ~ does not count itself.

vmstat [options] [period [count]]

period is an interval between updates (in seconds), count is the number of updates (infinity by default).

vmstat 3 5

show 5 updates [of system performance info] with an interval of 3s;

vmstat -S K 5 10

show 10 updates [of system performance info] with an interval of 5s, show sizes in kilobytes;

vmstat -d

show disk statistics (cumulative);

vmstat -s

show event counters and memory stats;

Options

-h    --help    -V    --vesrion

-a display active/inactive memory;
-f display the number of forks since boot;
-m display slabinfo;
-n display the header only once;
-s display a table of various event counters and memory stats;
-d report disk stats;

-p part

report detailed stats for the specified disk partition;

-S k|K|m|M

show sizes in bytes*n, where n is 1000 (k) or 1024 (K) or 1000000 (m) or 1048576 (M);

Field description

Processes

r the number of processes waiting for run time;
b the number of processes in uninterruptible sleep;

Memory

swpd the amount of virtual memory used;
free the amount of idle memory;
buff the amount of memory used as buffers;
cache the amount of memory used as cache;
inact the amount of inactive memory (-a);
active  the amount of active memory (-a);

Swap

si the amount of memory swapped in from disk (per sec);
so the amount of memory swapped out (per sec);

IO

bi blocks received from a block device (per sec);
bo blocks sent to a block device (per sec);

System

in the num of interrupts per sec, including clock;
cs the number of context switches per sec;

CPU

us time spent running non-kernel code;
sy time spent running kernel code;
id time spent idle;
wa time spent waiting for IO;
Field description for disk mode

Reads

total total reads completed successfully;
merged grouped reads (resulting in one I/O);
sectors  sectors read successfully;
ms milliseconds spent reading;

Writes

total total writes completed successfully;
merged grouped writes (resulting in one I/O);
sectors  sectors written successfully;
ms milliseconds spent writing;

IO

cur I/O in progress;
s seconds spent for I/O;
Field description for disk partition mode
reads total num of reads issued to this partition;
read sectors total read sectors for partition;
writes total num of writes issued to this partition;
requested writes  total num of write requests made for partition;
Field description for slab mode
cache cache name;
num num of currently active objects;
total total num of available objects;
size size of each object;
pages num of pages with at least one active object;
totpages  total num of allocated pages;
pslab num of pages per slab;