Concatenate strings in elisp

EmacsElisp

Emacs Problem Overview


I need to concatenate path string as follows, so I added the following lines to my .emacs file:

(setq org_base_path "~/smcho/time/")
(setq org-default-notes-file-path (concatenate 'string org_base_path "notes.org"))
(setq todo-file-path (concatenate 'string org_base_path "gtd.org"))
(setq journal-file-path (concatenate 'string org_base_path "journal.org"))
(setq today-file-path (concatenate 'string org_base_path "2010.org"))

When I do C-h v today-file-path RET to check, it has no variable assigned.

What's wrong with my code? Is there any other way to concatenate the path string?

EDIT

I found that the problem was caused by the wrong setup, the code actually works. Thanks for the answers which are better than my code.

Emacs Solutions


Solution 1 - Emacs

You can use (concat "foo" "bar") rather than (concatenate 'string "foo" "bar"). Both work, but of course the former is shorter.

Solution 2 - Emacs

Use expand-file-name to build filenames relative to a directory:

(let ((default-directory "~/smcho/time/"))
  (setq org-default-notes-file-path (expand-file-name "notes.org"))
  (setq todo-file-path (expand-file-name "gtd.org"))
  (setq journal-file-path (expand-file-name "journal.org"))
  (setq today-file-path (expand-file-name "2010.org")))

Solution 3 - Emacs

First of all, don't use "_"; use '-' instead. Insert this into your .emacs and restart emacs (or evaluate the S-exp in a buffer) to see the effects:

(setq org-base-path (expand-file-name "~/smcho/time"))

(setq org-default-notes-file-path (format "%s/%s" org-base-path "notes.org")
      todo-file-path              (format "%s/%s" org-base-path "gtd.org")
      journal-file-path           (format "%s/%s" org-base-path "journal.org")
      today-file-path             (format "%s/%s" org-base-path "2010.org"))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionprosseekView Question on Stackoverflow
Solution 1 - Emacsoffby1View Answer on Stackoverflow
Solution 2 - EmacsJürgen HötzelView Answer on Stackoverflow
Solution 3 - EmacsOTZView Answer on Stackoverflow