Skip to content

Files and windows

Reading and writing

:w             write
:w other.py    write somewhere else, and edit that from now on
:q             quit — refuses if the buffer has unsaved changes
:q!            quit anyway
:wq  :x        write and quit
ZZ             the same, without the colon

:q with a split open closes the window, as vim's does. Only the last window closing leaves the editor.

Several files at once

A buffer is a file that is open. A window is a rectangle looking at one. Most of this page is about the difference between them.

:e path        open a file
:e             read this one again off the disk
:e!            …discarding what you changed
:ls            list the buffers                (:buffers, :files)
:bn            the next buffer, wrapping       (:bnext)
:bp            the one before, wrapping        (:bprev, :bN)
:n             the next one, not wrapping      (:next)
:N             the one before, not wrapping    (:prev)
:bfirst        the first one                   (:bf, :first, :rew)
:blast         the last one                    (:bl, :last)
:b name        the buffer whose name matches   (:buffer)

A file already open comes back as the buffer it is, never as a second copy: two buffers on one file would each hold half the undo history, and whichever was written last would throw the other's work away without telling you.

Switching buffers keeps your registers and macros (those belong to the session) and leaves the undo history with the buffer it came from.

Splits

:sp [path]     a window above this one           (:split, :new)
:vsp [path]    a window beside it                (:vsplit, :vs, :vnew)
:clo           close this window                 (:close)
:on            close every other one             (:only)

Then press Ctrl+W and one of these:

w the next window in reading order, wrapping
h j k l left, down, up, right (arrows work too)
s v split this one, horizontally or vertically
c q close it
o close every other one

Two windows on one buffer share the text and keep their own cursors, so :sp lets you read two parts of one file at once.

The layout is a grid: :sp adds a row across the full width, :vsp adds a window to the row it is in. Split one way at a time and it behaves exactly as vim's tree of windows does.

The file picker

Ctrl+P, or :find, is a fuzzy picker over the project. Type a few letters of the path and press Enter; it opens in the current window. The characters that matched are underlined, so you can see why something ranked where it did.

It searches from the same project root :grep uses, and skips the same build output. The walk is pruned as it goes, so it reads the project without reading its virtualenv.

Reading rendered Markdown

gR on a Markdown file shows it rendered: headings, lists, tables and code blocks laid out and coloured, at the width of the window up to 80 columns. Longer lines are harder to read. :set readerwidth=100 changes the limit, and :set readerwidth=0 removes it. gR again goes back to the source. The status line says READER and shows gR edit as a reminder.

The rendered text is read-only and otherwise ordinary. Motions, /, visual mode and yanking all work on it, so a command can be copied out of a code block and put into another file. Each side keeps its own cursor, and switching puts it on the same word it was on.

i, a, I, A, o and O go back to the source and start inserting there, on the word the cursor was on, so a typo found while reading is one key from being fixed. Any other edit is refused with a message.

Enter on a link follows it. A Markdown file opens rendered, at the heading its #anchor names. Any other file opens as source, and a web address opens in your browser. Ctrl+O goes back, one link at a time.

:reader                 the same as gR
:set reader             rendered            (:set noreader for the source)
turbovi README.md +'set reader'     open straight into it

Any other format renders through a command that reads the text on its input and prints the result. Name it in the config:

render html lynx -dump -stdin -width={width}
render plantuml plantuml -tutxt -pipe
render markdown glow -s dark -w {width}

{width} is the window's width, and colours in the output are kept. Naming a command for markdown replaces the built-in renderer. :render on its own lists them, and :render NAME forgets one.

The system clipboard

"+y   "+p      yank to and put from the system clipboard
"*y   "*p      the same

This is a subprocess away (pbcopy on macOS, wl-copy or xclip on Linux), so it cannot happen inside a keystroke. The keymap records what was asked for and the session performs it. You will not notice, except that a machine with none of those programs installed says so.

Encodings and line endings

Three things are decided when a file is read and then honoured for the life of the buffer: how the bytes were decoded, what the line endings were, and whether the last line had one.

A latin-1 file with CRLF endings and no trailing newline is written back as exactly that. Getting any of this wrong turns "open a file and save it" into a diff of the whole file, which is the worst bug a text editor can have: it is silent, it is in every file you touch, and version control reports it as your work.

UTF-8 is tried first and latin-1 is the fallback, because every byte sequence is valid latin-1. When the guess is wrong, the file is still written back byte for byte. A byte-order mark is detected, never guessed at.

You can override any of it:

:set ff=unix      write LF endings          (fileformat)
:set ff=dos       write CRLF
:set ff            say which it is now
:set eol          end the file with a newline
:set noeol        do not
:set encoding=utf-8

.editorconfig sets the first two for you where it says so (see settings).

Where you left off

When the editor exits, it records where the cursor was in each open file, under $XDG_DATA_HOME/turbovi/positions.json. Opening the file again puts the cursor back there, or as near as the file now allows if it has shrunk. The last thousand files are remembered.

turbovi notes.md +42 opens on line 42 instead. Any +command runs as an ex command on the first file, as in vim, so +set nu works too. vim's +/pattern search is not supported yet.

Crash recovery

Every few seconds, each modified buffer is copied under $XDG_DATA_HOME/turbovi/recover/. If the editor dies, the copy is still there.

Opening a file with a recovery copy newer than it says so:

"notes.md" 214L — newer recovery file found, :recover to use it, :recover! to discard
:recover       put the copy into the buffer — :w to keep it, u to undo
:recover!      discard the copy

vi has no dialog for this. A buffer silently replaced by a recovery copy is worse than one that tells you the copy is there. Writing the file clears the copy, because a buffer that matches its file has nothing to recover.