I'm new to Clojure and new to Emacs.
Is there an Emacs short-cut to intelligently re-indent the whole file?
if not, is there at least a way to indent selected regions left or right?
I feel like I'm back in the stone age repeatedly pressing the arrows
C-x h selects the entire buffer. C-M-\ reindents the selected region.
Ctrl-x, h (select all) followed by Tab (to indent)
cider-format-buffer command (Since cider 0.9.0)
When you capture data from a sequence like C-u C-c C-e
(cider-eval-last-sexp), the raw data output to your buffer can be
unwieldy to inspect/work with. And the normal code-indenting commands
(mentioned in answers here) don't handle it well.
For handling results from such evaluated expressions, try
cider-format-edn-region.
As a concrete example, have you ever tried reformatting your
~/.lein/profiles.clj? This is pretty hard to do and keep
consistent, until you discover cider-format-edn-region. Take
caution that it will, however, remove any comments.
Use cljfmt for many configurable ways to reformat/reindent. It has an Emacs plugin, but also can be run via lein.
I'm new to Clojure and new to Emacs.
Is there an Emacs short-cut to intelligently re-indent the whole file?
if not, is there at least a way to indent selected regions left or right?
I feel like I'm back in the stone age repeatedly pressing the arrows
C-x h selects the entire buffer. C-M-\ reindents the selected region.
Ctrl-x, h (select all) followed by Tab (to indent)
cider-format-buffer command (Since cider 0.9.0)
When you capture data from a sequence like C-u C-c C-e
(cider-eval-last-sexp), the raw data output to your buffer can be
unwieldy to inspect/work with. And the normal code-indenting commands
(mentioned in answers here) don't handle it well.
For handling results from such evaluated expressions, try
cider-format-edn-region.
As a concrete example, have you ever tried reformatting your
~/.lein/profiles.clj? This is pretty hard to do and keep
consistent, until you discover cider-format-edn-region. Take
caution that it will, however, remove any comments.
Use cljfmt for many configurable ways to reformat/reindent. It has an Emacs plugin, but also can be run via lein.
yep I am the one who started to use EMACS but not willing to abandon the VIM skills I learned.
evil is definitely a good thing.
the issue I am encountering is that there too few documentation for this. Obviously I am using a tiny fraction of the key-mappings defined in evil.
So I take a look at the evil-maps.el, and get quite a few question regarding this file:
a lot of the key are not mapped to the function defined there
what is the evil-window-map ? (just find out my self, it is key-bindings for jumping between windows, and to activate it: need to (setq evil-want-C-w-in-emacs-state 1 ) )
what is the operator pending state ?
did evil load all the maps it listed in the evil-maps file into Emacs ?
any better documentation for evil ?
it takes sometime to understand the STATE of EVIL plugin and then those shortkeys just become handy. so the solution is to read the documentation again and again.
I would like rainbow parens for editing Clojure in Emacs and since VI does this I assume that in Emacs it should be something like M-x butterfly or something :)
This is an old question now, but I recently wrote RainbowDelimiters mode for this. It's a 'rainbow parens'-type mode which colors all parens, brackets and braces, made with Clojure programming in mind.
It highlights the whole buffer, not just the parens surrounding point.
The most important thing is that it's FAST - all the other rainbow paren modes I've tried slow down editing (especially scrolling) quite a lot. I put significant effort into profiling and optimizing it so doesn't have any noticeable impact on scrolling/editing speed.
You can find info about it at the EmacsWiki page, and the mode itself is at rainbow-delimiters.el.
I'm using highlight-parentheses-mode from the script mquander mentioned. It doesn't provide much of a rainbow effect out of the box, but it is customisable:
(setq hl-paren-colors
'(;"#8f8f8f" ; this comes from Zenburn
; and I guess I'll try to make the far-outer parens look like this
"orange1" "yellow1" "greenyellow" "green1"
"springgreen1" "cyan1" "slateblue1" "magenta1" "purple"))
I believe I've lifted the actual colours from Vimclojure. Note that Vimclojure highlights all parentheses in the file, whereas with highlight-parentheses-mode only the parens which actually contain the point will be highlighted (and only a limited number of levels). I happen to find this behaviour useful, but it is perhaps a bit lacking in the prettiness area in comparison with the Vimclojure way.
I now notice I've never gotten 'round to fixing those outer paren colours actually... Maybe I will now that you've reminded me about it.
Here's a mode for that which I have used in the past briefly. Here's another one which I haven't tried.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this 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?
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.
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 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 take my fingers off the home keys....
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.)
"... 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 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
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.
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.
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).
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(