Skip to content

Instantly share code, notes, and snippets.

@nick96
Last active May 27, 2017 05:29
Show Gist options
  • Save nick96/0637c1b35dcf1718887a60dbdad0a251 to your computer and use it in GitHub Desktop.
Save nick96/0637c1b35dcf1718887a60dbdad0a251 to your computer and use it in GitHub Desktop.
Coded up an assignment in C99 but the uni server is only C89. One of the main issues was declaring counters in the for-loop. This the regexp I used in VIM to move counteres outside.
" This was a massive help for me cause I could just run one command on a file and it would compile to C89.
" Hopefully this is useful to anyone who runs into the same problem :)
" It's a bit long winded so I put an explanation below:
" 1 = spacing, this allows the counter declaration to be easily indented the same amount no matter the
" indentation.
" 2 = 'for ('
" 3 = the counter type
" 4 = is for if the counter is pointer, like if you're iterating over a linked list
" 5 = the counter variable name
" 6 = the rest of the for-loop
%s/\(\s\+\)\(for (\)\(\w\+\) \(*\?\)\(\w\+\)\( .\+\)/\1\3 \4\5;\r\1\2\5\6/
" \s = whitespace
" \+ = one or more
" \? = one or none
" . = anything but an end of line character
" \<n> = refers to the nth grouping between \( and \)
@nick96
Copy link
Author

nick96 commented Apr 2, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment