12

Is it possible to write a single line for an Elisp string:

"hello world"

As:

"hello
world"

Without inserting a newline character? (wrapping a string literal).

I read that adding a tilde at the end of the line does this in some lisp's, but its not working for me.

NickD
  • 27,023
  • 3
  • 23
  • 42
ideasman42
  • 8,375
  • 1
  • 28
  • 105

3 Answers3

14

Yes, you can use a literal string that does not include the newline, so that the newline shows only for your code.

Use \ to escape the newline, so that it is not included in the string:

(setq foo "hello \
world")

C-h v foo

"hello world"

One place where this is often used is in a doc string, where the literal string uses \\<...> or \\[...] constructs that make the doc string look long in the source code. When rendered as part of the help output the result might show a short key sequence, so you use \ to chop the literal string in the code but keep it all on one line in the help output.

For example, this doc-string sentence is all on one line, which appears as You can use this command only from the minibuffer (‘C-%’). in the help output. But in the source code does not appear as a single, long line - there is a newline after the \.

"...
 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
 \\[icicle-candidate-set-swap]')."
Drew
  • 75,699
  • 9
  • 109
  • 225
6

Edit, it seems multi-line literals are supported, keeping answer since it may be useful still.

You can however use concat. eg:

(concat "hello"
 "world")
ideasman42
  • 8,375
  • 1
  • 28
  • 105
1

have you considered to use define-skeleton to insert predfined bloc of texts and more ? Emacs Skeleton

Stefan
  • 26,154
  • 3
  • 46
  • 84
Clement
  • 11
  • 1