brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: :w !sudo tee % saves a file you opened without sudo

Linux and shell
~/tips/vim-sudo-write

:w !sudo tee % > /dev/null

Saves a file that vim opened without permission, using sudo from inside the editor.

It happens to everyone: you edit a config file in vim, try to save, and get a permission error. Quitting and typing everything again is the slow way out.

:w !sudo tee % > /dev/null

:w !cmd does not write to the file but sends the buffer to a command. sudo tee % then writes it to the current file name as root, and the redirect hides tee’s echo. Vim will say the file changed on disk. Choose L to load it again.

Caveat: it is reported not to work in Neovim, which cannot show the sudo password prompt. For files you edit as root often, sudoedit is the safer habit, because you edit a copy as yourself and it saves with the right permissions.

Further reading: Vim documentation

Going deeper: Guide: advanced tmux, a config that grows with you, formats, layouts and more

More Linux and shell tips zoxide query -ls project eval "$(fzf --bash)" comm --total a.txt b.txt diff -r -x .git -x '*.log' one two printf '%s\n' a b c d e f | xargs -n1 -P3 --process-slot-var=SLOT sh -c 'echo "slot $SLOT got $0"; sleep 0.2' All Linux and shell tips (178) All topics

Link