If your sudoers policy allows you to run vi then you've already failed.
sudoedit(8) has been around for a long time and it solves this problem.
sudoedit(8) has been around for a long time and it solves this problem.
The idea for sudoedit is that it allows you to allow non-root users to safely edit files, and if they shell out of their editor then they've not gained root rights - they edit a temporary file as a regular user and then sudo moves the file into place after it is edited.