How to change SBCL's current directory?
Asked Answered
D

5

18

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?

Dover answered 12/10, 2011 at 5:31 Comment(0)
S
19
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"
Syllabary answered 7/10, 2013 at 8:55 Comment(3)
This doesn't seem to change the directory that sbcl looks in when you call (load ...).Testes
@Testes 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).Bouldon
you could just do (load (concatenate 'string (sb-posix:getcwd) "/" "my-foo.lisp")). Pretty clumsy, I know, but a little less brittle.Bouldon
P
16
(setf *default-pathname-defaults* #P"/New/Absolute/Path/")
Premier answered 11/7, 2016 at 19:10 Comment(0)
C
10

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))

Cadaver answered 20/5, 2017 at 14:47 Comment(2)
Note: uiop package is available in most CL implementations directly. No need to load using asdf / quicklisp. The above code will run directly.Advice
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 installedAssize
U
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"))
Uncut answered 12/10, 2016 at 0:40 Comment(0)
D
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.

Dover answered 23/10, 2011 at 3:35 Comment(1)
Is it simpler than use of chdir?Syllabary

© 2022 - 2024 — McMap. All rights reserved.