2

I am in a computer science class that requires that Lisp style look like this:

(define append (as bs)
  (if (null? as)
      bs
      (cons (car as)
            (append (cdr as) bs))))

instead of like this:

(define append (as bs)
  (if (null? as)
    bs
    (cons (car as)
          (append (cdr as) bs))))

Namely, the if branches should be aligned. Is there a way to force Vim to do this?

tekknolagi
  • 10,663
  • 24
  • 75
  • 119

1 Answers1

2

If you want Vim's Lisp mode to treat all forms as function calls, so that items of a multi-line expression align with the second element, then simply clear the lispwords parameter:

:set lispwords=

From your exmaple, you do seem to want a split define to be treated with indentation rather than alignment. Figure out the set of forms for which you want indenting rather than aligning behavior and put that exact set into your lispwords:

:set lispwords=define[,... whatever other symbols]

This can be done based on file type with an auto-loaded. In your .vimrc file you can have:

:au BufRead,BufNewFile *.scm set lispwords=define

After putting that in my .vimrc temporarily, if I edit a new file:

$ vim foo.scm

and then insert a few lines, they get automatically formatted like thisL

(define fun ()
  (if foo
      (then ...)
      (else ...)
~
~
~
:q!

I just hit Enter there; the auto-indentation kicked in. Vim's config already associates .scm files with Scheme and sets up Lisp mode. Our au command customizes that by overriding lispwords.

Kaz
  • 55,781
  • 9
  • 100
  • 149