How to see the current working directory in WinGHCi
Solution 1
System.Directory.getCurrentDirectory
from the directory
package.
Solution 2
I am not sure if this is the "right" way to do it, but since :!
allows shell commands, you can also get it with the appropriate shell command (of your OS) for reading out the directory or the content (so cd
, ls
and the likes). For example, you can write:
:! cd
It depends what do you need the directory for. If you just want to print it out in your console, then this can help.
All the best!
Solution 3
A poor man's solution to set the ghci prompt to the current working directory would be putting
let cur fill = do { cwd <- System.Directory.getCurrentDirectory; return (":set prompt \"" ++ cwd ++ fill ++ " \""); }
:def doprompt (\_ -> cur ">")
:def mycd (\dir -> System.Directory.setCurrentDirectory dir >> cur ">")
:doprompt
in the .ghci
file.
In the first line, we define cur :: String -> IO String
that gets the current directory and returns the ghci command to set the prompt accordingly. Then we define a ghci command doprompt
that performs that action and a command to change directory and set the prompt. The last line executes :doprompt
on startup.
Unfortunately, that doesn't make :cd dir
reflect the change of directory, one would have to do a manual :doprompt
afterwards, or use :mycd
to change the directory.
To avoid too long prompts, one could manipulate the result of getCurrentDirectory
by dropping an initial part of the file path.
Another drawback to that simple approach is that the prompt doesn't contain information about the loaded modules anymore. I believe all that could be overcome, but I'm not motivated enough to do the digging now.
Solution 4
In the ghci command prompt type :! cd
, it will give you the output for the current directory. In general, use :!
followed by shell commands to run any shell command in ghci command prompt. The shells are different on Windows and Mac/Linux, so the commands you can use will be different.
![Nawaz](https://i.stack.imgur.com/2WX9Z.jpg?s=256&g=1)
Nawaz
Following Rust and Haskell isocpp.org/wiki/faq Contact me on LinkedIn. Religion of C Correcting Grammar for Microsoft Products and Technology
Updated on June 16, 2022Comments
-
Nawaz about 2 years
I've started to teach myself Haskell, and for that I've installed The Haskell Platform for Windows. I'm using
WinGHCi
as of now. I learned that it has command:cd
which is used to change directory. But the question is, how would I know which directory I'm currently in? Without knowing that first why would I want to change directory. I searched a lot but couldn't find the answer.Please tell me if there is a way to know the current working directory. Preferably I would like to configure the command prompt itself to show the current directory, pretty much like Linux's Console.
Following @Daniel's suggestion, I did these:
- Since I worked on Windows 7, there is no
.ghci
file (I think it is for Unix-like OS), so I created a fileghci.conf
inC:\Users\Apelles\AppData\Roaming\ghc
folder, as it is instructed here. - Copy pasted the script from Daniel's answer to
ghci.conf
. - Then I started
ghci.exe
which is the console-like window. I noticed that it loaded few more modules than it usually used to load before. Here is the snapshot:
As you can see it loads more modules, and the last line says,
Can't parse prompt string. Use Haskell syntax.
What does it mean? Which line is causing problem (from the following script)?
let cur fill = do { cwd <- System.Directory.getCurrentDirectory; return (":set prompt \"" ++ cwd ++ fill ++ " \""); } :def doprompt (\_ -> cur ">") :def mycd (\dir -> System.Directory.setCurrentDirectory dir >> cur ">") :doprompt
Also, if I rename
ghci.conf
file to some random name, and then startghci.exe
, it loads these modules:As I said before, it loads less number of modules, which means with ghci.conf,
ghci.exe
does something successfully, but fails at some point. How to fix that? - Since I worked on Windows 7, there is no
-
Nawaz about 12 yearsLet me try this. For the time being, +1 for attempting to answer.
-
Nawaz almost 12 yearsI tried it on Windows, and it didn't work. I added things which I did, to my question. See it.
-
Daniel Fischer almost 12 yearsI'm sorry to hear that. Any indication what failed? (I see no edit to your question yet)
-
Nawaz almost 12 yearsSorry, it took time to edit my question. I tried to post as much information as I could. It is done now. :-)
-
Nawaz almost 12 yearsOops. I forgot to give +1 for the attempt. :|
-
Ryan almost 5 yearsUseful alternative, but should not be the preferred way to do this. This is a call to a language function, which does not help the user learn GHCi's commands. It's like if I asked how to type the letter
~
and you gave me the Alt-code (Type "Alt 126"), instead of telling me to hit the '~' key. -
sandwood over 4 yearscertainly the simplier one indeed