---
title: Copy result from LLDB to clipboard
slug: copy-result-from-lldb-to-clipboard
createdAt: 2021-07-01T17:59:30.000Z
---

Did you need to copy the result of `po` command to clipboard? Well, I needed, and it was a JSON of 1000+ lines. It was so painful to select everything from the LLDB console.

Thankfully, there's an option to create a custom LLDB command using Swift!

# Using Swift code in LLDB

```shell
expr -l Swift -O -- [Swift code]
```

`expr -l Swift -O --` will tell LLDB to interpret everything after `--` as Swift code.

```shell
expr -l Swift -O -- print("Hello from LLDB")
```

## Creating LLDB `copy` command

First, we need to create a custom LLDB command that can be used from the debug session.

To create custom command execute following in LLDB console

:::CodeblockTabs
LLDB

```shell
command regex copy 's/(.+)/expr -l Swift -O -- UIPasteboard.general.string = "\(%1)"/'
```
:::

Doing it this way will make this command available for the current running LLDB session. As soon as the session is terminated, command won't be available.

To make the command available for all LLDB sessions, we need to add it to `~/.lldbinit` file. To create LLDB `copy` command, execute the following in terminal.

```shell
echo "command regex copy 's/(.+)/expr -l Swift -O -- UIPasteboard.general.string = \"\(%1)\"; print(\"Result is copied to clipboard\")/'" >> ~/.lldbinit
```

If we try to run a `copy` command in LLDB session that is already running, it will show the following error.

![](https://api.archbee.com/api/optimize/VU9phOpDtRwSx5Irpi731/vnGvxgZf0ZCDiexzKCijP_image.png)

We can re-run application from Xcode. Or, we can reload it by executing following from LLDB console.

:::CodeblockTabs
LLDB

```shell
command source ~/.lldbinit
```
:::

### Using `copy` command

`copy` command will use `String` representation from what is passed.&#x20;

### Examples

Copying value from `UserDefaults`

:::CodeblockTabs
LLDB

```shell
copy String(data: UserDefaults.standard.data(forKey: "key")!, encoding: .utf8)!
```
:::

***

Copy object description

```swift
struct Person {
    let name: String
    let email: String
}

let person = Person(name: "Test Person", email: "test.person@email.com")
```

:::CodeblockTabs
LLDB

```shell
copy person
```
:::

And we will have the following text in our clipboard

```none
Person(name: "Test Person", email: "test.person@email.com")
```

:::hint{type="warning"}
Autocomplete is not working with custom LLDB commands. Simple workaround is to use po and then change it with copy.&#x20;
:::

# References

- [https://swiftsenpai.com/testing/add-custom-lldb/](https://swiftsenpai.com/testing/add-custom-lldb/)



