It is very dangerous to run VScode as root (as it is any other application) however, should you absolutely need to:
You must specify the user data directory to use when running vs code:
sudo code --user-data-dir="~/.vscode-root"
Then from the window you can open your project folder
Again: This is not recommended. Have fun.
Update May 2018
For everyone asking why this isn't recommended, you clearly don't understand why sudo
even exists. From this AskUbuntu Question:
It defeats the security model that's been in place for years. Applications are meant to be run with non-administrative security (or as mere mortals) so you have to elevate their privileges to modify the underlying system. For example, you wouldn't want that recent crash of Rhythmbox to wipe out your entire /usr directory due to a bug. Or that vulnerability that was just posted in ProFTPD to allow an attacker to gain a ROOT shell.
It's just good practice on any operating system to run your applications on a user level and leave administrative tasks to the root user, and only on a per-need basis.
I'm not saying that you should never use VSCode as root(though its pretty easy to avoid doing) I'm saying you should be careful when you want to do so and know the risks.
The absolute best thing to do to is make a copy of a restricted file, edit it, and copy it back when you're absolutely sure it's finished.
Also for editing files to which your user does not have permissions, I would now recommend Talha Junaid's answer which asks for permissions every single time you want to save a file. The prompt for root access did not exist when I wrote this answer.
It should be called code
and for me, I just have to run it.
$ code
$ code my-file
and that works. If installed the .deb (only available in older Ubuntu versions) the path should be
`/usr/bin/code`
If you installed it as a snap, e.g.
$ sudo snap install code --classic
Then it's path should be
`/snap/bin/code`
If you installed it another way, then try to find the binary, e.g.
command -v code
will print the path. command
is a bit more reliable than which
. VS Code is an Electron application (i.e. Chrome/node) and the launcher is a little wonky. For example, xdg-open
does not work well for me.
NOTE ABOUT INSIDERS VERSION:
As Nicholas Humphrey points out in the comments, it could also be called code-insiders
if you installed the insiders version. This allows you to have side-by-side installations of regular and insider versions.
Best Answer
You can try with adding the
-H
parameter for sudo:That would be:
This way,
~/.zshrc
would still refer to your calling users.zshrc
, but the home directory forcode
would appear as the one for root (~root
).Although what you are doing (running vs code as root) sounds wrong. Maybe you have your reasons, but in general it is not recommended to run programs as root.