# Editing files in place

With V3, built-in file editing was removed to allow for re-evaluation. In the meantime, you can edit files in place using shell redirection and a temporary file.

***

## Basic pattern

```bash
dasel -i <format> --root '<query>' < file > file.tmp && mv file.tmp file
```

1. Read the file into dasel via `< file`
2. Redirect the output to a temporary file via `> file.tmp`
3. Move the temporary file over the original via `mv file.tmp file`

It is important to use the `--root` flag — this ensures dasel outputs the entire document, not just the modified value.

***

## Examples

### Update a value in a TOML file

```bash
dasel -i toml --root 'foo = "bar"' < config.toml > config.toml.tmp \
  && mv config.toml.tmp config.toml
```

### Update a value in a JSON file

```bash
dasel -i json --root 'settings.theme = "dark"' < config.json > config.json.tmp \
  && mv config.json.tmp config.json
```

### Add a new field to a YAML file

```bash
dasel -i yaml --root '{$root..., "newField": "value"}' < data.yaml > data.yaml.tmp \
  && mv data.yaml.tmp data.yaml
```

### Shell function for convenience

You can wrap this pattern in a shell function:

```bash
dasel-edit() {
  local format="$1" query="$2" file="$3"
  dasel -i "$format" --root "$query" < "$file" > "$file.tmp" && mv "$file.tmp" "$file"
}

# Usage:
dasel-edit json 'name = "Tom"' config.json
```

***

## Why use a temporary file?

When you redirect output to a file, the shell truncates that file **before** running the command. This means the file would be empty when dasel tries to read it. The temporary file avoids this race condition.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://daseldocs.tomwright.me/input-output/editing-files-in-place.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
