8

I'm trying out Haskell in GHCi on Windows. In the tutorial I'm following, a function name is enclosed in backticks: x `mod` 7. However, I can't enter the character in GHCi.

The backtick character has ASCII value 096, but GHCI does not allow Alt + number to insert any character. Copying/pasting doesn't work either. What should I do?

Martin
  • 1,430
  • 10
  • 19
  • I'm not sure if I'm understanding the problem correctly, but the backtick character is typically located on a key immediately to the left of the "1" key on most keyboards: http://i.stack.imgur.com/TOn1U.png. – David Young Sep 26 '15 at 19:04
  • My version of GHCi accepts the backtick directly from my keyboard (although my keyboard isn't EN-US), and also accepts it from pasting. If you're using Windows, run GHCi on Powershell instead of Prompt to use Ctrl+V for pasting. – Mephy Sep 26 '15 at 19:05
  • 4
    Have an Italian keyboard, has a \ left of 1, sorry – Martin Sep 26 '15 at 19:05
  • If you don't find another approach, you can always use GHCi through emacs, which has some other, more substantial, benefits. – dfeuer Sep 26 '15 at 19:08
  • 3
    This SuperUser question might help: [Italian keyboard: entering the tilde (~) and backtick (`) characters without changing keyboard layout](http://superuser.com/q/667622/320921). – duplode Sep 26 '15 at 19:08
  • 1
    Well, one thing to note is that backticks are never *necessary*. ``x `mod` 7`` is the same as `mod x 7`. Backticks are just a way to use a function in an infix manner. Also, these options don't sound particularly convenient, but here is some info on typing backticks on Italian keyboards: http://superuser.com/questions/598135/type-backtick-on-windows-italian-keyboard – David Young Sep 26 '15 at 19:08
  • @mephy Powershell works as Cmd, no ctrl/v and no backticks in GHCi. Outside GHCi those characters are insertednormally. It's GHCi that "eats" them, it seems – Martin Sep 26 '15 at 19:11
  • I know how to add ascii characters, that's not the question. It's about how to convince GHCi not to ignore them when I type them! GHCI seems to ignore quite a bit of what is typed in; all CTRL/*, all mouse clicks, all ALT/*** characters. – Martin Sep 26 '15 at 19:16
  • Really, once you've worked with Haskell in a properly configured emacs (a bit tricky to set up, I'll admit), even the pain of emacs will seem small compared to the benefits you get from near-real-time feedback from flycheck, HLint, etc. And mapping something to backtick will be pretty easy. – dfeuer Sep 26 '15 at 19:17
  • 2
    I would guess that this is more a problem of keyboard layouts + terminal encoding and not so much with `ghci` itself. – JustGage Sep 26 '15 at 19:19
  • Everybody seems to think of it as a windows problem, it is NOT: it is GHCI that ignores a character typed correctly via the num-keypad. Guess I'll vave to follow David's advice and make changed in the tutorial examples. Thank you all. – Martin Sep 26 '15 at 19:23
  • @Martin Have you tried using WinGHCi? I think that still comes with the Haskell Platform and it has a GUI interface rather than using cmd. – David Young Sep 26 '15 at 19:23
  • Off on a tangent: Do Ctrl+Insert and Shift+Insert work for copy-pasting in PowerShell+GHCi? (I vaguely remember having done that in the past.) – duplode Sep 26 '15 at 19:24
  • 1
    Whether a command window supports copy&paste is independent of the shell. You can activate copy&paste (and other good stuff) in the command window's setting dialog, accessed from it's system menu: left-click on the window icon in the top-left corner of the window, select settings. – Toxaris Sep 26 '15 at 19:27
  • @Toxaris : It's NOT a windows problem, it's a GHCi problem – Martin Sep 26 '15 at 19:33
  • @Martin: Hmm. I can confirm that "paste by Ctrl-V" doesn't work in ghci. However, I just tried "paste by right mouse click" in a ghci running in a Windows command window and it works fine. ghci 7.8.3, Windows 8.1 – Toxaris Sep 26 '15 at 19:36
  • Use Win7, right click does not show the usual menu Select, paste, etc. not in Cmd, not in Powershell – Martin Sep 26 '15 at 19:38
  • 1
    I guess the underlying problem is that ghci uses readline (or maybe some Haskell port of readline, I forgot), which defines its own CTRL-Whatever key combinations. Maybe readline can be configured to give you a key combination to insert a backtick? – Toxaris Sep 26 '15 at 19:39
  • Did you activate "quick edit mode" and "paste mode" in the command window's settings? I'm pretty sure they existed for at least a couple of Windows versions now. – Toxaris Sep 26 '15 at 19:40
  • 2
    Found WinGHCi after all, (it's not in the BIN map, so not on the PATH) and it works. @David Young: formulate the answer and I'll mark it as correct – Martin Sep 26 '15 at 19:41
  • @Martin Well, the existing answer recommends using WinGHCi so I would suggest marking that as correct. – David Young Sep 26 '15 at 19:42

2 Answers2

10

I don't know how send a backtick to ghci if your keyboard doesn't have it. But this little technical issue shouldn't stop you from trying out Haskell!

  • You can program in Haskell without backticks by using f a b instead of a `f` b. In your example, mod x 7.

  • You can set your keyboard layout to us-american for programming. While it takes some practice to find the keys with the wrong labels on the physical keyboard, it can be convenient to have [, ], { and } more accessible, and in your case, have the backtick at all.

  • You can paste the backtick from the clipboard. (Activate "quick-edit mode" and "paste mode" from the command window's settings, and then use the right mouse button to paste).

  • Maybe using a wrapper around the command window helps? I sometimes use Console2 and sometimes M-x shell in Emacs.

  • Maybe WinGHCi helps? It supports copy and paste, at least.

  • See Ørjan Johansen's answer for how to add a custom keybinding to ghci.

Good luck with Haskell!

Toxaris
  • 7,156
  • 1
  • 21
  • 37
  • The windows console doesn't support CTRL-V for pasting (in fact, it doesn't support any keyboard shortcut for pasting at all) - except on Windows 10, it seems: http://stackoverflow.com/questions/131955/keyboard-shortcut-to-paste-clipboard-content-into-command-prompt-window-win-xp/31506358#31506358 – melpomene Sep 27 '15 at 01:09
  • Really? How annoying. I use CTRL-V with the command prompt in my Windows console all the time, but I now realized it is provided by http://mridgers.github.io/clink/ not the vanilla console or command prompt. Sorry for my confusion. – Toxaris Sep 27 '15 at 14:26
6

@Toxaris gave several suggestions in his answer, but in a comment he also mentioned the possibility of configuring GHCi's version of readline (which is called haskeline). And indead it allows custom keybindings. So I decided to test that out.

The documentation says to put the customizations in ~/.haskeline, which is not a Windows path, but it presumably translates to .haskeline in whatever directory System.Directory.getHomeDirectory returns. (vim can also edit that path directly on Windows, but I only realized that afterwards.) So I put the following in C:\Users\Ørjan\.haskeline:

bind: meta-' `

Now I can get ` in GHCi by pressing alt-' !

Ørjan Johansen
  • 18,119
  • 3
  • 43
  • 53
  • 1
    Sorry to have indicated the correct answer already: YOURS is what I was looking for. Put it in what Windows calls HOMEPATH (c:\users\myname). Txs. – Martin Sep 27 '15 at 09:08
  • @Martin I think you can change which answer you accept. I'm happy that we solved your issue. – Toxaris Sep 27 '15 at 14:33