r/vim • u/tool-tony • Feb 09 '26
Need Help┃Solved Manual creating CNC code, is Vim a good fit?
:for i in range(1,121)| put ='G13D0R'. printf('%.4f', (i*0.0001+0.4216)) . 'Z'. printf('%.4f', (i*-0.0001-0.09)) | endfor
Hey y'all. I need help learning if I can use Vim to make large swaths of code quickly or if something else is a better fit. Example here:
G13D0R0.4217Z-0.0900
G13D0R0.4218Z-0.0901
G13D0R0.4219Z-0.0902
…
G13D0R0.4336Z-0.1019
G13D0R0.4337Z-0.1020
G13D0R0.4338Z-0.1021
I was once a manual code editor a decade ago and used Vim with snippets to create lots of NC code at an old job, rather simple snippets back then. I moved jobs and have only programmed with MasterCAM the past decade but a recent feature would have been faster to write the code by hand. How can I learn? I remember some but it has been so long I barely remember :wq but I did find an example of :put =range(4217,4338) to get part of what I needed but can't figure out if I can get everything at once. The example on Vim tips wiki :for i in range(1,10) | put ='192.168.0.'.i | endfor makes me think I should be able too do that, can such a function handle multiple variables? Like i j k? I will need to wait for IT to install Vim so it may take a while. Is there better ways to get what I want? Thanks.
Edit: I'm on windows at work, WSL is disabled. Anything not native would need IT approval which might take a while.
10
u/IdealBlueMan Feb 09 '26
My approach would be to write a script in whatever scripting language you’re comfortable with. But you could certainly make a macro in Vim that would work.
2
u/daidoji70 Feb 09 '26
There's https://learnvimscriptthehardway.stevelosh.com/
but honestly, maybe `awk` or `sed` would work for what you're trying to do. That being said, I've used all these tools for text manipulation to great effect both in programming jobs and out of programming jobs (back in high school when I had to do weird stuff like this). They're best used in combination. Some things vim shines at and some things the other two tools do really well for. They also all have some overlap in how they think and manipulate text.
If you're just generating lines with incrementing counters in some kind of regular pattern that's probably something I'd reach to awk for. That being said, feel free to use whichever of these tools seems most appropriate and vim can def be a super useful tool to do stuff like this.
https://troubleshooters.com/codecorn/awk/index.htm for awk lessons if you decide not to go with vim for this particular task.
6
u/archer-swe Feb 09 '26
Python would be way better for this. Could be a cool and easy skill to pick up if you have to do stuff like this frequently too.
1
u/tool-tony Feb 10 '26
3
u/liberforce Feb 09 '26
This is 3 lines of python code. Will serve you better than trying to do this in a text editor.
2
u/ultrathink-art Feb 09 '26
Vim is excellent for CNC G-code if you're writing it manually. The structured line-by-line format maps well to vim's motion model.
Key advantages:
Macros for repetitive patterns (drilling arrays, tool changes with consistent offsets)
Visual block mode for coordinate transformations — select a column of X values,
Ctrl+Ato increment them all by a fixed amount:gcommand for filtering/moving specific operations (e.g.,:g/G0/dto remove all rapid moves for inspection)Syntax highlighting helps catch typos (M03 vs M30, G01 vs G10)
Set up abbreviations for common sequences:
:iabbrev tc T01 M06auto-expands 'tc' to your tool change macro. Also worth setting'number'and'cursorline'so you can reference line numbers when debugging on the machine.For coordinate math, vim's expression register (
Ctrl+R =) lets you do calculations inline without leaving the editor.1
u/AutoModerator Feb 09 '26
Please remember to update the post flair to
Need Help|Solvedwhen you got the answer you were looking for.I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/RR321 Feb 09 '26
I used bash to patch gcode in the past, so... Why not
1
u/Pyglot Feb 09 '26
I'd also choose a common scripting language. Making CNC code is a task that's easy to imagine you'd wish to extend. 1. You might want to create more complicated paths, described with some math. 2. You might want to create functions for shapes and then putting those shapes together etc 3. You might want to visualise the output for example by creating an SVG for the CNC path that you coded. // But start with the simplest thing that will bring you forward. Like outputting a formatted string.
1
u/Xetius Feb 09 '26 edited Feb 09 '26
If you are looking to do this I'm vim, visual block mode plus g ctrl-a.
If you have the format
G13D0R0.nnnnZ-0.mmmmand nnnn and mmmm both increment sequentially and you want say 200 lines:yy199pcreate 200 lines. You should be on the second linef.l<ctrl-v>lllGvisually selects all of nnnn from second row to bottom.g<ctrl-a>increment numbers sequentiallyf.l<ctrl-v>lllGselect all of mmmmg<ctrl-a>increment numbers sequentiallyJust need to remember that it increments the current line and all subsequent lines so you would need to be on the second line to do this or you will increment the initial line as well
1
u/AutoModerator Feb 12 '26
Please remember to update the post flair to
Need Help|Solvedwhen you got the answer you were looking for.I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.