Copy result from LLDB to clipboard
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
expr -l Swift -O -- [Swift code]expr -l Swift -O -- will tell LLDB to interpret everything after -- as Swift code.
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
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.
echo "command regex copy 's/(.+)/expr -l Swift -O -- UIPasteboard.general.string = \"\(%1)\"; print(\"Result is copied to clipboard\")/'" >> ~/.lldbinitIf we try to run a copy command in LLDB session that is already running, it will show the following error.

We can re-run application from Xcode. Or, we can reload it by executing following from LLDB console.
command source ~/.lldbinitUsing copy command
copy command will use String representation from what is passed.
Examples
Copying value from UserDefaults
copy String(data: UserDefaults.standard.data(forKey: "key")!, encoding: .utf8)!Copy object description
struct Person {
let name: String
let email: String
}
let person = Person(name: "Test Person", email: "[email protected]")copy personAnd we will have the following text in our clipboard
Person(name: "Test Person", email: "[email protected]")Autocomplete is not working with custom LLDB commands. Simple workaround is to use po and then change it with copy.
References