I want this ("start" "--color=always" "--exclude=*pdf" "end") but I am getting this ("start" ("--color=always" "--exclude=*pdf") "end").

Here are the things that I have tried. I have also tried split-string but that also returns a list. I need something that returns strings so that I can send them as arguments to list.

#+begin_src emacs-lisp :results output
(print (list "start"   (dolist (x '("--color=always" "--exclude=*pdf" "--exclude=*tex"))  (concat x) "end"))

: ("start" nil "end")

#+begin_src emacs-lisp :results output
(print (list "start" (mapcar (lambda(x)(concat x))(split-string "--color=always --exclude=*pdf")) "end"))

: ("start" ("--color=always" "--exclude=*pdf") "end")

Not sure why I get two ':' in the results.

Update by OP:

Although both answers work, flatten-tree is what I needed and the easiest way. The other answer works as well but had to do some modifications (see commented code).

  (list consult--fd-command ;; `(,consult--fd-command)
        my/fd-flags ;; my/fd-flags ; '("--color=never" "--extension=org")
       (consult--join-regexps re 'extended))) ;; `(,(consult--join-regexps re 'extended))
  • @dalanicolai Hello, there's a lot of complex code that creates "start" and "end". It would be a long post if I included all the code but basically trying to set the flags in this: (when re (cons (append (list consult--fd-command "--color=never" (consult--join-regexps re 'extended)) opts) hl))))
    – ritchie
    Feb 16, 2023 at 16:55
  • That does not make your goal much clearer:) Anyway, I already deleted my comment, because I figured you wanted to 'splice' the 'inner' list. If that is not what you mean, then please try to explain again what you try to achieve (please by editing your question, and not via a comment. Thanks!) Feb 16, 2023 at 17:02
  • emacs.stackexchange.com/tags/elisp/info
    – Drew
    Feb 16, 2023 at 18:41

2 Answers 2


You need:

(flatten-tree TREE)

Return a "flattened" copy of TREE.

(flatten-tree '(1 (2 3) 4))
;; ⇒ (1 2 3 4)
(flatten-tree '(1 
;; ⇒ (1 2 3 4 5 6 7)


To complete the answer: The two :'s are printed because print always adds a newline before the output (see here. You could replace print with prin1 or you could use :results raw instead.

So the following code would achieve what you ask for:

#+begin_src emacs-lisp :results output
(prin1 `("start" ,@(mapcar (lambda(x)(concat x))(split-string "--color=always --exclude=*pdf")) "end"))

To evaluate the 'complex code' that produce "start" and "end" (as you mention in the comment), you can add additional ,'s in front of those expressions (again see backquote).


Do you mean that you want to 'splice' the list withing the 'surrounding' list?

You can achieve it either via append:

(append '("start") '("--color=always" "--exclude=*pdf" "--exclude=*tex") '("end"))

or you can use backquote syntax:

`("start" ,@'("--color=always" "--exclude=*pdf" "--exclude=*tex") "end")
  • Your ideas gave me the answer I needed that is correct. Working on posting the final solution. I think I will edit the answer to post the solution or comment. It's been a while since I've asked a question lol. The solution was to use append with multiple arguments.
    – ritchie
    Feb 16, 2023 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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