# Opening Files

*Lx* can edit a file, show it read-only, render a preview, or display its raw bytes.

## Opening modes

- **Edit** lets you change text.
- **View** shows text without allowing changes.
- **Preview** uses a visual preview when the file has one.

If no preview is available, Preview safely falls back to View.

Bytes and hex are separate viewers:

- **Bytes** shows raw byte values and text.
- **Hex** shows raw byte values in hexadecimal.

## Default choices

*Lx* normally edits text and views binary files, images, and document containers.

You can change these defaults in your settings:

```lua
local config = require("core.config")

config.default_modes.text = "edit"
config.default_modes.binary = "view"
config.default_modes.image = "view"
config.default_modes.document = "view"
```

Use `edit`, `view`, or `preview`.

## Choose a mode for one format

Format settings take priority over the general defaults:

```lua
config.format_modes.md = "preview"
config.format_modes.json = "view"
```

Format names ignore letter case.

## Choose a viewer for one format

A handler chooses the viewer or special opening process:

```lua
config.format_handlers.png = "image"
config.format_handlers.dos = "ansiart"
config.format_handlers.md = "markdown"
```

Other built-in handler names include `font`, `office`, `bytes`, and `hex`.

## Correct a misleading filename

You can tell *Lx* how to treat an extension:

```lua
config.extension_overrides.xx = "md"
```

Do not include the dot before `xx`.

You can also match one exact filename:

```lua
config.filename_overrides["README.custom"] = "md"
```

Both lookups ignore letter case. An exact filename wins over an extension.

## Extracting and unpacking

Some files contain other content. An action prepares that content first:

- **Extract** gets readable text from a document.
- **Unpack** opens stored content such as XML inside an Ableton Live Set.

The selected edit, view, preview, bytes, or hex choice is applied after the action.

See [Command Line](cli.md) for examples.

## Change an open text file

Run `doc:toggle-viewer` from the command palette to switch plain text between Edit and View.

*Lx* asks you to save first when needed. The same tab and reading position are kept.

## Switch between editing and reading

Press `F9`, press `Ctrl+Alt+E`, or run `doc:toggle-edit` to switch the current tab between editable and read-only views.

Both views stay alive, so your editor state is kept. Only one is shown in the tab at a time. The normal text editor and viewer follow the same exact visible text in both directions, including wrapped text and partial rows, without moving the editor cursor. A Markdown preview uses the closest rendered content on or before the editor's visible source line. Other retained modes keep their own positions. A hidden preview waits until you switch back before it renders later edits.

Set `config.toggle_edit_source_position = "cursor"` to make a read-only view follow the editor cursor instead. Use `"view"` for exact normal text editor and viewer position matching.

Markdown prefers its rendered preview. Other text falls back to the plain viewer. Documents without an editable view are left unchanged.

While reading, press `Alt+M` or run `doc:cycle-mode` to cycle through the viewing modes supported by that document: Preview, View, Bytes, then Hex. Missing modes are skipped, and the list wraps back to the start. Binary documents skip plain View. Each mode keeps its own reading position when you return to it.

`doc:cycle-mode` does not switch between reading and editing. In Edit, it currently does nothing because Edit is the only editable mode.

## Which choice wins?

*Lx* uses the first matching choice in this order:

1. An explicit opening request
2. Your setting for that format
3. The action's preferred result
4. The format's own default
5. The general default

See [Format modules](formats.md) for more ways to control file recognition.
