Strip the leading dollar sign when copying shell command #35615
Replies: 7 comments 2 replies
-
There's actually a dedicated syntax for shell-sessions: ```console
$ echo Foo
Foo
```The syntax highlighting being used even tokenises the prompt-symbol for the purposes of excluding it from copied code-snippets. However, GitHub have yet to make use of it... Note that ```console
# id -u
0
# # Comment 1
$ # Comment 2
``` |
Beta Was this translation helpful? Give feedback.
-
|
Thanks, I didn't know about I'll use it in my Markdowns so that I'll be ready when GitHub implements this feature. |
Beta Was this translation helpful? Give feedback.
-
|
Users can also solve this problem themselves via their own config. For example, Zsh users can create a dirt simple pass-thru dollar function like so: # .zshrc
function \$ { "$@"; }Bash users can't make a function called # .bashrc
ble/function#advice around ble/widget/default/accept-line '
if [[ "${_ble_edit_str:0:2}" == "$ " ]]; then
ble/widget/beginning-of-logical-line
ble/widget/insert-string "${_ble_edit_str:2}"
ble/widget/kill-forward-logical-line
fi
ble/function#advice/do
'And in Fish, you can add your own paste scrubber: function fish_paste_scrubber
# run the Fish default
fish_clipboard_paste
# remove any leading dollar signs from the pasted command
commandline -r (string replace -r '^\$ ' '' (commandline))
endThen you have to bind it to your paste key sequence in function fish_user_key_bindings
bind \cv fish_paste_scrubber
# ... other keybindings ...
endIt'd still be a good idea to implement this feature. Just thought it worth mentioning this can be solved on the user end too. Since things like GitLab and BitBucket and blogs and articles etc exist, users will forever be annoyed by this problem if they don't apply a fix themselves. |
Beta Was this translation helpful? Give feedback.
-
|
Interesting I don't think that can be omitted, thought you can still just modify it in a way to make look prettier. |
Beta Was this translation helpful? Give feedback.
-
|
This discussion could be made a bit more general, as I’m pretty sure AsciiDoc in GitHub has the same issue. Some parts of their code are probably language-agnostic. By the way, GitHub’s “Copy” button on snippets from pages written in AsciiDoc copy the callout numbers ( |
Beta Was this translation helpful? Give feedback.
-
|
It's a simple add that makes a difference. I have worked in a proprietary documentation environment that elided dollar sign prompts in Copy button text if the Markdown code block was type bash, shell, etc. We used this a lot for instructions to perform common on-call tasks, where getting things right without hassle was important. |
Beta Was this translation helpful? Give feedback.
-
|
Years have passed, and there's still no good solution. I'll write down all that I have tried. This is such a tiny and stupid problem that GitHub doesn't care about it. Let's say I want to show in my README.md how to run a command. I use the $ echo "XD"The "copy" button is now broken because it copies the dollar sign. But there's another problem. I also want to show the output of running the command: $ echo "XD"
XDThe above is okayish, but what if I a long command invocation that is multiline? $ echo "XD" \
--some-flag
XDIt's hard to see where the flags end and where the output begins. (Of course, I can find the last Of course, these are toy examples, but I consider the core problem to be unsolved. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
When writing READMEs (or basically any Markdown) I really like to prefix the commands that I want the reader to copy-paste into their terminal with a dollar sign
$.For example, I prefer this:
to this:
Unfortunately, prefixing the commands with the dollar sign makes it harder for the reader to use the built-in Copy button that GitHub displays by default on the right side of the code snippet.
This often results in small, annoying mistakes:
I'd like the leading dollar sign to be automatically stripped when copying the snippet. Maybe it should happen only when the type of the code snippet is
bashorsh– I don't know.A site that already does this is Google's docs. See example here.
Beta Was this translation helpful? Give feedback.
All reactions