Comments in AutoLISP
Tuesday, January 27, 2026Commenting your code is a good practice that makes your programs easier to maintain and understand. AutoLISP supports five comment styles, each with a specific purpose. Let's take a look at them.
The inline comment: ;| ... |;
This style allows you to insert a comment directly in the middle of an expression, or across multiple lines:
(setq x ;| initial value |; 10)
You can also use it across multiple lines:
;| This is a comment
spanning multiple lines |;
This is the only comment style in AutoLISP that allows you to comment out a block of code. It is useful for temporarily disabling a portion of code during debugging.
The column comment: ;
A comment preceded by a single semicolon is aligned to a fixed column defined in the VLIDE formatting options. It is intended for short end-of-line annotations:
(setq radius 10) ; Circle radius
(setq diameter (* 2 radius)) ; Calculate diameter
When auto-formatting code in VLIDE, these comments are repositioned to the column defined by the Single-Semicolon comment indentation option.
The current-column comment: ;;
A comment preceded by two semicolons is indented at the same level as the surrounding code. It is used to describe the code block that follows:
(defun c:draw-circle (/ center radius)
;; Ask the user for parameters
(setq center (getpoint "\nCenter of the circle: "))
(setq radius (getdist center "\nRadius: "))
;; Draw the circle
(command "._CIRCLE" center radius)
)
This is the most common style for commenting code sections inside a function. Note that with VS Code, you can also use Ctrl + K, Ctrl + C to comment out selected lines (Ctrl + K, Ctrl + U will uncomment them).
The heading comment: ;;;
A comment preceded by three semicolons is always placed at the beginning of a line, without indentation. It is used to document functions:
;;; Draws a circle from a center point and a radius
;;; center : the center point of the circle
;;; radius : the radius of the circle
(defun draw-circle (center radius)
(command "._CIRCLE" center radius)
)
This is the convention used to describe a function's purpose, its parameters and its return value.
The closing comment: ;_
This style is placed right after a closing parenthesis to indicate which expression it closes:
(defun c:example (/ i)
(setq i 0)
(while (< i 10)
(if (= (rem i 2) 0)
(princ (strcat "\n" (itoa i) " is even"))
(princ (strcat "\n" (itoa i) " is odd"))
) ;_ if
(setq i (1+ i))
) ;_ while
) ;_ defun
When functions grow long and parentheses pile up, this comment style helps identify which expression each closing parenthesis terminates.
Full example
Here is an example that uses all five styles:
;;; Displays numbers from 1 to n with their parity
;;; n : the maximum number
(defun display-parity (n / i)
;; Initialization
(setq i 1)
;; Main loop
(while (<= i n)
(if (= (rem i 2) 0)
(princ (strcat "\n" (itoa i) " is even" ;| even result |;))
(princ (strcat "\n" (itoa i) " is odd"))
) ;_ if
(setq i (1+ i)) ; Increment counter
) ;_ while
) ;_ defun
Best practices
- Use
;;;to document your functions (purpose, parameters, return value). - Use
;;to explain logical sections of your code. - Use
;_when your functions exceed a dozen lines to identify closing parentheses. - Use
;sparingly for end-of-line annotations. - Use
;| ... |;to temporarily disable code during debugging.
Need an AutoCAD (AutoLISP, ObjectARX, .NET, VBA) development? Contact me for a free quote.