3 ms·
The way I do it, is that in my editor, I can type: '(some data|) Where | is my cursor. And then I have a keybinding that moves my cursor to the front and wra
by didibus 8y ago
The way I do it, is that in my editor, I can type:
'(some data|)
Where | is my cursor.
And then I have a keybinding that moves my cursor to the front and wraps around like so:
(| '(some data))
So I can type the next thing:
(get-string| '(some data))
Press it again:
(| (get-string '(some-data)))
And so on.
That way, even though the code is visually nested, I can write it in logical order from what eval first to what eval last. A good editor for editing lisp code makes that possible, as I showed, and many other operations.
Basically, get yourself accustomed to a good Lisp editor such as Emacs and I'm sure you'll find that problem will disappear. For me, it's even more productive now typing wise, because I can make more structural edits to my code, which is way faster.
- Riverheart 8y agoFor anyone stubborn enough to want to do this with Vim, I've come up with this. function NestExpression() " Get end paren pos " Returns [0, 0] if not in parens. let save_cursor = getpos('.') " If line not set to zero, append let end_paren_pos = searchpairpos('(', '', ')') if end_paren_pos[0] > 0 execute "normal! a)" else " We're not in a set of parentheses return endif " Return to the inside of the parentheses call setpos('.', save_cursor) " If line not set to zero, start insert " mode, prepend and move cursor left let end_paren_pos = searchpairpos('(', '', ')', 'b') if end_paren_pos[0] > 0 call feedkeys("i( \<LEFT>") endif endfunction nmap fj :call NestExpression()<cr>
- oblio 8y agoI don't think he's talking about how to type the characters... He's probably asking: you're inverting, visually, the order of operations. When does start becoming natural?
- Riverheart 8y agoThis is just what I've been looking for! I've been searching for this and see there are commands for navigating s expressions but I haven't found this example. Is this a built-in command? Is there name for this action?
- TeMPOraL 8y agoThere are some built-in commands in Emacs for navigating, cutting and transposing s-exps, but what you want is probably a step further - a package like Paredit[0], that gives you much more sexp-specific actions, and automatically keeps parens balanced for you. See animated examples at [1] and a cheat sheet at [2]. There isn't, as far as I know, a default keybinding for what GP did - navigate up the SEXP, wrap in parens and place point before the inner SEXP. You can get very close to it with two steps: paredit-backward-up [C-M-u] and paredit-wrap-round [M-(], though it gets confused by the hanging quote a bit. To demonstrate on GP's example: '(some data|) '|(some data) ; after C-M-u '(|(some data)) ; after M-( So the quote is in a wrong place. You could probably whip a command doing OP's thing that calls paredit-backward-up and paredit-wrap-round, with a conditional that backs up a few chars if it detects a piece of syntactic sugar attached to the SEXP (quote, quasiquote, comma, reader macro invocation). Somebody has likely already done it, or maybe there is a less known Paredit command for that - I don't know. If anyone does, please chime in. Myself, I mostly use wrapping, barfing and slurping. -- [0] - Available in Emacs' package manager on MELPA, or here: https://github.com/emacsmirror/paredit https://github.com/emacsmirror/paredit. [1] - http://danmidwood.com/content/2014/11/21/animated-paredit.html http://danmidwood.com/content/2014/11/21/animated-paredit.ht... [2] - https://github.com/joelittlejohn/paredit-cheatsheet https://github.com/joelittlejohn/paredit-cheatsheet