The > /dev/null part explicitly throws away the standard output, since, as I said, we don't need to pass anything to another piped command. " Allow saving of files as sudo when I forgot to start vim using sudo. vimrc to make this trick easy-to-use: just type :w!!. We don't need to pass anything to another piped command in this case we're just using tee as an alternate way of writing a file and so that we can call it with sudo. sudo tee writes to our file and also sends the buffer contents to standard output, but we ignore standard output. In the situation your question describes, using tee is a hack because we're ignoring half of what it does. Understanding teeĪs for tee, picture the tee command as a T-shaped pipe in a normal bash piping situation: it directs output to specified file(s) and also sends it to standard output, which can be captured by the next piped command.įor example, in ps -ax | tee processes.txt | grep 'foo', the list of processes will be written to a text file and passed along to grep. If Vim wasn't run with sudo access, its :w can't modify a protected file, but if it passes the buffer contents to the shell, a command in the shell can be run with sudo. For instance, :w !cat will just display the contents. Instead of file2.txt, you can substitute a shell command to receive the buffer contents. If you opened and modified file1.txt, then ran :w file2.txt, it would be a "save as" file1.txt wouldn't be modified, but the current buffer contents would be sent to file2.txt. One confusing part of this trick is that you might think :w is modifying your file, but it isn't. For example, :%s/foo/bar means " in the current file, replace occurrences of foo with bar." If you highlight some text before typing :s, you'll see that the highlighted lines take the place of % as your substitution range.) :w isn't updating your file (In substitution commands, it's slightly different as :help :% shows, it's equal to 1,$ (the entire file) (thanks to for pointing out that this does not evaluate to the filename). % means "the current file"Īs eugene y pointed out, % does indeed mean "the current file name", which is passed to tee so that it knows which file to overwrite. Instead, create a vim alias for this command. It's too long and complicated to remember. If anyone asked me to write this command, I wouldn't be able to. The >/dev/null part discards tee's stdout as you don't need to see it in vim. The tee command now runs in a privileged environment and redirects its stdin to FILENAME. What happens here is vim spawns sudo tee FILENAME and pipes the contents of the file to its stdin. The special symbol % means the filename of currently open file. ![]() In this case the command is sudo tee % >/dev/null. Means – write currently open file to stdin of command. That works but here's another method that you can use without quitting vim: ![]() If you're an intermediate vim user, then you save the file to /tmp directory:Īnd then you sudo move the /tmp/foo to the right location: "/etc/apache/nf" E212: Can't open file for writing You open a file and you forget to use sudo: How many times have you had a situation when you open a file for editing, make a bunch of changes, and discover that you don't have the rights to write the file?
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |