dotfiles/notes/vim/tee.md
2022-09-07 22:24:18 -06:00

797 B

Tee command

Duplicate piped output between a file and the stdout

sudo echo "something" > root_file

This won't work since output redirection is always done with user privileges. Instead we use:

echo "something" | sudo tee root_file
echo "something" | sudo tee root_file &>/dev/null

Now the redirection will be executed with root privileges. Stdout can be silenced to get the same effect as before. Tee can also copy output to multiple files, or act like >> with -a

echo "something" | tee file1 | tee file2 > file3

It's very useful in vim, when you forget to use root privileges when editing a root-only file:

:w ! sudo tee %

Note that neovim doesn't support interactive sessions through external commands yet, so this only works in vim