G-code (ISO 6983 / RS-274) is the language most CNC controllers read. Many commands are modal: once set, a code stays in effect until another code from the same modal group replaces it. Exact dialects vary between controllers (Fanuc-style, Haas, LinuxCNC, GRBL, Marlin); this page covers the common core and says where controls differ. Group numbers below follow LinuxCNC and the NIST RS-274/NGC interpreter. To check a program line by line, paste it into the G-code Explainer & Simulator, which explains each block and plots the toolpath.
Motion and canned cycles
| Code | Name | What it does |
|---|---|---|
| G00 | Rapid move | Traverse at the machine's maximum rate, no cutting (motion, group 1). LinuxCNC moves in a straight coordinated line; on some controls (e.g. Haas) each axis moves at full speed, so the path can dog-leg. Keep rapids clear of the part and clamps. |
| G01 | Linear feed | Straight line at the programmed feed rate (F). Used for cutting (group 1). |
| G02 | CW arc | Clockwise circular interpolation (group 1). Needs I/J/K (centre offset from the start point) or R (radius). With R, a negative value selects the arc that is longer than 180° (LinuxCNC / RS-274/NGC). |
| G03 | CCW arc | Counter-clockwise circular interpolation. Same parameters as G02. |
| G04 | Dwell | Pause; non-modal (group 0), not a motion code. On LinuxCNC, P is seconds and may be fractional (G4 P0.5). On Haas, P without a decimal point is milliseconds (G04 P10 = 10 ms, P10. = 10 s). Commonly used to break chips at the bottom of a hole. |
| G33 | Thread cutting | Lathes: spindle-synchronised motion; K is the distance per revolution (pitch). |
| G73 | Peck drill (high speed) | Canned cycle with a short retract between pecks to break chips. |
| G76 | Threading / boring (varies) | Meaning depends on the control: on LinuxCNC (and on lathes) G76 is the multi-pass threading cycle; some mill controls use G76 for a boring cycle. Check your control's manual. |
| G80 | Cancel canned cycle | Ends the active drilling/tapping/boring cycle (G73, G81–G89). |
| G81 | Drill canned cycle | Drill to Z, then retract. |
| G82 | Drill + dwell | Drill to Z, dwell P at the bottom, retract (spot facing, counterbores). |
| G83 | Peck drill | Deep-hole drilling: retracts to the R plane between pecks to clear chips. Peck parameters differ between controls. |
| G84 | Tapping cycle | Right-hand tapping cycle. On LinuxCNC, G84 does not synchronise feed to the spindle (use a floating tap holder); rigid tapping there is G33.1. Industrial controls differ, so check your manual. |
| G85 | Boring | Feed in, feed out (no dwell). |
Plane, distance, units and feed mode (groups 2, 3, 5, 6)
| Code | Group | Meaning |
|---|---|---|
| G17 | Plane (2) | XY plane: the mill default. Arcs use I/J. |
| G18 | Plane (2) | XZ plane, used on lathes. Arcs use I/K. LinuxCNC still defaults to G17 in lathe mode, so put G18 in the program preamble. |
| G19 | Plane (2) | YZ plane. Arcs use J/K. |
| G20 | Units (6) | Inches. |
| G21 | Units (6) | Millimetres. |
| G90 | Distance (3) | Absolute positioning (X is a coordinate). On Haas lathes G90 is also the O.D./I.D. turning cycle. |
| G91 | Distance (3) | Incremental positioning (X is a distance from the current position). |
| G93 | Feed mode (5) | Inverse time: F is 1 ÷ minutes per move. |
| G94 | Feed mode (5) | Feed per minute (in/min or mm/min); the mill default. On Haas lathes, feed per minute is G98 and G94 is the end-facing cycle. |
| G95 | Feed mode (5) | Feed per revolution. On Haas lathes this is G99. |
| G98 | Return mode (10) | Mill canned cycles: return to the initial Z between holes. On Haas lathes: feed per minute. |
| G99 | Return mode (10) | Mill canned cycles: return to the R plane between holes. On Haas lathes: feed per revolution. |
Tool & cutter compensation (groups 7, 8)
| Code | What it does |
|---|---|
| G40 | Cancel cutter-radius comp. |
| G41 | Cutter comp left of programmed path (climb milling, RH tool). |
| G42 | Cutter comp right of programmed path (conventional milling, RH tool). |
| G43 | Tool-length comp positive (H word gives offset number). |
| G44 | Tool-length comp negative (rare). |
| G49 | Cancel tool-length comp. |
Work coordinate systems (group 12)
| Code | Meaning |
|---|---|
| G53 | Machine coordinates for this block only (non-modal). |
| G54 | Work offset 1 (the usual default at start-up). |
| G55 | Work offset 2. |
| G56 | Work offset 3. |
| G57 | Work offset 4. |
| G58 | Work offset 5. |
| G59 | Work offset 6. |
| Extended | Additional work offsets: syntax varies by control (e.g. Haas uses G154 Pn). |
M-codes (miscellaneous / machine functions)
| Code | What it does |
|---|---|
| M00 | Program stop (wait for operator). |
| M01 | Optional stop (runs only if "optional stop" is on). |
| M02 | End of program. On LinuxCNC, M2 and M30 both end the program and reset the modal state; Cycle Start restarts from the top of the file. |
| M03 | Spindle on, clockwise. Usually with S (rpm). |
| M04 | Spindle on, counter-clockwise. |
| M05 | Spindle stop. |
| M06 | Tool change (typically preceded by T#). Some controls also stop the coolant here. |
| M07 | Mist coolant on. |
| M08 | Flood coolant on. |
| M09 | Coolant off. |
| M19 | Spindle orient (for boring/indexing). |
| M30 | End of program and reset (LinuxCNC: also exchanges pallet shuttles). On Marlin 3D printers M30 is not a program end: it deletes a file from the SD card (Marlin reference). |
| M41/M42 | Low/high spindle gear range (lathes, and mills with a gearbox). |
| M48/M49 | Feed/speed override enable / disable (LinuxCNC). Other controls use M48 for something else, for example a probe repeatability test on Marlin; see the M-code reference by controller. |
| M98/M99 | Call sub-program / return. |
Address letters (parameters)
| Letter | Typical use |
|---|---|
| X Y Z | Linear axes. |
| A B C | Rotary axes around X, Y, Z. |
| U V W | Secondary linear axes (also incremental in some dialects). |
| I J K | Arc centre offset from start point (IJ for G17, IK for G18, JK for G19). |
| R | Arc radius, also retract plane in canned cycles. |
| F | Feedrate (in/min or mm/min; per rev with G95). |
| S | Spindle speed (rpm) or surface speed (with CSS mode). |
| T | Tool number (plus M06 to execute change). |
| H / D | Tool length offset number (H) / diameter offset number (D). |
| N | Block/sequence number. |
| P Q | Parameters — dwell time (P in G04), subprogram number, cycle peck. |
| L | Loop / subprogram repeat count. |
Minimal mill program — face a rectangle
% O0001 (FACE 60X40) G21 G17 G40 G49 G80 G94 (mm, XY plane, clear comps) G90 G54 (absolute, work offset 1) T1 M06 (face mill, tool change) G43 H1 (tool length comp) S3000 M03 (spindle on CW 3000 rpm) M08 (flood on) G0 X-10 Y0 (rapid to start) Z5 (safe Z) G1 Z-0.5 F200 (plunge) X70 F600 (cut pass 1) Y20 (step over) X-10 (pass 2) Y40 X70 (pass 3) G0 Z50 (rapid up) M09 (coolant off) M05 (spindle off) G28 G91 Z0 (home Z) G90 M30 (end) %
Comments are in ( ) because ";" means "end of block" on Fanuc/Haas-style controls. Open a program like this in the simulator to see each line explained, the toolpath plotted and the run time estimated.
Common dialect differences
- Fanuc and Fanuc-compatible controls: % … % program delimiters, O#### program numbers.
- Haas: Fanuc-compatible plus Haas-specific codes. On Haas lathes, G98/G99 are feed per minute / per revolution and G90/G94 are turning/facing cycles; G04 P without a decimal point is milliseconds.
- LinuxCNC: follows the NIST RS-274/NGC interpreter closely. A negative R gives arcs over 180°; G76 is the threading cycle; rigid tapping is G33.1; lathe X is radius unless diameter mode (G7) is set.
- GRBL (hobby routers and laser engravers): a subset of RS-274. Supports G54–G59 work offsets, but no cutter compensation (G41/G42), no macro variables, and no canned cycles (only G80 is accepted).
- 3D printers (Marlin, Klipper): use G0/G1 + extruder E axis; G28 homing, G29 bed mesh; M104/M109 hotend, M140/M190 bed, M106/M107 fan.
- Always consult the machine builder's manual for exact behaviour — especially canned cycles, dwell units, G28, and macro variables (#100+).
Sources
- LinuxCNC documentation: G-codes, M-codes, overview and modal groups (linuxcnc.org/docs).
- NIST, The NIST RS274NGC Interpreter — Version 3, NISTIR 6556 (Kramer, Proctor, Messina, 2000).
- Haas Automation mill and lathe G-code / M-code pages.
- Grbl v1.1 README (github.com/gnea/grbl).
Frequently asked questions
What is the difference between G00 and G01?
G00 is a rapid move at the machine's maximum rate with no cutting; G01 is a straight cutting move at the programmed feed rate F. On LinuxCNC a G00 move is a straight coordinated line, but some controls (such as Haas) move each axis at full speed, so a rapid can take a dog-leg path.
What do G02 and G03 mean?
G02 is a clockwise arc and G03 a counter-clockwise arc. The arc is defined either by I/J/K (the centre's offset from the start point) or by R (the radius). In LinuxCNC and the NIST RS-274/NGC interpreter, a negative R selects the arc that is longer than 180°.
What are G54 to G59?
They select work coordinate systems 1 to 6 (modal group 12), so the same program can run on parts clamped in different places. G53 moves in machine coordinates for one block only. Extra offsets beyond G59 use a control-specific code, such as G154 on Haas.
What is the difference between M02 and M30?
Both end the program. On LinuxCNC, M2 and M30 both reset the modal state (G54, G17, G90, G94, cutter compensation off, spindle stopped) and Cycle Start restarts the program from the top; M30 also exchanges pallet shuttles. Check your control's manual for any difference it makes.
How long is a G04 dwell?
It depends on the control. On LinuxCNC, G4 P is in seconds and may be fractional (G4 P0.5 waits half a second). On Haas, a P value without a decimal point is in milliseconds, so G04 P10 is 10 ms while G04 P10. is 10 seconds.