15

It is very easy to change CLisp's current working directory:

>cat ~/.clisprc.lisp 
;;; The following lines added by ql:add-to-init-file:
#-quicklisp
(let ((quicklisp-init (merge-pathnames "quicklisp/setup.lisp" (user-homedir-pathname))))
  (when (probe-file quicklisp-init)
  (load quicklisp-init)))

(cd "/media/E/www/qachina/db/doc/money")
(load "money")

However, it seems there is no cd similar function in SBCL. How can this be done with SBCL?

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
z_axis
  • 8,272
  • 7
  • 41
  • 61

5 Answers5

18
CL-USER> (sb-posix:chdir "/home/apugachev")
0
CL-USER> (sb-posix:getcwd)
"/home/apugachev"
CL-USER> (sb-posix:chdir "/tmp/")
0
CL-USER> (sb-posix:getcwd)
"/tmp"
peroksid
  • 890
  • 6
  • 17
  • 9
    This doesn't seem to change the directory that sbcl looks in when you call `(load ...)`. – daveloyall Apr 08 '15 at 23:40
  • 5
    @daveloyall I think that observation is worth its own top-level question. It's puzzling behavior and I don't know the answer, either (I just work around it by using full path names all the time). – Reb.Cabin Jan 23 '16 at 23:30
  • you could just do `(load (concatenate 'string (sb-posix:getcwd) "/" "my-foo.lisp"))`. Pretty clumsy, I know, but a little less brittle. – Reb.Cabin Feb 06 '16 at 18:26
13
(setf *default-pathname-defaults* #P"/New/Absolute/Path/")
xor-xor
  • 70
  • 5
Kripa R
  • 131
  • 1
  • 2
9

Right now, better answer is: use (uiop:chdir "some/path").

Or you can use this function to change directory temporarily:

(uiop:call-with-current-directory "some/path" (lambda () (do-the-job))

Or this macro for more convenient way:

(uiop:with-current-directory ("some/path") (do-the-job))

Alexander Artemenko
  • 21,378
  • 8
  • 39
  • 36
  • Note: uiop package is available in most CL implementations directly. No need to load using asdf / quicklisp. The above code will run directly. – Capstone Jun 06 '19 at 08:16
  • 1
    I'm using SBCL 2.0.0 in Windows and had to `(require "asdf")` to use uiop. It's the case that nothing had to be installed – Heitor Chang Aug 22 '20 at 23:22
8

Had the same question. Turns out

(setf *default-pathname-defaults* (truename "./subdir"))

changes load path to subdir. If you don't want relative path, then

(setf *default-pathname-defaults* (truename "/absolute/path"))
xor-xor
  • 70
  • 5
Gerard Domek
  • 89
  • 1
  • 6
0

Now i use rlwrap to run SBCL and solves this problem

$"cat ~/bin/sb"
breakchars="(){}[],^%$#@\"\";:''|\\"

cd /media/E/www/qachina/db/doc/money/
exec rlwrap --remember -c -b "$breakchars"  -f "$HOME"/.sbcl_completions sbcl "$@"

then run sb.

z_axis
  • 8,272
  • 7
  • 41
  • 61