As a Nushell user, I often get frustrated when copy-pasting commands from a website into my terminal. The typical use-case I encounter is the Github CI spitting out the failing tests with a command I can run in my terminal, like this one:
bundle exec rspec \
'./spec/requests/api/v1/campaigns/show_spec.rb[1:1:1:1:1]' \
'./spec/requests/api/v1/campaigns/show_spec.rb[1:1:1:2]' \
'./spec/requests/api/v1/campaigns/show_spec.rb[1:1:1:3:1]' \
'./spec/requests/api/v1/campaigns/show_spec.rb[1:1:1:4:1]'Running this in nushell will give you an error like:
Error: nu::parser::parse_mismatch
× Parse mismatch during operation.
╭─[repl_entry #3:2:62]
1 │ bundle exec rspec \
2 │ './spec/requests/api/v1/campaigns/show_spec.rb[1:1:1:1:1]' \
· ┬
· ╰── expected operator
3 │ './spec/requests/api/v1/campaigns/show_spec.rb[1:1:1:2]' \
╰────This happens because \ is not recognized as an escape character in Nushell.
The temporary workaround I found was to drop to zsh and run the command. But it was not an acceptable, long-term solution.
So here is a little snippet I found on Github to solve that issue. I added this to my config.nu file, in the keybindings array:
{
name: "replace_slash_line_break_and_run"
modifier: "control"
keycode: "enter"
mode: [emacs, vi_normal, vi_insert]
event: [
{
send: executehostcommand,
cmd: '
mut cmd = (commandline)
let $line_break_regex = '\\\n'
$cmd = $cmd | str replace --all --regex $line_break_regex ''
commandline edit $"($cmd)" --accept
'
},
]
}So now, I just paste the multiline snippet, hit control-enter and voilà!
All credits go to Rondimesquita
