Editing¶
Modes¶
| Mode | How you get there | What it is for |
|---|---|---|
| Normal | Esc, always | Where the keys are commands. The editor starts here. |
| Insert | i I a A o O s S c |
Typing text. |
| Replace | R |
Typing over the text already there. |
| Visual | v |
Selecting characters, then acting on the selection. |
| Visual line | V |
The same, by whole lines. |
| Visual block | Ctrl+V | The same, by a rectangle of columns. |
| Command | : / ? |
The line at the bottom of the screen. |
The status bar names the mode you are in, except normal, which says nothing because it is where you spend most of your time.
Ctrl+C does not quit. It says type :q to exit, which is vi's own answer.
The grammar¶
The whole of it is:
Everything follows from that. d is delete, w is a word, so dw deletes a word. 2dw and d2w both delete two. "ad2w deletes two words into register a.
Operators¶
d |
delete |
c |
change: delete, then insert |
y |
yank (copy) |
> < |
indent, outdent by one unit of whatever the file indents with |
gu gU g~ |
lowercase, uppercase, swap case |
gc |
comment out, or back in (see commenting) |
! |
run the lines through a shell command (see below) |
Doubling an operator applies it to the whole line: dd, yy, cc, >>, <<, guu, gcc, !!. TurboVI keeps vi's asymmetry here: Y is yy while D is d$.
Motions¶
h j k l |
left, down, up, right (arrow keys work too) |
w b e |
word forwards, back, to the end |
ge |
back to the end of the word before |
W B E gE |
the same, treating punctuation as part of the word |
0 ^ $ |
the first column, the first non-blank, the end of the line |
gg G |
the first line, the last |
{ } |
back and forward a paragraph |
f F t T |
to a character on this line, forwards or back, on it or just before |
; , |
the last f F t T again, the same way or the other |
% |
to the bracket matching the next one on this line |
H M L |
the top, middle and bottom line of the window |
* # |
the next and previous place the word under the cursor is (see searching) |
`a 'a |
to a mark |
j and k move a line of the screen: on a long line that wraps, they step through it one screen line at a time, as vim's gj and gk do. With an operator they still take whole lines, so dj deletes this line and the next however long they are. Both remember the column you were aiming for, so moving through a short line and out the other side puts you back where you were.
Text objects¶
An operator takes an object as readily as a motion. i is "inner" and a is "around": a takes the delimiters too.
iw aw |
a word |
iW aW |
a word, punctuation included |
i( a( i) a) ib ab |
what is in the parentheses |
i{ a{ i} a} iB aB |
in the braces |
i[ a[ i] a] |
in the brackets |
i< a< i> a> |
in the angle brackets |
i" a" i' a' |
i` a` |
ip ap |
a paragraph; ap takes the blank lines after it |
it at |
inside an HTML or XML tag, or the tag and all, across lines |
ci" is the one to learn first: it changes the string you are standing in, whichever quote it is and wherever in it you are.
Commenting¶
gc is an operator that comments lines out and back in. gcc does this line, 3gcc three, gcip the paragraph, and gc on a selection the lines it covers.
The block toggles as one. If every line with text on it is already a comment, gc uncomments them all; otherwise it comments them all, so a block with one commented line in it comes out all commented and never half and half. The marker goes at the indent the lines share, which keeps the block aligned, and blank lines are left alone.
The marker comes from the file's syntax: # for Python and shell, // for C, Go, Java, JavaScript and Rust, -- for Haskell, Lua and SQL, and a block comment around each line for HTML, CSS and Markdown. For anything else, say how:
Single-key edits¶
x X |
delete the character under the cursor, or before it |
D C |
delete or change to the end of the line |
s S |
substitute the character, or the line |
r |
replace one character with the next key you press |
~ |
swap the case of the character |
J |
join the next line onto this one; 3J joins three |
gJ |
the same, without adding or removing spaces |
p P |
put what was yanked, after the cursor or before |
o O |
open a line below or above, keeping the indent |
| Ctrl+A Ctrl+X | add one to the next number on the line, or take one away; 5Ctrl+A adds five |
The indent o keeps is the one the line above is made of, so a tab-indented Makefile recipe stays tab-indented. Enter in insert mode keeps it too.
Ctrl+A reads decimal, with a minus sign, and hex after 0x. A number with leading zeros keeps its width, so 007 becomes 008, and hex keeps the case of its letters.
While typing¶
Insert mode has a few keys of its own, so a slip can be fixed without leaving it:
| Ctrl+W | delete the word before the cursor |
| Ctrl+U | delete back to the indent; again, the indent too |
| Ctrl+R x | put register x, as if it were typed |
R types over the text instead of pushing it along, and Backspace puts back what was there. The status bar says REPLACE while it lasts.
Visual mode¶
v, V and Ctrl+V select characters, lines and a rectangle. Any motion moves the end of the selection, and any operator acts on it: d, y, c, >, gU, gc, !. u makes it lowercase and U uppercase, r followed by a character replaces every character of it, J joins its lines, and p puts a register in its place. o jumps to the other end, iw or ip selects a text object, and gv selects again what was selected last. A register goes first, as in normal mode: "+y copies the selection to the system clipboard. On a rectangle, I and A type on every line of it; see the key reference.
Registers¶
"x before a command sends its text to register x. With no register named, it goes to the unnamed one.
"ayy yank this line into register a
"ap put it back
"+y yank to the system clipboard
"+p put from it
The clipboard registers + and * are the system one, and go through a subprocess (see files and windows).
Your terminal's own select, copy and paste all work, because TurboVI leaves the mouse alone: double-click a word, Cmd+C, and Cmd+V pastes into any mode as text. See the key reference.
Undo¶
Press u to undo and Ctrl+R to redo. One u undoes one change: everything you typed between i and Esc goes at once. A :s over the whole file goes at once too.
Repeat¶
. repeats the last change, and 3. repeats it three times. It is stored as the keystrokes that made it, as vi stores it, and that is why a command ending in insert mode replays whole: cwNEWEsc is one change, and replaying only the cw would leave you in insert mode with nothing typed.
Macros¶
A macro is . under a name, recording for as long as you like.
qa start recording into register a
… whatever you want recorded
q stop
@a play it
3@a play it three times
@@ play the last one again
Macros share storage with the yank registers, the way vi's do: "ap pastes a macro as text, and @a runs a line you yanked. A macro that calls itself stops with a message before the stack runs out.
Marks¶
ma put mark a on this line and column
`a come back to the character
'a come back to the first non-blank of that line
`` come back from the last jump
:marks list them
The difference between ` and ' matters most with an operator in front: d`a takes characters, d'a takes whole lines.
Every jump (G, gg, a search, a %) leaves a mark behind, so `` returns from it.
Smaller than vim's
Marks are not shifted when the text above them moves. Insert ten lines above mark a and it still points at the same line number, which now holds other text. Differences from vim has the other places this editor does less.
Scrolling and paging¶
| Ctrl+F Ctrl+B | forward and back a screenful |
| Ctrl+D Ctrl+U | half a screenful |
| Ctrl+E Ctrl+Y | move the view without moving the cursor |
zt zz zb |
move the view so this line is at the top, in the middle, at the bottom |
A "screenful" is the window you are in, measured on the last frame, so it is right in a split and right on a tall terminal. Ctrl+E drags the cursor only when it has to, where Ctrl+D always takes it along. The point of the pair is to look at what is below without losing your place.
Long lines wrap onto the next screen line, and tabs are drawn out to their tab stops.
Through a shell command¶
! hands lines to a shell command and puts back what it prints. Any command that reads its input and writes its output turns into an editing command this way:
:%!sort sort the file
:2,5!fmt rewrap lines 2 to 5
!ip the paragraph: opens :1,4! for the command to be typed
!!date replace this line with the date
:%!jq . pretty-print a JSON file
! with a motion, or on a selection, opens the command line on those lines and waits for the command, as vi's does, so what runs can be read before it does. The command runs through the shell, so pipelines work; one u takes the result back. If the command fails, the text stays as it was and the first line of its error is shown.
Two forms run a command without changing anything:
:!make run it; one line of output is the message, more opens a window
:w !wc -w the same, with the buffer on its input
The output is captured, so a command that wants the terminal, an editor or a pager, does not get one.