12

In questions and answers, I would sometimes like to keep a whole string together, to avoid what's happening to the string [![:print:]] here:

bad hyphenation example

Is there a trick to avoid breaking a short string?


In an earlier revision of this question, I mentioned non-breaking spaces. This seemed to derail people's attention (hence the comments below). This question is about preventing word-breaking, not about how to do non-breaking spaces.

11
  • 2
    Side note - if you don’t feel like pressing Alt + Space then typing   works as well. – Jonny Henly Jan 4 '20 at 7:40
  • @JonnyHenly Not within backticks for an in-line code string (or triple backticks for a block). It does work if you use it within a <code>...</code> HTML tag, which I never use as I write lots of small code snippets. Every once in a while, that might be useful though. – Kusalananda Jan 4 '20 at 7:50
  • 1
    I see your point. I’m afraid your best option is to use block formatting rather than inline. Though this should only pertain to exact syntax situations where the incorrect reading of inline hyphenation could lead to undesired behavior. – Jonny Henly Jan 4 '20 at 8:00
  • @JonnyHenly Hm? Your comment was about non-breaking spaces, not about hyphenation, right? In-line code marked up with <code> is still hyphenated at awkward positions. My question is about how to prevent hyphenation of in-line text/code. If there was something that could be done with HTML attributes in the <code> tag, for example, that would be nice. – Kusalananda Jan 4 '20 at 8:07
  • 1
    As for Not within backticks for an in-line code string in the comment above: please don't add non-breaking spaces in code. It will confuse people a lot when copy/pasting that code. – Arjan Jan 4 '20 at 8:08
  • @Arjan How would I then best show a string with some trailing spaces in a paragraph, as I did in this question? Note that this is almost never in code that anyone would copy. This is for text talking about code. – Kusalananda Jan 4 '20 at 8:11
  • The non-breaking space thing that I mentioned in passing seems to derail people's attention. This question is not about how to do non-breaking spaces. – Kusalananda Jan 4 '20 at 8:13
  • Use block formatting - either indent the code with four spaces, use triple ticks or go the html route with <pre><code>...</code></pre>. – Jonny Henly Jan 4 '20 at 8:13
  • @JonnyHenly I don't think that would be appropriate for the example that I show in the question where I talk about a filename globbing pattern. Using a pre+code block for each [![:print:]] (or even one of them) would make the text confusing and unreadable. – Kusalananda Jan 4 '20 at 8:20
  • I agree with you, it’s better to keep those inline. But for situations in which spacing is part of the syntax (i.e. makefiles, sh builtins, ...) it’s better to block format that code. Basically inline code is going to be broken up or awkwardly placed on a new line if it cannot fit, you can try adding <br>s to make it work but it’s going to look weird on other people’s displays. – Jonny Henly Jan 4 '20 at 8:30
  • @JonnyHenly I'm well aware how code block formatting works. This is not an issue. This is about span elements, i.e. strings in text in flowed paragraphs (see example in question). – Kusalananda Jan 4 '20 at 8:32
10

To prevent the word-break

In questions and answers, I would sometimes like to keep it together, to avoid this to the string [![:print:]] happening

Use a &#8205; between all characters, which is a Zero-width joiner

In questions and answers, I would sometimes like to keep it together, to avoid this to the string ‍[‍!‍[‍:‍print‍:‍]‍]‍ happening

Above in Markdown:

> In questions and answers, I would sometimes like to keep it together
> , to avoid this to the string 
> <code>&#8205;[&#8205;!&#8205;[&#8205;:&#8205;print&#8205;:&#8205;]&#8205;]&#8205;</code> happening

Far from convenient to create (or use later on (copy/paste)) but that was not stated as a requirement.

5
  • Urgh. Yeah, far from convenient, but possibly still useful under under some limited circumstances. – Kusalananda Jan 4 '20 at 10:51
  • Or use it as code on a separate linr, that also doesn't break. As per your markdown piece. – Luuklag Jan 4 '20 at 12:45
  • 1
    I feel adding zero-width joiners will confuse people who copy the code. It might also affect search. (And aside, this is optimizing for some views, typically the regular desktop browser, but not for others such as the mobile view or any usage of the data dumps, which might flow the text differently.) Related, the auto-inserted magic in comments, that confused people too: Occasionally, the Unicode character sequence U+200C U+200B (ZWNJ ZWSP) is inserted into comments. – Arjan Jan 4 '20 at 12:47
  • 1
    If I copy and paste your example with the zero-width joiner in it, that invisible character can stay as part of the pasted text, possibly resulting in some strange, bizarre, or hard to understand errors, depending on how that character is handled. – 1201ProgramAlarm Jan 4 '20 at 16:32
  • 3
    @1201ProgramAlarm and we all love those errors, right? – rene Jan 4 '20 at 17:13
8

Not sure if this qualifies as an answer, but I think you should not care too much about this, because you can't completely prevent these kind of things happening. Perfecting your posts is commendable, but in this case your energy might be better spent elsewhere, e.g. finding and writing an answer to another question.

For a real-life example, this very morning, I was Googling for how to check the log of launchctl. The first search result is a Server Fault answer and it's short enough that I don't need to navigate:

So I copy

sudo launchctl log level debug

and execute it, and then I copy

tail -f /var/log/system.log

and execute it. Or so I thought; since Google strips the formatting, that actually ended up being

tail -f /var/log/ system.log

thanks to the line break. The author of that question wisely chose a code block for their commands, but even that didn't help.

1
  • 1
    Above at least the whitespace was visible after pasting. Zero-width fillers, like suggested elsewhere, would add far more confusion. – Arjan Jan 4 '20 at 14:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .