This is unbelievably annoying, since when I code online, there is no means of undoing, and thus retrieving the text I deleted.
2 Answers
Most online text editors which support keyboard shortcuts (like the Stack Exchange editor right here!) support ⌃Z (ctrl-Z) for undo too, which won't cause any system functions to be fired.
If you want to use ⌘Z, you can remap the Undo Close Tab function off ⌘Z to something else using System Preferences → Keyboard → Shortcuts → App Shortcuts. Set Application to Safari.app, Menu Title to Undo Close Tab and Keyboard Shortcut to something other than ⌘Z.

- 201,078
If you use a language other than English and the proposed solution by grg doesn't work, specify in System Preferences > Keyboard the equivalent to "Undo Close Tab" in your language.
For example, if Safari's interface language is Russian, open a tab, close it, select the Edit menu:
and use the text next to CommandZ (that is, 'Отменить закрытие вкладки') instead of "Undo Close Tab".

- 15,010

- 31
Undo Close Tab
then remap the key. – frumbert Sep 28 '23 at 07:24