2

I often have long strings I want to break into new lines in various languages. E.g. in python

x = ("The quick brown fox jumps over the lazy dog")
                         /\ cursor here

becomes

x = ("The quick brown fox "
     "jumps over the lazy dog")

Or in javascript

var x = "The quick brown fox jumps over the lazy dog."
                            /\ cursor here

becomes

var x = "The quick brown fox " + 
        "jumps over the lazy dog."

Is there a way to do this in vanilla Emacs or Spacemacs?

3

The smartparens package provides the command sp-split-sexp. From the docs (for spacemacs)

sp-split-sexp is an interactive compiled Lisp function in ‘smartparens.el’.

It is bound to SPC j s, M-m j s.

(sp-split-sexp ARG)

Split the list or string the point is on into two.

If ARG is a raw prefix SPC u split all the sexps in current expression
in separate lists enclosed with delimiters of the current
expression.

See also setting ‘sp-split-sexp-always-split-as-string’ which
determines how sexps inside strings are treated and also for a
discussion of how to automatically add concatenation operators to
string splitting.

Examples:

  (foo bar |baz quux)   -> (foo bar) |(baz quux)

  "foo bar |baz quux"   -> "foo bar" |"baz quux"

  ([foo |bar baz] quux) -> ([foo] |[bar baz] quux)

  (foo bar| baz quux) -> (foo) (bar|) (baz) (quux) ;; SPC u

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.