G And M Codes
G-codes and M-codes are the instruction set CNC machines use to control tool movement, spindle speed, coolant, and other machine functions — G-codes handle geometric moves (rapid positioning, linear and circular cutting, canned cycles), while M-codes handle miscellaneous machine functions (spindle on/off, coolant, program stop, tool change). Together they form the low-level language that a CAM-generated program, or a manually written one, uses to drive the machine. The tables below give a comprehensive listing of common G-codes and M-codes for milling and turning, with an explanation and syntax example for each.
Codes can vary slightly between machine controllers (Fanuc, Siemens, Haas, etc.), so always check your specific controller's programming manual for any machine-specific variations before relying on a code's behaviour exactly as listed here.
'G' stands for Geometry; hence, the G-Code commands are responsible for the movements of the machine that create the geometry of the part.
'M' stands for Machine (or Miscellaneous), and the M-Codes are responsible for Machine commands that cause particular operations of the equipment. Unlike G-codes, which can appear multiple times on the same line, M-Code is limited to one code per line.
| G-CODE | GROUP | FUNCTION |
|---|---|---|
| G01 | 1 | Linear Interpolation Motion (X,Y,Z,A,B,F) |
| G02 | 1 | Circular Interpolation Motion CW (X,Y,Z,A,I,J,K,R,F) |
| G03 | 1 | Circular Interpolation Motion CCW (X,Y,Z,A,I,J,K,R,F) |
| G04 | 0 | Dwell (P) (P =seconds"."milliseconds) |
| G09 | 0 | Exact Stop, Non-Modal |
| G10 | 0 | Programmable Offset Setting (X,Y,Z,A,L,P,R) |
| G12 | 0 | Circular Pocket Milling CW (Z,I,K,Q,D,L,F) |
| G13 | 0 | Circular Pocket Milling CCW (Z,I,K,Q,D,L,F) |
| G17* | 2 | Circular Motion XY Plane Selection (G02 or G03) (Setting 56) |
| G18 | 2 | Circular Motion ZX Plane Selection (G02 or G03) |
| G19 | 2 | Circular Motion YZ Plane Selection (G02 or G03) |
| G20* | 6 | Verify Inch Coordinate Positioning (Setting 9 will need to be INCH) (Setting 56) |
| G21 | 6 | Verify Metric Coordinate Positioning (Setting 9 will need to be METRIC) |
| G28 | 0 | Machine Zero Return Thru Reference Point (X,Y,Z,A,B) (Setting 108) |
| G29 | 0 | Move to location Thru G28 Reference Point (X,Y,Z,A,B) |
| G31** | 0 | Feed Until Skip Function (X,Y,Z,A,B,F) |
| G35** | 0 | Automatic Tool Diameter Measurement (D,H,Z,F) |
| G36** | 0 | Automatic Work Offset Measurement (X,Y,Z,A,B,I,J,K,F) |
| G37** | 0 | Automatic Tool Offset Measurement (D,H,Z,F) |
| G40* | 7 | Cutter Compensation Cancel G41/G42/G141 (X,Y) (Setting 56) |
| G41 | 7 | 2D Cutter Compensation Left (X,Y,D) (Setting 43, 44, 58) |
| G42 | 7 | 2D Cutter Compensation Right (X,Y,D) (Setting 43, 44, 58) |
| G43 | 8 | Tool Length Compensation + (H,Z) (Setting 15) |
| G44 | 8 | Tool Length Compensation - (H,Z) (Setting 15) |
| G47 | 0 | Text Engraving (X,Y,Z,R,I,J,P,E,F) (Macro Variable #599 to Change Serial number) |
| G49* | 8 | Tool Length Compensation Cancel G43/G44/G143 (Setting 56) |
| G50* | 11 | Scaling G51 Cancel (Setting 56) |
| G51** | 11 | Scaling (X,Y,Z,P) (Setting 71) |
| G52 | 12 | Select Work Coordinate System G52 (Setting 33, YASNAC) |
| G52 | 0 | Global Work Coordinate System Shift (Setting 33, FANUC) |
| G52 | 0 | Global Work Coordinate System Shift (Setting 33, HAAS) |
| G53 | 0 | Machine Zero XYZ Positioning, Non-Modal |
| G54* | 12 | Work Offset Positioning Coordinate #1 (Setting 56) |
| G55 | 12 | Work Offset Positioning Coordinate #2 |
| G56 | 12 | Work Offset Positioning Coordinate #3 |
| G57 | 12 | Work Offset Positioning Coordinate #4 |
| G58 | 12 | Work Offset Positioning Coordinate #5 |
| G59 | 12 | Work Offset Positioning Coordinate #6 |
| G60 | 0 | Uni-Directional Positioning (X,Y,Z,A,B) (Setting 35) |
| G61 | 13 | Exact Stop, Modal (X,Y,Z,A,B) |
| G64* | 13 | Exact Stop G61 Cancel (Setting 56) |
| G65** | 0 | Macro Sub-Routine Call |
| G66** | 0 | Modal Mode for Macro Sub-Routine Call |
| G67** | 0 | Cancel Modal Mode for Macro Sub-Routine Call |
| G68** | 16 | Rotation (G17,G18,G19,X,Y,Z,A,R) (Setting 72, 73) |
| G69* | 16 | Rotation G68 Cancel (Setting 56) |
| G70 | 0 | Bolt Hole Circle with a Canned Cycle (,I,J,L) |
| G71 | 0 | Bolt Hole Arc with a Canned Cycle (,I,J,K,L) |
| G72 | 0 | Bolt Holes Along an Angle with a Canned Cycle (,I,J,L) |
| G73 | 9 | High Speed Peck Drill Canned Cycle (X,Y,A,B,Z,I,J,K,Q,P,R,L,F) (Setting 22) |
| G74 | 9 | Reverse Tapping Canned Cycle (X,Y,A,B,Z,R,J,L,F) (Setting 130, 133) |
| G76 | 9 | Fine Boring Canned Cycle (X,Y,A,B,Z,I,J,P,Q,P,R,L,F) (Setting 27) |
| G77 | 9 | Back Bore Canned Cycle(X,Y,A,B,Z,I,J,Q,R,L,F) (Setting 27) |
| G80* | 9 | Cancel Canned Cycle (Setting 56) |
| G81 | 9 | Drill Canned Cycle (X,Y,A,B,Z,R,L,F) |
| G82 | 9 | Spot Drill / Counterbore Canned Cycle (X,Y,A,B,Z,P,R,L,F) |
| G83 | 9 | Peck Drill Deep Hole Canned Cycle (X,Y,A,B,Z,I,J,K,Q,P,R,L,F) (Setting 22, 52) |
| G84 | 9 | Tapping Canned Cycle (X,Y,A,B,Z,R,J,L,F) (Setting 130, 133) |
| G85 | 9 | Bore in~Bore out Canned Cycle (X,Y,A,B,Z,R,L,F) |
| G86 | 9 | Bore in~Stop~Rapid out Canned Cycle (X,Y,A,B,Z,R,L,F) |
| G87 | 9 | Bore in~Manual Retract Canned Cycle (X,Y,A,B,Z,R,L,F) |
| G88 | 9 | Bore~Dwell~Manual Retract Canned Cycle (X,Y,A,B,Z,P,R,L,F) |
| G89 | 9 | Bore~Dwell~Bore out Canned Cycle (X,Y,A,B,Z,R,L,F) |
| G90* | 3 | Absolute Positioning Command (Setting 56) |
| G91 | 3 | Incremental Positioning Command (Setting 29) |
| G92 | 0 | Set Work Coordinate Value (Fanuc) (HAAS) |
| G92 | 0 | Global Work Coordinate System Shift (Yasnac) |
| G93 | 5 | Inverse Time Feed Mode ON |
| G94* | 5 | Inverse Time Feed Mode OFF/Feed Per Minute ON (Setting 56) |
| G95 | 5 | Feed Per Revolution |
| G98* | 10 | Canned Cycle Initial Point Return (Setting 56) |
| G99 | 10 | Canned Cycle "R" Plane Return |
| G100 | 0 | Mirror Image Cancel |
| G101 | 0 | Mirror Image (X,Y,Z,A,B) (Setting 45, 46, 47, 48, 80) |
| G102 | 0 | Programmable Output to RS-232 (X,Y,Z,A,B) |
| G103 | 0 | Limit Block Look-a-head (P0-P15 for number of lines control looks ahead) |
| G107 | 0 | Cylindrical Mapping (X,Y,Z,A,Q,R) |
| G110 | 12 | Work Offset Positioning Coordinate #7 |
| G111 | 12 | Work Offset Positioning Coordinate #8 |
| G112 | 12 | Work Offset Positioning Coordinate #9 |
| G113 | 12 | Work Offset Positioning Coordinate #10 |
| G114 | 12 | Work Offset Positioning Coordinate #11 |
| G115 | 12 | Work Offset Positioning Coordinate #12 |
| G116 | 12 | Work Offset Positioning Coordinate #13 |
| G117 | 12 | Work OffsetPositioning Coordinate #14 |
| G118 | 12 | Work Offset Positioning Coordinate #15 |
| G119 | 12 | Work Offset Positioning Coordinate #16 |
| G120 | 12 | Work Offset Positioning Coordinate #17 |
| G121 | 12 | Work Offset Positioning Coordinate #18 |
| G122 | 12 | Work Offset Positioning Coordinate #19 |
| G123 | 12 | Work Offset Positioning Coordinate #20 |
| G124 | 12 | Work Offset Positioning Coordinate #21 |
| G125 | 12 | Work Offset Positioning Coordinate #22 |
| G126 | 12 | Work Offset Positioning Coordinate #23 |
| G127 | 12 | Work Offset Positioning Coordinate #24 |
| G128 | 12 | Work Offset Positioning Coordinate #25 |
| G129 | 12 | Work Offset Positioning Coordinate #26 |
| G136** | 0 | Automatic Work Offset Center Measurement |
| G141 | 7 | 3D+ Cutter Compensation (X,Y,Z,I,J,K,D,F) |
| G143** | 8 | 5 Axis Tool Length Compensation+ (X,Y,Z,A,B,H) (Setting 117) |
| G150 | 0 | General Purpose Pocket Milling (X,Y,P,,Z,I,J,K,Q,D,R,L,S,F) |
| G153** | 9 | 5 Axis High Speed Peck Drill Canned Cycle (X,Y,A,B,Z,I,J,K,Q,P,E,L,F) (Setting 22) |
| G154 | 9 | Select Work Offset Positioning Coordinate P1-99 |
| G155** | 9 | 5 Axis Reverse Tapping Canned Cycle (X,Y,A,B,Z,J,E,L,F) |
| G161** | 9 | 5 Axis Drill Canned Cycle (X,Y,A,B,Z,E,L,F) |
| G162** | 9 | 5 Axis Spot Drill/Counterbore Canned Cycle (X,Y,A,B,Z,P,E,L,F) |
| G163** | 9 | 5 Axis Peck Drill Canned Cycle (X,Y,A,B,Z,I,J,K,Q,E,L,F) (Setting 22) |
| G164** | 9 | 5 Axis Tapping Canned Cycle (X,Y,A,B,Z,J,E,L,F) |
| G165** | 9 | 5 Axis Bore in, Bore out Canned Cycle (X,Y,A,B,Z,E,L,F) |
| G166** | 9 | 5 Axis Bore in, Stop, Rapid out Canned Cycle (X,Y,A,B,Z,E,L,F) |
| G169** | 9 | 5 Axis Bore, Dwell, Bore out Canned Cycle (X,Y,A,B,Z,P,E,L,F) |
| G174 | 0 | Special Purpose Non-Vertical Rigid Tapping CCW (X,Y,Z,F) |
| G184 | 0 | Special Purpose Non-Vertical Rigid Tapping CW (X,Y,Z,F) |
| G187 | 0 | Accuracy Control for High Speed Machining (E) |
| G188 | 0 | Get Program From PST (Program Schedule Table) |
| * = Defaults | ||
| ** = Optional | ||
| M-CODES | FUNCTION | |
| M00 | The M00 code is used for a Program Stop command on the machine. It stops the spindle, turns off coolant and stops look-a-head processing. Pressing CYCLE START again will continue the program on the next block of the program. | |
| M01 | The M01 code is used for an Optional Program Stop command. | |
| Pressing the OPT STOP key on the control panel signals the machine toperform a stop command when the control reads an M01 command. It will then perform like an M00. | ||
| M03 | Starts the spindle CLOCKWISE. Must have a spindle speed defined. | |
| M04 | Starts the spindle COUNTERCLOCKWISE. Must have a spindle speed defined. | |
| M05 | STOPS the spindle. | |
| M06 | Tool change command along with a tool number will execute a tool change for that tool. This command will automatically stop the spindle, Z-axis will move up to the machine zero position and the selected tool will be put in spindle. The coolant pump will turn off right before executing the tool change. | |
| M08 | Coolant ON command. | |
| M09 | Coolant OFF command. | |
| M30 | Program End and Reset to the beginning of program. | |
| M97 | Local Subroutine call | |
| M98 | Subprogram call | |
| M99 | Subprogram return (M98) or Subroutine return (M97), or a Program loop. | |
NOTE: Only one "M" code can be used per line. And the M-codes will be the last command to be executed in a line, regardless of where it's located in that line.
RELATED PAGES
FREQUENTLY ASKED QUESTIONS
What's the difference between a G-code and an M-code?
G-codes primarily control tool motion and geometry (where and how the tool moves); M-codes control miscellaneous machine functions such as spindle control, coolant, and program flow. A typical program line often combines both, e.g. a G-code move alongside an M-code turning on coolant.
Are G-codes and M-codes standardised across all machines?
Many common codes (G00, G01, G02/G03, M03, M05, M08, etc.) are broadly consistent across most controllers, but not universally — some codes vary by manufacturer or are repurposed for different functions on different controllers. Always verify against your specific machine's programming manual, especially for less common codes.
Do I need to know G-code if I use CAM software?
Not in detail for everyday programming, since CAM software generates the code automatically, but understanding the basics is valuable for troubleshooting, editing programs by hand, and verifying that the post-processor output is correct for your machine.

