# In-code shell and Python eval

{ .image-clip }
![inline-shell-and-python-eval.webp](static/inline-shell-and-python-eval.webp)

{ .text-center .muted }
Run shell commands and evaluate Python expressions without leaving the editor.
Commands, expressions, and their results remain ordinary text in the document.

## Overview

Inline commands execute or evaluate the current line directly from the editor.

| Action                                                            | Description                                       |
|:------------------------------------------------------------------|:--------------------------------------------------|
| <kbd>⌘<span>command</span></kbd> <kbd>⇧<span>shift</span></kbd> <kbd>⏎<span>return</span></kbd> | Run inline command. |

Koi's <kbd><span>Inline command</span></kbd> interprets the current line based on its prefix:

- A line starting with `%` is run as a shell command.
- A line without a prefix can be evaluated as a Python expression.

### Configuration

By default, inline commands are only allowed in unsaved buffers, Plain Text, Markdown, and Org-mode files.
The `%` prefix can also be changed in the configuration:

<pre class="editor-mockup language-hackerman-config"><code>
<span class="config-header-syntax">[editor]</span>

inline_command_in_files             <span class="comment-syntax">txt, md, org,</span>
inline_shell_start_symbol           <span class="comment-syntax">%</span>
</code></pre>

## Inline shell

Start a line with `%`:

<pre class="editor-mockup"><code>
<span class="inline-shell-syntax">% pwd</span>
</code></pre>

Place the cursor on the line and run <kbd><span>Inline command</span></kbd>.
Koi runs the command and inserts its output below:

<pre class="editor-mockup"><code>
<span class="inline-shell-syntax">% pwd</span><span class="block-cursor"></span>
/Users/michael/project
</code></pre>

Shell commands run through your shell environment, so the same commands and command-line tools you normally use can be invoked from the document.

## Python eval

In files where inline commands are enabled, a line without a recognized prefix can be evaluated as a Python expression:

<pre class="editor-mockup"><code>
sum(x &#42; x for x in range(10))
</code></pre>

Place the cursor on the line and run <kbd><span>Inline command</span></kbd>.
The result is inserted directly below the expression:

<pre class="editor-mockup"><code>
sum(x &#42; x for x in range(10))<span class="block-cursor"></span>
285
</code></pre>

Python eval can be used as a quick calculator or for small transformations:

<pre class="editor-mockup"><code>
2 &#42;&#42; 16
65536

", ".join(str(x) for x in range(5))
'0, 1, 2, 3, 4'

print(24&#42;"-")<span class="block-cursor"></span>
------------------------
</code></pre>

## Why run commands in the document?

Small commands and calculations often don't need a separate Terminal or Python REPL.

Inline commands keep the input, output, and surrounding context together as ordinary text.
You can edit the command, run it again, delete the result, or save everything with the document.

This works particularly well for notes, documentation, debugging, quick calculations, and lightweight executable documents.

