How to see the current working directory in WinGHCi

16,082

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.

Share:
16,082
Nawaz
Author by

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, 2022

Comments

  • Nawaz
    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 file ghci.conf in C:\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:

    enter image description here

    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 start ghci.exe, it loads these modules: enter image description here

    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?

  • Nawaz
    Nawaz about 12 years
    Let me try this. For the time being, +1 for attempting to answer.
  • Nawaz
    Nawaz almost 12 years
    I tried it on Windows, and it didn't work. I added things which I did, to my question. See it.
  • Daniel Fischer
    Daniel Fischer almost 12 years
    I'm sorry to hear that. Any indication what failed? (I see no edit to your question yet)
  • Nawaz
    Nawaz almost 12 years
    Sorry, it took time to edit my question. I tried to post as much information as I could. It is done now. :-)
  • Nawaz
    Nawaz almost 12 years
    Oops. I forgot to give +1 for the attempt. :|
  • Ryan
    Ryan almost 5 years
    Useful 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
    sandwood over 4 years
    certainly the simplier one indeed