How to open Visual Studio Code from the command line on linux? - visual-studio-code

I know I can use command "code" to open VS code or file, but I don't know what should I do to make it possible after I install VS code in Ubuntu.Thanks.

Launching from the Command Line
You can launch VS Code from the command line to quickly open a file, folder, or project. Typically, you open VS Code within the context of a folder. We find the best way to do this is to simply type:
code .
Tip: We have instructions for Mac users in our Setup topic that enable you to start VS Code from within a terminal. We add the VS Code executable to the PATH environment variable on Windows and Linux automatically during installation.
Sometimes you will want to open or create a file. If the specified files does not exist, VS Code will create them for you:
code index.html style.css readme.md
Tip: You can have as many file names as you want separated by spaces.
Source: https://code.visualstudio.com/Docs/editor/codebasics

So, there are a couple of solutions for this.
I've linked a video that shows you how to add vscode to $PATH
(which didn't work for me because I couldn't find the "shell:install path" command)
I uninstalled the vscode from my ubuntu and re-installed using sudo snap install --classic code
(This method worked for me)
Tell me which one works for you... and if you have extensions installed to your vscode then i guess you ought to make a backup or something.
Link to the video: https://youtu.be/iP5FKZXtDBs

Related

Visual Studio Code, code command does not start new file

I'm having an issue where if I do code <filename> where the file does not already exist, it does not create a new file.
Normally, vscode would (almost) create a new file, I could write some code, and then save it and it would appear in my file manager. But this just stopped working.
I had a look through all my extensions and uninstalled all the recent extensions. I've reinstalled vscode, still no luck.
I am using windows with wsl, this affects me when running vscode both in windows and in wsl.
Did some googling around, can't find anything useful. Any advice?
I figured out what caused this in case anyone else is having the same problem.
I installed Rust in windows and that seemed to have messed up the code command.
Sorry, can't remember which version it was, I just uninstalled it on windows and noticed that it fixed it.
(p.s: Rust is great, but I have it installed in WSL so don't need it in windows.)
If you are using npm on windows, you can install touch by running npm i -g touch and creating a folder in a specific location by navigating in the location and creating by mkdir <foldername> navigating inside it and using touch <filename>. From there you can simply launch VS Code by running code . in that folder namespace
If you are using WSL (Ubuntu), it comes preinstalled and you can run mkdir <foldername> && touch <filename> && code .
Not exactly what you are looking for but it can work!

setting up visual studio code for c++, but unable to compile

I have installed visual studio 2019 which works fine for c++ but unable to create folders and sections to store multiple code files. So thought of switching to visual studio code and followed all tutorials available on youtube and documentation. But Visual studio IDE has got nothing showing up when Run Build task is initiated, which is expected to show the c++ extension downloaded. Mingw64 is installed and checked in the terminal too.
no output or terminal opened on Run
compiler installation checked
From the image attached regarding no output in the terminal or terminal opened, it looks like the exe is made because it says 'Build finsihed succesfully'. You'll need to run this executable from the terminal.
For this, if youre in the same directory all you have to do is type the filename. helloworld.exe in the cmd, if its powershell, then ./helloworld.exe. (Also note that for exe files you dont need to explicitly mention .exe at the end of the file name)
The powershell method should work on cmd too.
And if youre not in the same directory as the executable, you can either change directory by typing cd path/to/file and proceed as above, or run directly by typing path/to/file/helloworld.exe. where path/to/file is the directory of your helloworld.exe file

Visual Studio Code Love2D support extension detecting love.app as a directory

I have just started with löve because of an internet course I'm on, and I'm using Visual studio code as my editor. The course recommends that I use the "Love2D Support" extension with Visual Studio. It allows me to run the code directly from VS by pressing Command + L.
Although when I do it, VS outputs me this error:
The setting specified in pixelbyte.love2d.path must be an executable file, not a directory. Check your settings.
The path I have input in the settings is /Users/My_Username/Desktop/love.app
I am 100% sure that it is the correct directory. What is the problem?
Thanks for answering!
The actual executable file in Mac is /Applications/love.app/Contents/MacOS/love so put this in your pixelbyte.love2d.path command in vscode and you are good to go.

VSCode Can't Find the PATH To the Flutter SDK

I just tried to execute Flutter: New Project, but it shows an error that could not find a flutter SDK.
I've ensured to complete all of the requirements from the flutter doctor.
Is there any way to solve this problem?
Thanks.
On macOS this was solved by following these steps.
First find where you downloaded the flutter sdk (Can be downloaded from here). It should be in your downloads folder unless you selected somewhere else.
Next:
Open up Terminal. Run the following command: sudo nano /etc/paths
Enter your password, when prompted.
Go to the bottom of the file, and enter the path you wish to add.
Hit control-x to quit.
Enter “Y” to save the modified buffer.
If the sdk is downloaded into your Downloads folder, your path to add should be /Users/yourusername/Downloads/flutter/bin
Lastly, quit and restart your terminal windows and VS Code. After restarting, both apps should recognize flutter and running flutter doctor should now work.
If you installed Flutter extension please restart VS_CODE and try it again. I already had the same problem.
The thing is VSCode looks for .packages file in your Flutter project this file contains path to your Flutter SDK & some other metadata. You're getting this error because either the .packages file is missing or either the path to Flutter SDK in .packages file contains some discrepancies.
To resolve it, you can;
Either edit the .packages file & set the path.
Or if you already have a working Flutter project then a .packages must've been already created. Copy that file & paste it in the root directory of your project.
Restart VSCode & run flutter pub get
This should resolve the issue.
Check your environment variables:
Look for the PATH if it exists append the full path to flutter\bin
using ; as a separator from existing values.
If it does not exist, create a new user variable named PATH with the
full path to flutter\bin as its value.
And check the installation guide
After doing this:
Open VS Code.
Press Ctrl + Shift + P.
Then type flutter and select run flutter doctor it should be fixed now.
Create a new project to make sure it's run.
For Linux Users
For future visitors :)
I had the same problem, in my Terminal flutter doctor or in general flutter was recognized, but VSCode couldn't recognize my SDK location so flutter doctor for instance didn't work.
The first thing to do is to follow the steps in flutter documentation: Flutter Doc
If it didn't work, change the environment variables directly.
sudo nano /etc/environment
Now add the location of the bin folder of your flutter SDK, The folder that you downloaded from here.
For example, you extracted it here: /Home/User/Software
Add /Home/User/Software/flutter/bin to the PATH variable. Paths are separated by a colon(:)
After a few solutions I tried, this one worked.
For Windows
Open Visual Studio Code
Click Locate SDK
Find your flutter folder
Click Save
Re-open your Visual Studio Code
Press Ctrl + Shift + P
Type flutter and choose Flutter: New Application Project
Type your project name (e.g: flutter_application_1)
Select a folder to create the project in
If you want to change your flutter sdk location
Go to Visual Studio Code > Manage > Setting (or with shortcut Ctrl
Search dart
Find [dart] configure setting to be overriden for [dart] language
Click Edit in settings.json
You'll see :
{
"dart.sdkPath": "C:\\tools\\dart-sdk",
"dart.flutterSdkPath": "C:\\tools\\flutter",
"[dart]": {
"editor.tabSize": 2,
"editor.insertSpaces": true,
"editor.detectIndentation": false,
"editor.suggest.insertMode": "replace"
}
}
Replace dart.sdkPath and dart.flutterSdkPath with your new sdk location
Installing Flutter on Linux is a breeze using snapd as is documented officially. But then trying to create a Flutter project by following the official steps as mentioned here - Test drive - Flutter will throw an error similar to the description provided in the question.
On a side note though, you don't have to bother adding Flutter to PATH if it was installed using snapd.
Rather, follow these steps:
flutter create <insert_app_folder_name>
cd <insert_app_folder_name>
code .
What it really does is:
The Flutter binary is called from within snapd & creates a project with a directory name you provide.
You can now cd into that specific directory.
Call VSCode in the current directory which opens a VSCode instance within the project directory.
Refer to the answer by #maheshmnj about the .package. This file was created by Flutter earlier if you followed the aforementioned steps.
This means the Flutter extension couldn't find the Flutter SDK automatically (eg. by looking in your PATH or a path configured with dart.flutterSdkPath). Clicking Locate SDK should let you browse to the SDK manually and record that location for the next time. If you see this again after restarting, that's definitely a bug in the Dart/Flutter extension, so please open an issue at https://github.com/Dart-Code/Dart-Code and include the output from clicking the Show Log button, as well as noting whether there's a dart.flutterSdkPath setting in your VS Code User Settings.
If you have just installed the flutter extension, close all the vs code applications running and restart the vs code then it will work as expected.
In order to run a new flutter project,
Go to vs code-> View tab -> command pallette -> select Flutter ->
give a name to the project ->give the path of the project
Now you are good to go with flutter.
I would like to complete NoobN3rd information.
In VSCode Linux I used this steps:
sudo nano /etc/environment
Type your password
I added 3 paths at the end of line:
3.1 /"flutter instalation directory"/flutter/bin
3.2 /"flutter instalation directory"/flutter/packages
3.3 /"flutter instalation directory"/flutter/dev
Save it and reboot your system
Look at my environment file to have an example:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/paulo/development/flutter/bin:/home/paulo/development/flutter/packages:/home/paulo/development/flutter/dev"
if youknow where have you extracted your zip file of dart(Eg. at the time of extraction i setup the path C:\src\flutter)
then in vs code goto
c drive
-src
-flutter
-bin
-cache
-(dart-sdk)
select this folder and your
vs code will be adjusted.
I was facing the same problem. I've initially followed all the steps provided by installation guide of Flutter and was able to run an existing project using flutter run command.
So I've tried again repeating the very same steps of the installation guide and changed the path that I initially set like:
export PATH="$PATH:~/path/to/flutter/bin"
For one like:
export PATH="$PATH:/home/user/path/to/flutter/bin"
Then I run source ~/.bashrc and restarted Visual Code and worked! so I suppose that it was something related to ~ in the path.
I would like to add a complementary and alternative response to the Linux solutions given by NoobN3rd, Traufvihal and Paulo Ravaiano.
I'm using Manjaro and for some reason my Visual Studio Code was not picking up all the environment variables definition. I tried all the solutions and finally I realised the problem was not with the variables definition (my final solution contains them all in ~/.bashrc but it should work with /etc/environment as well) but on how Visual Studio Code was launched.
In case someone else is facing this problem, I noticed that running VS Code from the applications menu fails to link the environment variables for some reason. On the other hand, launching "code-oss" from the terminal does the trick and picks all my variables defined in my ~/.bashrc file.
Hope it helps!
I just got out of the same trouble, after numerous hit and trials trying to run flutter commands from vs code terminal the below set of steps got the things up for me.
In Ubuntu 18.04
Grab the path of the directory where flutter was unpacked by you.Mine was in the Home directory for the root user. Location can be any of your choice.
Go to the Home for your root user
Un-hide all of the hidden files in this directory.
Look for .bashrc file. Do not look for .profile file.
Open .bashrc file using nano/vim/text editor which ever you like.
Scroll to the bottom of the file and add this line -
export PATH="$PATH:pwd/flutter_linux/flutter/bin"
Quit all instances of the vscode (if any) opened.
Re-open and in the terminal type which flutter
Here -
I had re-named the un-zipped folder name like "flutter_linux_v1.12.13+hotfix.9-stable" with "flutter_linux"
Hope this helps......
i was also facing this error, after putting the flutter sdk path at the End of the PATH variable(not in middle or start) it works fine for me
For those who want to change their Flutter SDK path for the Dart & Flutter plugin
Click on Manage Icon(on Mac)
Click on Settings
On the Settings page, Click on Dart & Flutter.
Look for Dart: Sdk Path, click on Edit in settings.json
Edit dart.flutterSdkPath to your VSCode Flutter path
For me non of the answers helped.
What helped was (on macOS) opening the .bash_profile file via
sudo nano .bash_profile
and change the path to where my Flutter was as this has changed. Or add this line export PATH="$PATH:/home/user/path/to/flutter/bin"
hope this helpes.
QUICK SOLUTION!: If on macOS, PLUS everything had been working flawlessly previously, and this happens all of a sudden, or periodically, here is what I eventually discovered...
If your mac restarted, either on its own due to a crash, freeze or overnight install of updates, and VS Code was running, it generally restores all your vscode instances, but with various environment-related errors such as this one. The reason is that you are not running in the context of a shell and you don't have access to those environment settings. See this article from the VS Code documentation.
THE SOLUTION?: Quit and restart VS code as you normally would. Others have mentioned this, but I thought it would be good to know why this happens so you aren't surprised next time.
Please refer to the Get Flutter SDK section and locate where you have installed your flutter\bin folder. In my case, I put the flutter bin in my Documents folder.
See the following image Flutter\bin location
Windows
After make sure right Flutter version is in your path: https://docs.flutter.dev/get-started/install/windows
and you can run flutter doctor successfully
in VS Code you need to go to File->Preferences->Settings (or Ctrl+,)
then search for dart.flutterSdkPath then click on "Edit in settings.json" make sure it is the right path there.
I had the same problem, the solution was:
take the folder where you have extracted your zip file (for example: C:\src\flutter\flutter) then put it in the environment variable path and restart vscode.

How to call VS Code Editor from terminal / command line

The question says it all.
How can I open VS Code editor from
windows cmd
linux and mac terminal
e.g. for notepad++ I write
> start notepad++ test.txt
By the way, the editor is awesome (cross-platform)! Thank you Nadella!
You can download it from microsoft
To open a file or directory use the command:
code /path/to/file/or/directory/you/want/to/open
For macOS users, it needs to be installed manually:
Launch VS Code.
Command + Shift + P to open the Command Palette.
Type shell command, to find the Shell Command: Install 'code' command in PATH and select to install it.
Restart your terminal.
Per the docs:
Mac OS X
Download Visual Studio Code for Mac OS X.
Double-click on VSCode-osx.zip to expand the contents.
Drag Visual Studio Code.app to the Applications folder, making it available in the Launchpad.
Add VS Code to your Dock by right-clicking on the icon and choosing Options, Keep in Dock.
Tip: If you want to run VS Code from the terminal, append the following to your ~/.bash_profile file (~/.zshrc in case you use zsh).
code () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* ;}
Now, you can simply type code . in any folder to start editing files
in that folder.
Tip: You can also add it to VS Code Insiders build by changing "com.microsoft.VSCodeInsiders". Also if you don't to type the whole word code, just change it to c.
Linux
Download Visual Studio Code for Linux.
Make a new folder and extract VSCode-linux-x64.zip inside that folder.
Double click on Code to run Visual Studio Code.
Tip: If you want to run VS Code from the terminal, create the following link substituting /path/to/vscode/Code with the absolute
path to the Code executable
sudo ln -s /path/to/vscode/Code /usr/local/bin/code
Now, you can simply type code . in any folder to start editing files
in that folder.
VS Code is a must have code editor for 2018
For Windows 10 users a lot is possible, the same way the Mac OS users type code . .
Look for you VS Code \bin folder path e.g C:\Program Files\Microsoft VS Code\bin . The bin folder includes a file called code.cmd .
If you are not sure about what is your path, type where code.cmd, and then, copy it without the \code.cmd after the ...\bin.
Follow the steps below and be proud of the OS you use.
Search for "Advanced System Setting" from Start.
Click on Environment Variables.
On System Variables choose "path" from Variable tab and click on Edit.
Click on New on the right side of the popup window.
Copy your path from the Explorer's breadcrumb path and paste it into the new opened path in step 4, example:- C:\Program Files\Microsoft VS Code\bin.
Click Ok on all the open windows to confirm changes and restart your cmd .
Go to your cmd and navigate to you working directory on server and type code . .
C:>cd wamp64\www\react-app> code . to open with VS Code on Windows.
Visual Studio Code also includes a command prompt (terminal) window and you can open one or more of them with:
Ctrl + ` on your keyboard.
Hope this helps some one like it did to many of us.
You can also run VS Code from the terminal by typing code after adding it to the path:
Launch VS Code.
Open the Command Palette (⇧⌘P) and type shell command to find the Shell Command: Install code command in PATH command.
Mac shell commands
Restart the terminal for the new $PATH value to take effect. You'll be able to type code . in any folder to start editing files in that folder.
For VS Code Insiders Windows users (vs code doc):
Add the directory "C:\Program Files (x86)\Microsoft VS Code Insiders\bin"
at %PATH% environmental variable.
then go to the folder that you want to open with vs code and type:
code-insders .
Sometimes setting path from VS Code command palette does not work
Instead manually add your VS Code to your path:
Run in terminal
sudo nano /etc/paths
Go to the bottom of the file, and enter the path you wish to add
Hit control-x to quit. Enter “Y” to save the modified buffer.
Restart your terminal and to test echo $PATH. You should something similar
~ echo $PATH /Users/shashank/.nvm/versions/node/v8.9.2/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Visual Studio Code.app/Contents/Resources/app/bin
Next time, you navigate to your project folder from terminal
Enter:
code .
or
code /path/to/project
Source
In the case of Linux and Mac, you want to navigate to the directory that you extracted the VSCode files using the 'cd' command. For example:
cd ~/Downloads/VSCode
Then you start the application by running..
./Code
'Code' being the name of the executable.
If you have root access on the machine, you can configure the system to allow you to start VSCode from anywhere by linking it to /usr/bin, where links to executables are often stored.
sudo ln -s /path/to/VSCode/folder/Code /usr/bin/Code
You can now launch VSCode from anywhere by typing:
Code
For command line heads you can also run
sudo ln -s "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code" /usr/local/bin/code
this will do the exact same thing as the Shell Command: Install 'code' command in PATH command feature in VSCode.
When installing on Windows, you will be prompted to add VS Code to your PATH.
I was trying to figure out how to open files with VS Code from the command line and I already had the capability - I just forgot I had already added it. You might already have it installed - check by navigating to a folder you want to open and running the command code . to open that folder.
In linux if you use code . it will open VS Code in the folder the terminal was in.
Using code . Filename.cs it will open in folder and open said file.
For linux Debian the below can be done
$ export PATH=$PATH:/usr/share/code
Then run it
$ code
$ code file.py
$ code workingdir
Open command line and type:
cd your_folder_path
code.cmd .
or
code.cmd your_folder_path
It will open your folder in Visual Studio Code.
Make Sure, you are inside the correct folder after executing "cd your_folder_path" command.
On Windows you can add the following path to the system environment variables.
C:\Users\username\AppData\Local\Programs\Microsoft VS Code\bin
This works for Windows:
CMD> start vscode://file/o:/git/libzmq/builds/msvc/vs2017/libzmq.sln
But if the filepath has spaces, normally one would add double quotes around it, like this:
CMD> start "vscode://file/o:/git/lib zmq/builds/msvc/vs2017/libzmq.sln"
But this messes up with start, which can take a double-quoted title, so it will create a window with this name as the title and not open the project.
CMD> start "title" "vscode://file/o:/git/lib zmq/builds/msvc/vs2017/libzmq.sln"
typing "code" in dos command prompt worked for me
On Ubuntu the flatpak version seemed broken. I uninstalled it and downloaded the deb package right from Microsoft.
I use the following command to load a project quickly (in linux)
cd into the project cd /project
run command code pwd
similar steps can be used in other Os too.
In linux terminal you can just type:
$ code run
Windows:
Add code CLI path in a system environment variable.
in windows default code cli path is (username is you pc username)C:\Users\username\AppData\Local\Programs\Microsoft VS Code\bin
Then you can check it like this by taking your project folder and open new cmd and type code .
Step 1: create a .bat file with the name you want e.g vscode.bat
Step 2: Write your path to Visual Studio Code
Step 3: Save it in C:\Windows\System32 directory
**
C:
cd Users\Bino\AppData\Local\Programs\Microsoft VS Code
Code.exe**
Step 4: You can call visual studio code from any where by typing "vscode" which is the name of your bat file
This will work. This is your directory name "Directory_Name"
sudo code --user-data-dir="Directory_Name"
Other easyway to do it on mac is :go to Command Palette[ Shift ⇧+ Command (⌘)+P] and type :Shell Command: Install 'code' command in PATH
once installed: Shell command 'code' successfully installed in PATH.
Then you can use code from the terminal as well.
If you install VS CODE using snap. You will need to add /snap/bin in your PATH environment variable.
so - open your .bashrc or .zshrc
and add :/snap/bin in your PATH environment variable
reload terminal,
and than code comand will start it
A simple way is to go to your Project where you want to open it and type
code.cmd D:\PathTo\yourProject\MyProject
That's it. It will open your project in Visual Studio Code.
Delete old virtual environment and create a fresh virtual environment.
In a way I am reticent to add to the long list of answers. However, I searched this page for the word "portable" and came up empty. (And I did a full Stack Overflow search and also found nothing.) So I want to add this very specific answer for potential future searchers.
This answer is for if you installed VS Code in Portable Mode on Windows 10.
"Portable Mode" refers to what is described on the official VS Code web pages, which as of 21 January 2021 are found here: https://code.visualstudio.com. It does not mean the Visual Studio Code Portable project started/run by Gareth Flowers, or any similar project. (I am not saying anything bad about this or other projects - I have neither used nor evaluated.) If you are using one of those projects, you need to check with that project documentation/community - although this might work.
"Installing" VS Code in Portable Mode is downloading a .zip archive and extracting it locally wherever you want your VS Code "installation" to live. There is no actual installation or setup that is run, so there is no automatic adding of the code command to your PATH.
Answer
After extracting the Portable Mode VS Code files to the location of your choice, there should be a bin folder in that location. Copy the full path of that bin folder and add it to your System or User (your choice) PATH variable.
You should then be able to use the code command from PowerShell or CMD.
In linux you need to check first what is the name you your vscode binary file
When you get the binary file name check where it is by using this command :
whereis your_file_name
Go to the / (root) and go to bin and rename file with any name what you want to call it.
To rename : mv your_file_name your_changed_name
Now you can access vscode from any where in terminal
This works on Debian based Os definitely