Format a file(s) in-place from the command line?
eatobin opened this issue · comments
Question
I'm a new user trying to format a file in-place from the command line using hindent 5.3.1 and Stack.
This works fine:
$ echo 'example = case x of Just p -> foo bar' | stack exec -- hindent; echo
This too:
$ cat file.hs | stack exec -- hindent > file2.hs
(Now I have 2 files.)
But this:
$ cat file.hs | stack exec -- hindent > file.hs
does not format/overwrite the file as I had hoped. (file.hs
is now empty.)
How can I format/overwrite a file in-place from the command line?
How could I do this as a "batch" for my src
folder?
Thank you!
Sorry for the late reply.
First, if you want to format a file in-place with HIndent, you can simply do hindent foo.hs
.
Next, to format all Haskell source code in the src
directory with HIndent, you can run hindent src/**/*.hs
.
Finally, I will explain why $ cat file.hs | stack exec -- hindent > file.hs
does not work. This is not a HIndent-specific problem, but a general shell characteristic. When redirecting to write to a file, as in > file.hs
, the entire contents of the file are erased first. Thus, in the case of the above command, the entire contents of file.hs
are first erased and then passed to HIndent using cat
, resulting in an empty file.hs
.