Question

I try to keep my fingers on home row as much as possible.

Typing all the parentheses makes me move away from there a fair bit.

I use Emacs; the parentheses themselves are no issue, I'm comfortable with them. And I don't like modes that type them for me automatically.

I've thought about remapping the square brackets to parentheses and vice versa. Is this a good idea? What does everyone else do?

Was it helpful?

Solution

I would personally recommend the lethal combo of Emacs, SLIME & paredit.el Paredit allows you to pseudo-semantically edit the LISP code at sexp level, and that makes the parentheses disappear almost completely. You can move around sexps as if they are simple words and even mutate them with just a couple of key-strokes. There is also a minor mode called Redshank which works in conjunction with Paredit and that provides more power.

Consider simple these examples (| is the position of the cursor):

(foo bar (baz| quux) zot) + C-( => (foo (bar baz| quux) zot)
(foo (bar |baz) quux zot) + C-) => (foo (bar |baz quux) zot)

(foo (bar baz |quux) zot) + C-{ => (foo bar (baz |quux) zot)
(foo (bar |baz quux) zot) + C-} => (foo (bar |baz) quux zot)

I have done some serious Common Lisp programming in my time, and paredit has proved to be invaluable to me. I can't even think of writing non-trivial LISP code without my toolbox. And after all that's the way it should be; you should never have to count or match your parentheses... once you master Paredit, the parentheses will just disappear from in front of your eyes.

Remapping the [] to () will help, but you will stop caring much after you start using Paredit.

Feel free to use the LISP specific portions from my dot-emacs.

OTHER TIPS

With many non-US keyboard layouts, typing square brackets or braces is even more cumbersome than typing parentheses, anyway, which makes programming in most languages very strainful, so consider yourself lucky. ;)

As for me, I use a programmer-friendly non-standard keyboard layout that lets me type parentheses via [Super]-j and [Super]-k.

I take my fingers off the home keys....

I have foot pedals. LeftFoot = open paren, RightFoot = close paren.

Well, I don't, but I don't use Lisp. It doesn't seem like a bad idea, though.

Could you imagine a variation on Lisp that used indentation instead of parens? (taking a page from the Python spec)

I tried remapping in Emacs, but it creates new problems: say you're editing in a terminal window through ssh and you paste a snippet into the window; then parens and brackets get swapped in your pasting, not just your typing. If you try this, remap at a lower level in your system, like xmodmap.

(Of course, the obvious other problem is using other computers without your remapping. That was a nuisance too, though bearable.)

I remapped [] to () with xmodmap and like it. It was a bit weird getting used to writing code in languages that use [], but like any change, you get used to it. Having unshifted parens in Lisp is nicer than not having unshifted brackets in other languages, so it works out.

Anyway, here is the necessary xmodmap incantation for my US keyboard:

!! swap () and []
keycode  18 = 9 bracketleft
keycode  19 = 0 bracketright
keycode  34 = parenleft braceleft
keycode  35 = parenright braceright

"... so many parenethesis"

The first thing I did was bind the '(' key to the sequence '('+')'+right(), so my parenthesis auto balance, leaving half as many left to type when writing new code.

You also want a convenient way to navigate out one paren -- bind C-] to the sequence search(')')+right(). Authoring becomes shorter now, as you don't need to take hands off the home position -- just type C-] every time you complete an S-expr.

Next thing I did was bind a key to a subroutine that pushes an existing item onto the current list ... so if // is the cursor position, then this command will transform:

(if (< //) (+ x 1) 
    (x) 
  (y))

to

(if (< (+ x 1) //)
    (x) 
  (y))

Effectively pushing one item from the right into the current list -- very useful for editing existing code. The sequence '(', '<', C-S-], Space, '2' adds "compare less than 2" to an existing expression. Combined with C-], this lets you build new expressions very quickly from existing ones.

@jamesnvc, I didn't think about binding () to [] keys... I'll have to try that, thx!

I have to take my fingers off the home row to reach all the other shift-number operators, so I never thought about it much.

And once you type a left-parens, electric-parens give you the right.

If you use the parentheses more than the square brackets, by all means, remap away. I don't see how it could pose any more problems than, say, a lefty swapping her mouse buttons.

When I'm writing code, I generally spend much more time thinking and reading my code, than I do typing it. I've tried a couple of times in the past to switch to the Dvorak keyboard layout, but I lack obvious motivation because I can type much faster than I can think. Programming language syntax is a similar issue - as long as I can type code without leaving the keyboard (ie. using the mouse would be bad), I'm happy.

Mostly, I just type them, but occasionally, I use M-( and M-) (especially when I am adding a LET binding "late in the stage"), to enclose the relevant nnumber of expressions.

I also changed my (dvorak) keyboard layout (via xmodmap) to switch the brackets ("[]") and parens, in conjunction with paredit-mode (which does indeed take some getting used to).

I use paredit and pair-mode packages but, for fast parenthesis typing I use electric-dot-and-dash to replace a double period in a () on a 5 ms delay (if I type slowly I get two dots then). It's a wonderful package (I hacked a bit for my personal preference; as I type with Dvorak keyboard, I replaced the dash key by a slash (// is not so common in lisp)).

To avoid the mess in parens, I add a package named 'highlight-parentheses to my tool set, and for maximum efficiency on sexp grabbings or text navigation in general, I also use vimpulse (as I am a Vim addict).

DrScheme has the keystrokes for parens and square braces flipped by default. It also has a feature where it magically guesses which one of the two you meant, so you rarely reach for shift-9.

Quack has a similar feature to that of DrScheme.

DivaScheme (my editor), is something completely different. It edits at the sexp level, so that the parens are no longer in the way.

Rebind capslock to "(" and have the editor autoinsert ")" for you.

(This also helps for other languages with a lot of brackets, for instance HTML...)

I use Vim with vim-sexp and vim-sexp-mappings-for-regular-people then have mapped <leader>u to put me in normal mode inside of a new (.

map <leader>u i(

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top