TinyVi a tiny vi
AlphaNew software. Things will break, and features are missing.

TinyVi commands

This is every command TinyVi has. Each one does what it does in vi; where TinyVi follows Vim instead, or differs from both, the text says so. Every command here is tested, most of them against Vim 9.1 itself (testdata/cases.txt).

Notation: {motion} is any motion, {count} a number typed first, x a letter (a register or a mark), Ctrl-X the control key. A count is optional unless the text says otherwise.

The vi command

cmd/vi is TinyVi on a terminal: macOS, Linux and the BSDs, the Windows console (Windows 10 and later: Windows Terminal, the console window, ssh), and Plan 9 (9front): in a rio window or in vt(1).

vi [-R] [+command] [file]
vi -r [file]
view [file]
Option What it does
file The file to edit; a new one if it isn't there. TinyVi edits one file: when more are named, the first.
-R Read-only: :w refuses (E45) unless :w!. view is vi -R.
+command, -c command Run an ex command after reading the file: +12 goes to line 12, +/text to the first line with text, + to the last line.
-r file Recover: the file, with the changes a session that ended without writing them had made. See Files.
-r List the files that have changes to recover.
--version, --help Say so, and stop.

On Plan 9, in a plain rio window (which has no cursor addressing), it draws its own cells with the draw device, in your font ($font, else /lib/font/bit/vga/unicode.font): the cursor is a reversed cell, and the window may change size. In vt(1) (which sets TERM) it draws as on a terminal, with the size from $COLS and $LINES. Keys come from /dev/cons in raw mode; :! and :sh run rc.

On Windows it draws through the console's VT mode and reads keys as key events, so Escape is never mistaken for the start of a key's sequence; it uses the console when standard input or output is redirected, and :! and :sh run cmd.exe. Elsewhere it draws with the control sequences of $TERM's terminfo entry (from $TERMINFO, ~/.terminfo, $TERMINFO_DIRS and the system's folders), or an ANSI terminal's when there is none, on the alternate screen, which it leaves as it was at the end. The window may change size. A hang-up (the connection dropped) or a terminate keeps the changes not written for vi -r. :! commands run with $SHELL -c; :sh runs sh, on TinyVi's console (see Shell commands).

Normal mode

TinyVi starts in normal mode. Keys are commands; Esc cancels one that is half typed.

Keys What it does
i / Insert Insert before the cursor.
a Insert after the cursor.
I Insert before the first non-blank of the line.
A Insert at the end of the line.
o / O Open a new line below / above, and insert there.
{count}i … A count types the text that many times: 3ix<Esc> is xxx, 3ox<Esc> is three lines.
x / Delete Delete the character under the cursor (3x: three).
X Delete the character before the cursor.
D Delete to the end of the line (d$).
C Change to the end of the line (c$).
s Change the character under the cursor (3s: three).
S Change the whole line (cc).
Y Yank the line (yy).
p / P Put the register after / before the cursor; lines go below / above the line. 3p puts it three times.
r{char} Replace the character under the cursor; 3rx replaces three. r<Enter> breaks the line.
J Join the next line to this one (3J: three lines). The next line's leading blanks become one space, two after . ? !, none before ).
~ Switch the case of the character, and move on (5~: five).
>> / << Shift the line one shiftwidth right / left (3>>: three lines).
u Undo the last change (3u: three). Up to 1000 changes.
Ctrl-R Redo what u undid.
. Repeat the last change. A count replaces the change's own: 3x then 5. deletes five.
m{a-z} Set a mark here.
: Type an ex command. 3: starts it with the range .,.+2.
& Repeat the last :s on this line.
ZZ Write the file if it changed, and quit (:x).
ZQ Quit without writing (:q!).
Ctrl-^ Edit the file before (:e #).
Ctrl-G Show the file name, whether it is modified, the lines and where the cursor is.
Ctrl-L Draw the screen again.
Ctrl-F / Page Down A screen forward (two lines overlap).
Ctrl-B / Page Up A screen back.
Ctrl-D / Ctrl-U Half a screen down / up, the cursor too. A count sets how far, for this and the later ones.
Ctrl-E / Ctrl-Y The screen one line down / up; the cursor moves only if it would leave the screen.
"x Use register x for the next delete, yank or put: "ayy, "ap.

Motions

A motion moves the cursor, or (after an operator) says how much text the operator takes. A count repeats it.

Keys Moves to
h / Left / Backspace / Ctrl-H The character to the left.
l / Right / Space The character to the right.
j / Down / Ctrl-N / Ctrl-J The line below, in the same column.
k / Up / Ctrl-P The line above, in the same column.
+ / Enter The first non-blank of the line below.
- The first non-blank of the line above.
_ The first non-blank of this line (3_: two lines down).
0 / Home The start of the line.
^ The first non-blank of the line.
$ / End The end of the line (2$: of the next line). After $, j and k stay at the ends of lines.
{count}| Column count.
w / W The start of the next word / blank-separated word.
b / B The start of this or the word before / blank-separated word.
e / E The end of this or the next word / blank-separated word.
G The last line, or line count (10G).
gg The first line, or line count.
H / M / L The top / middle / bottom line of the screen (3H: the third from the top).
% The bracket that matches the next ( ) [ ] { } on the line.
{count}% That many per cent through the file (50%).
f{char} / F{char} The next / previous char on the line.
t{char} / T{char} Just before the next / just after the previous char.
; / , The last f F t T again, the same way / the other way.
/{pattern}<Enter> The next match of the pattern. /<Enter> uses the last one.
?{pattern}<Enter> The previous match.
n / N The next match of the last search, the same way / the other way. Searches go round the end of the file, and say so.
`x / 'x Mark x / the first non-blank of its line.
`` / '' Where the cursor was before the last jump / that line.

A word is letters, digits and _; or a run of other non-blank characters. An empty line is a word too. The jumps, which set ``, are G, gg, H, M, L, %, /, ?, n, N, the marks and :{line}.

Operators

An operator is followed by a motion, and acts on the text the motion moves over. Typed twice, it acts on whole lines: dd, 3dd (three lines).

Operator What it does
d{motion} Delete.
c{motion} Change: delete, then insert. cw changes to the end of the word, not over the blanks after it.
y{motion} Yank (copy) into a register.
>{motion} / <{motion} Shift the lines one shiftwidth right / left.
!{motion} Filter the lines through a shell command: see Shell commands. !! filters the line, 3!! three lines.

Counts multiply: 2d3w deletes six words. Motions by lines (j, k, G, 'a, …) act on whole lines; the others on characters, up to the motion's place, or including it for e, $, f, t and %. As in vi, dw on the last word of a line doesn't join the next line, and a character motion that ends at the start of a line ends at the end of the line before.

Insert mode

Typed characters go into the text until Esc.

Keys What it does
Esc Back to normal mode; the cursor moves back one.
Enter / Ctrl-J Break the line. With autoindent, the new line gets the indent of this one.
Backspace / Ctrl-H Delete the character before the cursor; at the start of a line, join it to the line before.
Delete Delete the character under the cursor.
Ctrl-W Delete the word before the cursor.
Ctrl-U Delete everything before the cursor on the line.
Tab A tab; spaces to the next tab stop with expandtab.
Ctrl-V{key} The next key as a character (Ctrl-V Tab for a tab with expandtab).
Left Right Up Down Home End Move the cursor.

The command line

:, / and ? read a line at the bottom of the screen.

Keys What it does
Enter Run it.
Esc / Ctrl-C Cancel.
Backspace Delete the character before the cursor; on an empty line, cancel.
Ctrl-U Delete the whole line.
Ctrl-W Delete the word before the cursor.
Ctrl-V{key} The next key as a character.

Registers

Register What it holds
"" The last delete or yank. p puts it. With the computer's clipboard (see below), it is the clipboard.
"* and "+ The computer's clipboard, by either name. Without one, they are "".
"0 The last yank.
"1 – "9 The last nine deletes of a line or more; each one moves the others up.
"- The last delete of less than a line.
"a – "z What you put there: "ayy, "ap.
"A – "Z Add to "a – "z instead of replacing it.

The computer's clipboard

Where the computer has a clipboard, the unnamed register "" is that clipboard, and so are "* and "+. A yank, a delete or a change puts its text on the clipboard, and p and P take the text from it, so text moves between TinyVi and other programs. Text that ends in a new line is put as lines; other text as characters. A named register ("a) stays in the editor and leaves the clipboard alone; after "ayy, p still puts the clipboard. Text from another program has its line ends and control characters cleaned, and is cut at 1 MiB. If the clipboard can't be used, TinyVi says so once and the registers go on as they always did. The vi command uses pbcopy and pbpaste on a Mac, the Windows clipboard, /dev/snarf on Plan 9, and on Linux and FreeBSD wl-copy and wl-paste, xclip or xsel; with none of them (over ssh) a yank still reaches the clipboard of terminals that take OSC 52, but p uses the editor's register.

The mouse

On a screen that has a mouse (a terminal that sends mouse reports, a window), a left click in the text puts the cursor there, in normal mode and in insert mode (typing goes on at the new place). The wheel moves the cursor three lines. A click on the status line, or while a command is half typed (d, then a click), does nothing. The cursor is a line while text is typed and a block otherwise, and vi draws in the terminal's own colours.

Marks

ma – mz set a mark, `a goes to it and 'a to its line, as a motion (d'a) or in a range (:'a,'bd). A mark follows its line when lines above it are added or deleted; when its line is deleted, the mark is gone (E20). `` and '' go back to where the last jump started.

Patterns

Searches, :s, :g and the /re/ addresses take vi's patterns, with the additions of Vim that vi users type:

Pattern Matches
. Any character.
* Any number of the thing before (at the start of the pattern, a *).
[abc] [^abc] [a-z] [[:digit:]] One of / none of the characters; POSIX classes. A ] first is a ].
^ The start of the line, at the start of the pattern (elsewhere, a ^).
$ The end of the line, at the end of the pattern (elsewhere, a $).
\( \) A group, for \1 – \9 in a replacement.
| Either side.
\< \> The start / end of a word.
\+ \= \? One or more / none or one of the thing before.
\s \S \d \D \w \W \t A blank, a non-blank, a digit, a non-digit, a word character, a non-word character, a tab.
\c \C Anywhere in the pattern: ignore case / match case.
\. \* \[ \\ … The character itself.

( ) | + ? { } are plain characters, as in vi.

Ex commands

Typed after :. Most take a range in front; without one they act on the cursor's line. [x] is a register, ! forces a command that would otherwise refuse. The part in brackets may be left out: :d[elete] is :d, :del or :delete.

Command What it does
:{range} Go to the range's last line (:1, :$, :'a, :/re/).
:[range]d[elete] [x] [count] Delete the lines. With a count, count lines from the range's last.
:[range]y[ank] [x] [count] Yank the lines.
:[range]m[ove] {address} Move the lines below line address (:m0: to the top).
:[range]co[py] {address} Copy the lines below line address.
:[range]t {address} The same as :copy.
:[range]j[oin] [count] Join the lines (the line and the next, with no range).
:[range]> / :[range]< Shift the lines right / left; :>> twice as far.
:[range]s[ubstitute]/{pattern}/{text}/[flags] [count] Substitute: see Substitute.
:[range]&[&] Repeat the last :s; :&& with its flags.
:[range]g[lobal]/{pattern}/{command} Run the command on each line that matches (:p if none).
:[range]g[lobal]!/{pattern}/{command} On each line that doesn't match.
:[range]v[global]/{pattern}/{command} The same as :g!.
:[range]p[rint] Show the lines.
:[range]nu[mber] / :[range]# Show the lines with their numbers.
:[range]l[ist] Show the lines with tabs as ^I and the ends as $.
:[range]= Show the range's last line number (the number of lines, with no range).
:[line]k {a-z} Set a mark on the line (:ka too).
:[line]ma[rk] {a-z} The same as :k.
:w[rite][!] [file] Write the buffer, to its file or to file. A buffer with no name takes file as its name.
:[range]w[rite][!] {file} Write the lines to file.
:q[uit][!] Quit; it refuses if there are changes not written (E37), unless !.
:wq[!] [file] Write and quit.
:x[it] [file] / :exi[t] [file] Write if there are changes, and quit.
:e[dit][!] [file] Edit file, or this file again. It refuses if there are changes not written, unless !, which drops them.
:e[dit] # Edit the file before (Ctrl-^).
:[line]r[ead] {file} Read the file in below the line (:0r: at the top).
:f[ile] [name] Show the file's name and where the cursor is; with name, rename the buffer.
:se[t] {option} … Set options: see Options.
:u[ndo] / :red[o] Undo / redo one change.
:noh[lsearch] Does nothing: TinyVi doesn't highlight matches.
:!{command} Run a shell command and show its output: see Shell commands.
:[range]!{command} Filter the lines through the command.
:[line]r[ead] !{command} Read the command's output in below the line.
:[range]w[rite] !{command} Give the lines (all, with no range) to the command as its input.
:sh[ell] Go to the shell; leaving it comes back to the editor.

Output that is more than one line (:p, :g/re/p) shows at the bottom of the screen with "Press ENTER or type command to continue" (-- More -- for more than a screen; q stops). Typing : there starts the next command.

Ranges

A range is one or two addresses, separated by , (or ;, which moves to the first address before reading the second).

Address Line
{number} That line. 0 is before the first line: :0r file, :m0. For other commands it is line 1.
. The cursor's line.
$ The last line.
% The whole file (1,$).
'x The line of mark x; '' the line before the last jump.
/{pattern}/ The next line that matches (after the cursor's).
?{pattern}? The previous line that matches.
+{n} / -{n} After any address, n lines on or back: .+2, $-1, /re/+1. + and - alone are 1.

An address left out before , is the cursor's line: :,+1d deletes this line and the next. A range given backwards (:4,2d) is turned round.

Substitute

:[range]s/{pattern}/{text}/[flags] [count]

Any character that isn't a letter, a digit, a blank, \, " or | can take the place of /: :s#/usr#/opt#. A \ before the delimiter makes it part of the pattern or the text. An empty pattern is the last search pattern. The last delimiter and the flags may be left out: :s/a/b.

Flag What it does
g Every match in the line, not just the first.
i / I Ignore case / match case, whatever ignorecase is.
e No error when nothing matches.
n Only count the matches ("3 matches on 3 lines"); change nothing.
p # l Show the last line changed.
& First: the flags of the last :s (:s/a/b/&).
{count} After the flags: count lines from the range's last.

In the text:

Text Is replaced by
& / \0 The whole match.
\1 – \9 Group 1 – 9 of the match.
~ The text of the :s before.
\r A line break.
\n A NUL character (shown as ^@), as in Vim.
\t A tab.
\u / \l The next character in upper / lower case.
\U / \L The rest in upper / lower case, until \e or \E.
\& \~ \\ \/ The character itself.

After :s the cursor is on the first non-blank of the last line changed. When more than two substitutions are made, it says how many ("17 substitutions on 4 lines"). One u undoes the whole :s, as it does a whole :g.

Examples:

Command What it does
:%s/search/replace/g Replace every search in the file.
:3,4s/stext/rtext/ Replace the first stext on lines 3 and 4.
:%s/\(\w\+\) \(\w\+\)/\2 \1/ Swap the first two words of each line.
:g/^$/d Delete the empty lines.
:g/^/m0 Turn the file upside down.
:v/TODO/d Keep only the lines with TODO.

Shell commands

The program TinyVi runs in gives it its shell: DCL in dclshell, and sh, cmd.exe or rc in the vi command. Where there is none (a host that gives none), these commands say E319.

Command What it does
:!{command} Run the command on TinyVi's console. Its output shows as it comes, and it can read what you type. Then "Press ENTER or type command to continue". First "[No write since last change]" if the buffer has changes.
:[range]!{command} Filter: the lines go to the command as its input, and its output takes their place. :%!sort sorts the file.
!{motion}{command} Filter the lines the motion goes over: !}, !G, !j. TinyVi puts the range on the command line (:.,.+2!) for you to type the command after it, as vi does.
!!{command} / {count}!!{command} Filter the line / count lines.
:[line]r !{command} Put the command's output below the line (:0r !cmd: at the top).
:[range]w !{command} Give the lines to the command as its input, and show its output on the console. The buffer isn't written, and stays modified.
:sh[ell] Run the shell on TinyVi's console. Leaving it (exit, LOGOUT, or Ctrl-D on an empty line) comes back to the buffer. The vi command runs sh for it (with its line editing off, as the console edits the line), not $SHELL.

The console is TinyVi's own screen for a command: the output scrolls up from the bottom as the command writes it, and what you type goes to the command a line at a time.

Key on the console What it does
Enter Give the typed line to the command.
Backspace, Ctrl-U, Ctrl-W Delete a character, the line, a word.
Ctrl-V{key} The next key as a character.
Ctrl-D / Ctrl-Z On an empty line: the end of the input.
Ctrl-C Interrupt the command: a reading command gets an interrupt, and the vi command stops a running one (SIGINT to it and what it started, then, after two seconds, SIGKILL).

The console draws plain text: colours and other escape sequences in the output are left out, and full-screen programs (top, vi) don't work on it.

  • A ! in a command is the last command: :!! runs it again, :%!! filters with it again, and :!! | more adds to it. \! is a plain !.
  • A filter's input is its lines, and :r !'s is empty: their output goes into the buffer, not on the console, so they don't read what you type.
  • A filter that fails changes nothing. Its output and its status show as an error ("shell returned: exit status 1"). Vim takes the failed command's output in place of the lines; TinyVi keeps them.
  • One u undoes a filter, and . repeats one.
  • After a filter of more than two lines, TinyVi says how many ("4 lines filtered"), as vi does.

Options

:set {option} turns an option on, :set no{option} off, :set inv{option} or :set {option}! the other way, :set {option}={n} gives a number, and :set {option}? shows it. Several may follow one :set. :set alone shows the options that differ from the defaults; :set all shows them all.

Option Default What it does
autoindent (ai) off A new line gets the indent of the line before.
expandtab (et) off Tab, >> and << make spaces, not tabs.
fileformat (ff) as the file unix: :w ends lines with LF; dos: with CR LF. See Files.
ignorecase (ic) off Searches, :s and :g ignore case.
list (list) off Show tabs as ^I and the ends of lines as $.
number (nu) off Show line numbers.
readonly (ro) off :w to the file refuses (E45) unless :w!.
shiftwidth (sw) 8 How far >>, <<, > and < shift.
tabstop (ts) 8 How wide a tab is.
wrap (wrap) on Long lines go on over more rows; with nowrap, the screen scrolls sideways.

Files

TinyVi writes a file back as it read it:

  • Line ends. A file whose every line ends in CR LF (Windows) is a dos file: the lines show without ^M, and :w ends every line, new ones too, with CR LF. One line that ends in LF alone makes it a unix file, whose CRs show as ^M, as in Vim. :set ff=dos or :set ff=unix changes how :w ends lines.
  • Bytes that aren't UTF-8 text (a Latin-1 file, a binary file) show as <e9> and are written back as they were.
  • The last line is written with a line end, as Vim does by default.

Writing safely. :w refuses to write over a file that something else has changed since TinyVi read it ("WARNING: The file has been changed since reading it"); :w! writes it all the same. In the vi command, a file is written to a new file beside it, which then takes its place, so a write that fails (a full disk) leaves the file as it was. A symbolic link is written through to its file. A file with other hard links, or that someone else owns, is written in place, after a copy of it is kept until the write has worked, so the links and the owner stay.

Recovery. While the buffer has changes not written, TinyVi keeps a copy of it (at most once a second as you work, and at once if the terminal goes: a dropped connection). vi -r file opens the file with those changes; :w then writes them, or :q! gives them up. Writing the file or quitting removes the copy. When a copy from an earlier session is there, opening the file says so ("E325: ATTENTION"), and that session's copy is kept as it is until it is recovered. The vi command keeps the copies in your cache folder (tinyvi/recovery), readable by you alone; dclshell and other hosts may keep none.

When it opens a file, TinyVi says what it found, as Vim does: "notes.txt" [noeol][dos] 12L, 340B. [noeol] is a last line with no line end, [dos] CR LF line ends, [NUL bytes: not text?] a file that holds NUL bytes, and [ILLEGAL BYTE in line 3] the first line with a byte that isn't UTF-8.

Not in TinyVi

These vi or Vim commands aren't there; most say so with an error.

  • Visual mode (v, V, Ctrl-V), macros (q, @), text objects (iw, a"), several windows, syntax colour, .exrc and :map.
  • ge, gE, (, ), {, }, [[, ]], U, R (replace mode), gp, gJ, *, #.
  • :a, :i, :c, :pu, :z, :ab, :map, :normal, :so, :ve.
  • The c flag of :s (confirm each one): it says E474.
  • | between ex commands (:g/x/s/a/b/|d).
  • :j! joins with spaces, the same as :j.
  • Recovery and file safety come with TREE-1527.

How TinyVi differs from vi in what it does have:

  • Undo has 1000 levels and Ctrl-R redoes, as in Vim (vi has one level, and u undoes the undo).
  • Backspace in insert mode can go back past where the insert started, and join lines, as Vim does with backspace=indent,eol,start.
  • A count that would make more than 10 MB of text (99999999p) is refused with Vim's E1240, as is a count that would type more than 100000 keys again one by one (text with Backspace or the arrows in it; plain text and line breaks have no such limit).
  • A search with a count past the number of matches goes round as many times as it takes, but quickly: 99999999n is as quick as n.
  • A combining mark takes a column of its own on the screen.