AutoLISP: The Complete Guide — Chapter 2 / 16

Getting Started — The Command Line

One of the great advantages of AutoLISP is that it is directly accessible from AutoCAD's command line. No need to install anything, no need to compile: you type an expression, press Enter, and AutoCAD executes it immediately. It's the fastest way to discover the language.

AutoCAD's Command Line

The command line is the text area located at the bottom of the AutoCAD window. This is where you normally type command names like LINE, CIRCLE, or COPY. It's also where you'll write your AutoLISP expressions.

Empty AutoCAD command line

How does AutoCAD recognize an AutoLISP expression? It's simple: any input that starts with an opening parenthesis ( is interpreted as AutoLISP code, not as a standard AutoCAD command.

Your First Expression: (alert)

Let's start with something very simple and visual. The alert function displays a dialog box with a message. Type this in the command line:

(alert "Hello AutoLISP!")

Typing (alert "Hello AutoLISP!") in the command line

Press Enter. A dialog box appears with your message:

The alert dialog box displaying "Hello AutoLISP!"

Congratulations, you just executed your first AutoLISP expression! Let's break down what happened:

  • The parentheses ( and ) delimit the expression
  • alert is the name of the function to call
  • "Hello AutoLISP!" is the argument passed to this function (a string, enclosed in quotation marks)

The alert function is ideal for beginners because it produces a result that is immediately visible. You'll know right away whether your code works.

Testing Simple Calculations

AutoLISP can also serve as a calculator. Try these expressions one by one in the command line:

(+ 2 3)

AutoCAD displays the result directly in the command line:

5

Result of (+ 2 3) in the command line

Try other operations:

(- 10 4)

Result: 6

(* 3 7)

Result: 21

(/ 20 4)

Result: 5

You'll notice that the syntax is different from what you're used to: instead of writing 2 + 3, you write (+ 2 3). The operator comes first, before the numbers. This is prefix notation, a characteristic feature of LISP. We'll study it in detail in the next chapter.

The Return Value

Every AutoLISP expression returns a value. This is a fundamental concept. When you type (+ 2 3), the result 5 isn't just displayed — it is returned by the + function.

Even the alert function returns a value. After closing the dialog box, you'll see nil appear in the command line. nil is the "nothing" value in AutoLISP — it's what functions return when they have no meaningful result.

nil result in the command line after an alert

Nesting Expressions

Since every expression returns a value, you can use the result of one expression as the argument of another. For example:

(alert (itoa (+ 2 3)))

This expression works from the inside out:

  1. (+ 2 3) evaluates to 5
  2. (itoa 5) converts the number 5 to the string "5" (itoa stands for integer to ASCII)
  3. (alert "5") displays "5" in a dialog box

The alert dialog box displaying "5"

This is the power of LISP: expressions can be nested within each other, like Russian dolls. Each pair of parentheses contains a complete expression.

Errors

Don't be afraid of making mistakes! AutoLISP displays explicit error messages. For example, if you forget a closing parenthesis:

(+ 2 3

AutoCAD waits for more input and displays an indicator showing that parentheses are missing. You can either complete the expression by typing ), or press Escape to cancel.

If you try to use a function name that doesn't exist:

(hello)

You'll get an error message like:

; error: no function definition: HELLO

AutoLISP error message in the command line

These messages are your allies: they tell you precisely what went wrong. Get into the habit of reading them carefully.

Displaying Text in the Command Line

The alert function displays a message in a dialog box, but you can also write directly to the command line with princ:

(princ "Hello from the command line!")

The text appears immediately in the command line, without a dialog box. You'll notice that the text appears twice: once as the effect of princ, and once as the return value of the expression. Don't worry about this for now.

You can also use print (with a t) which automatically adds a newline:

(print "Hello with a newline!")

Summary

Here's what we learned in this chapter:

Concept Example
Execute an expression (alert "Hello!")
Calculate (+ 2 3)
Nest expressions (alert (itoa (+ 2 3)))
Display in the command line (princ "text")
Return value Every expression returns a value
nil The "nothing" value

In the next chapter, we'll explore AutoLISP syntax in detail: prefix notation, the role of parentheses, and how to read LISP code with ease.


Helping hand Need an AutoCAD (AutoLISP, ObjectARX, .NET, VBA) development? Contact me for a free quote.