2

As part of learning Haskell, for fun I'm attempting to use Raspberry PI. Having encountered a myriad of issues installing ghci on the PI I've resolved to using just ghc.

So to create, compile & run a new Haskell file :

vi first.hs
i
main = putStrLn "First"
Esc
:w
:q
ghc -o first first.hs
./first

Output is : "First"

I would like to automate the commands :

Esc
:w
:q
ghc -o first first.hs
./first

Can these be added as new command from within vi / vim, something like :

:mycustomcommands

And run from within the vi / vim editor ?

blue-sky
  • 51,962
  • 152
  • 427
  • 752
  • vi or have you access to vim ? – Jaffa Jul 22 '15 at 09:39
  • @Geoffroy yes, can use vim also – blue-sky Jul 22 '15 at 09:47
  • In addition to cxw's answer, you can run shell commands from vim with `:!` - e.g. `:!ghc -o first first.hs` – duplode Jul 22 '15 at 09:50
  • 1
    It is indeed tricky to get a full GHC with interactive/TH compiled on the Rπ (I did manage it, but [don't ask me how](http://stackoverflow.com/questions/29379706/ghci-on-raspberry-pi-2)). But apparently there are now ready binaries [in Debian-unstable](http://www.reddit.com/r/haskell/comments/35bw0b/at_last_debian_unstable_has_working_arm_ghci_and/), with those it should be easy enough. – leftaroundabout Jul 22 '15 at 12:09
  • @leftaroundabout can you point me where to get copy of Debian-unstable image for pi ? – blue-sky Jul 22 '15 at 16:55
  • @leftaroundabout yes, that did the trick, thanks. To install change /etc/apt/sources.list to deb http://http.debian.net/debian sid main contrib non-free sudo apt-get update sudo apt-get upgrade sudo apt-get install ghc sudo apt-get install cabal-install. Error during installation , needed to run : gpg --keyserver pgpkeys.mit.edu --recv-key 8B48AD6246925553 gpg -a --export 8B48AD6246925553 | sudo apt-key add - gpg --keyserver pgpkeys.mit.edu --recv-key 7638D0442B90D010 gpg -a --export 7638D0442B90D010 | sudo apt-key add - – blue-sky Jul 22 '15 at 18:19
  • ref for previous comment in question content of : http://stackoverflow.com/questions/30201508/why-cant-i-install-any-packages-with-ghc-7-8-4-on-raspberry-pi – blue-sky Jul 22 '15 at 18:21

3 Answers3

2

Maybe you could try adding something like this to your vimrc:

function! ExecuteHS()
    w
    !ghc -o first %
    !./first
endfunction

And to use this function you just have to call it like that :call ExecuteHS(). Vim will be put on background during the execution of your file and will then come back on foreground at the end of the execution.

As a bonus you can add the folowing line to your vimrc

nnoremap <key> :call ExecuteHS()<CR>

Replacing <key> with your prefered key combination <Leader>e for example. This way you'll simply have to hit ,e (if you didn't changed your leader key) in normal mode to call the function.

That's probably not the cleanest way to do it but it should work for what you want.

statox
  • 2,827
  • 1
  • 21
  • 41
  • 1
    Also for your questions concerning vim and its usage you could be interested in [the dedicated stackexchange site](http://vi.stackexchange.com/) where answers are pretty fast ;-) – statox Jul 22 '15 at 13:28
1

Absolutely in vim, though not necessarily in other vi flavors. See this tutorial on defining custom commands. Put the custom command in your vimrc and it will always be available as :Customcmd or whatever you call it. For one-button access, you can use :remap to assign a hotkey to your custom command or the sequence of built-in commands you want to run. This is a tutorial on keymappings that will give you more information.

I second @statox's referral to https://vi.stackexchange.com :)

cxw
  • 16,685
  • 2
  • 45
  • 81
0

I use vim-haskell, which includes a couple nice things. In particular, it includes a file for setting up cabal-install as the compiler, which is a very nice way of working. Dump this in ~/.vim/compiler/cabal-build.vim:

CompilerSet makeprg=cabal\ build
CompilerSet errorformat=
            \%W%f:%l:%c:\ Warning:%m,
            \%W%f:%l:%c:\ Warning:,
            \%E%f:%l:%c:%m,
            \%E%f:%l:%c:,
            \%C\ \ %#%m,
            \%-G%.%#,
            \%-G%.%#

And this in ~/.vim/ftplugin/haskell.vim:

compiler cabal-build

(The argument to compiler should match the name of the file you put in ~/.vim/compiler.) Then you can run :make in vim and it will save any changed buffers (assuming autowrite is set) and build your project. When there are errors, it will populate the quick-fix list, which lets you jump to the specific file and line numbers of each error or warning with a key. Read more about this feature with :help quickfix. Once everything is working, you can :!cabal run to run it.

Daniel Wagner
  • 145,880
  • 9
  • 220
  • 380