How to insert control characters in Vim (and have them printed through cat)

The other day, I was studying about the Linux du command when I stumbled upon its --files0-from option that enables the tool to accept input file/directory names written in a file. The option has an associated condition that names specified in the file should be NUL terminated.

Now, it was when I started entering names in a newly-created file through VIM, I realized that I don't know how to enter the NUL character while inside the editor window. So I researched a bit, and gladly, found the solution.

So here's how you do this:

Firstly, bring the cursor exactly at the point inside the Vim editor window where you want to enter the control character. Then, press ctrl+v, which tells the editor to insert the next character literally. Now, just type the control character - for example, ctrl+shift+2 in our case produced the ^@ character which is treated as NUL.

vim-nul-character

That's it. And just in case you want the cat command to print these kind of characters present in a file, use the -v option.

cat -v [file-name]

cat-v-option

To access more such Vim-related tips here at FAQForge, check out our Linux&Unix section.