Select Page

Introduction

If you use Vim, it should not surprise you that Vim tries to stay as uncommon as it can. Following this very root, Vim will not help you in undoing stuff if you try the traditional Ctrl + Z to do so. Vim uses the letter “u” instead. But unfortunately, many code-sters could find this a bit inconvenient if they’re not used to it, and might wish to have Ctrl + Z mapped for the job. Wanna hear some good news?

Vim allows you to customize every aspect of it, marking no difference in occasions like this one either. You can map Ctrl + Z to undo things in Vim, if you want. But there is an existing use case for Ctrl + Z assigned already. If you don’t want that, you may happily reassign it, but before you do, it’s best you hear me out first!


Use of Ctrl + Z in a Terminal

This does not resolve much around what we’re actually here for, but just so you know. Using Ctrl + Z in a terminal directly (while Vim is not running), the terminal will detach the currently running program, and keep it in the background in a stationary mode. What this means is that any shell commands or programs, or whatever that you may be running inside a terminal, it’ll pause the command and send it to the background.

Once you’re done with whatever other task you wanted to do, you can return to that previous command by typing “fg” on your terminal and then pressing the Enter key.

This is pretty much exactly what happens when you press Ctrl + Z inside of Vim, but more on that later.


Use of Ctrl + Z in regular applications

In regular GUI applications, Ctrl + Z has been traditionally mapped to do the undo command for ages, dating as far back as the ancient 1970s (hehe). The company Xerox had first publicly introduced this command in 1974, which Microsoft’s Excel had followed up later in the year 1985, assigning Ctrl + Z to undo things in a Mackintosh Machine. Since then, this shortcut has taken pretty much every computer application that involves undoing. Even android apps can be undone if you can invoke a control command on it! Unfortunately, The Vim philosophy does not really like to steal things from others. Therefore, not assigning Ctrl + Z as a shortcut for undoing, even to this date, while it is still being regularly updated in this Modern generation of mouse clicking nerds. Pretty neat.


Use of Ctrl + Z in Vim (Normal Mode)

This is where it all gets interesting. Because Vim has most of these shortcuts set to do something different than the terminal does, even though it’s run from the terminal. Vim is able to bypass any terminal command that’d provoke a program being run from, but Vim, having full control can neglect them and run its own commands instead of a given shortcut. If you try commands like Ctrl + C inside of Vim, you’ll see that it does not close the Vim session, like it would do for other terminal commands whatsoever.

Now we come to the real deal. While in Vim Normal Mode, of you press Ctrl + Z, you’ll see that the session closes and shows a Stopped message, which should look something like this:

Now don’t worry if you see this, because it wouldn’t mean that all the code you’ve been writing for hours having forgotten to save them, will be erased. It won’t. Simply type “fg” while in the terminal, and press Enter, you’ll be brought back to where you left off, all unscratched. Now quickly save your file before you face another disaster.

So here’s the point. If Vim can bypass other terminal commands, why does it not do so for Ctrl + Z? It appears it is pretty much an intended feature. This could be useful for certain use cases. Let’s say, you want to execute a few commands behind the scenes without quitting the current Vim session, and you cannot open another terminal for the task, which is common if you are using TTY, or accessing another machine from an SSH server. At such times, you can willfully detach the current Vim session to be kept in the background, then run any terminal commands you wish. Afterwards, you can return to the detached session by typing fg as already shown earlier.

You can also kill the detached vim session if you wish by running this command:

kill %1

Remap Ctrl + Z to undo in Vim

If you don’t need this feature for yourself, you can change it to do just the thing you do elsewhere, which is to undo. Now it does not have to be just that, you can assign anything you wish. But for the most folks out there, I suppose adding this below line on their .vimrc will do the job very well:

nnoremap <C-z> u 

One thing to note is that this will assign Ctrl + Z to undo things only when in Normal Mode, which will override the default behavior of detachment. To make it also work in Vim Insert Mode, add one more line below the previous one:

inoremap <C-z> <C-o>u

This will now let you undo in Vim the way you would do pretty much everywhere else, as long as it’s not your friend’s Vim configuration, who probably hasn’t set Ctrl + Z for undoing.


Conclusion

A lot of folks have posted their issues on the internet stating Ctrl + Z to be a problem they have faced. However, it is not an actual issue as long as you understand what it’s used for. Many people would probably find this to be a useful feature, especially the server guys. But it doesn’t mean you have to use it the way other regular Vim users would.

Just Remap. Think Peace. Live Life.