How to open file using vscode terminal? - visual-studio-code

I want to open a new tab and open a file using vscode terminal.
like open somefile.js command, so I can use ls and open file quickly.

Use code -r <file> to open the file in the last active code window

use
code -r <filename>
just remeber to install the code command to PATH. in VScode, open the command palette and type "code", you should see a Shell Command: Install code to PATH option.
I really haven't actually seen any difference between using the command without the -r flag.

If you are already in VSCode terminal (not an external OS terminal), check out the latest VSCode 1.64 (Jan. 2022) Terminal shell integration:
The terminal now features experimental opt-in shell integration which allows VS Code to gain insights on what is going on within the terminal as it was previously a black box.
When enabled using "terminal.integrated.enableShellIntegration": true, arguments to run a shell integration script will be injected into your terminal profile if possible.
The script itself mostly just injects invisible sequences into your prompt, providing us with information like where the prompt, command and command output is, what the current working directory (cwd) is for each command and the exit code of each command.
That means:
Link support relative to the cwd
Since we know the cwd for each line in the terminal buffer, we can support opening links in the terminal relative to the cwd at the location where it was activated.
Before, when a link was clicked, a quick pick would open with results from any folders containing a match for that name.
Now, the exact file match will be opened.
In a terminal with a cwd of VSCode, package.json is echoed.
Clicking on the file name will result in vscode/package.json opening.
The directory is changed to be the template-string-converter and then package.json is echoed.
Clicking on the file name will open template-string-converter/package.json.

Related

Is there a way to open a file in VS Code from the integrated terminal/codespace?

The integrated terminal in VS code means that you can run builds, scripts and other commands and see the results as is. Sometimes though they produce output files that you'd like to view in an editor, like a logfile or exception trace.
Is there a way of running a built-in command in the terminal to say 'please open this file in vscode', specifically in a codespace? On macOS, you might use 'open' or 'start' on Windows, but this only works if your codespace is hosted on the same machine that you're browsing to.
It would be great if there was a command that could be run that would open up the corresponding file in vscode, such that you could set the VIEW or EDITOR environment variables and have such operations edit in the file itself. Then (for example) running git in the integrated terminal would open a new vscode editor for writing the git message, instead of the default vi/emacs/notepad/edit.com/pico/nano.
Yes you can. Use the command:
code filename
Or, if you are running an early release (or GitHub codespaces):
code-insiders filename

Visual Studio Code - Where is the terminal history saved?

I noticed that the command history is saved when using Visual Studio Code. Using the arrow up and down keys, I can toggle through previously issued commands in the integrated terminal.
Where is the terminal history saved on the drive? Is it possible to open a file in notepad (or notepad++) and investigate the complete history, delete it?
I'm using Windows 10, and I have concluded that deleting the folders C:\Users\john\.vscode and C:\Users\john\AppData\Roaming\Code will not delete the terminal history for a particular project.
VSCode terminal use external shell. For linux the default shell is bash, I am not so sure about window, but I guess the default shell for window is powershell.
If your terminal shell is indeed powershell, this should help you locate the log file.
On Linux & MacOS, type the following command to display the path of the history file:
echo $HISTFILE
The history file may be different between VSCode and your regular terminal.
So you should type this command within the integrated terminal.
On Ubuntu you can see your history using this command:
vi ~/.bash_history

How to open folders in VSCode via Terminal in a fresh state?

It appears that VSCode always opens a folder in with the last UI state it had.
I'm looking for something like Sublime's remember_open_files: false, or in other words, I would like VSCode to open up with a clean UI state regardless of what state the UI was in the last time the folder was open.
What's happening now:
cd my-project-folder/
code .
# VSCode opens folder with saved UI state
What I want:
cd my-project-folder/
code .
# VSCode opens folder with fresh UI state
I tried to do it through command line using the command
code -n .
which should have opened VS code in current folder with a new session but it does not seem to work at all. I believe that code . seems to ignore the -n new session option and restores the previous session for the folder. So this feature is probably not implemented in VS code.
(Refer here for the commandline options for VS code.)
this is the command that works for me on windows
code -r .
From inside VS Code built-in terminal, cd into your project folder/directory and enter command:
code -a .
Note period at end.
This will open your current directory/project folder without opening a new window.
Part of the answer on this thread worked for me. Basically, make sure VSC is in the Applications folder. Then open the Command Palette (F1 or ⇧⌘P on Mac) and type shell command to find the Shell Command: Install 'code' command in PATH command.
Restart Visual Studio Code if it's open. In terminal, navigate to the folder you want to open in VSC, and type code .. Hopefully it should work for you.
If you are using a Mac, you need to first install the VSCode command amongst the shell command list.
Do this:
Open VSCode
press CMD + SHIFT + P
type shell command
select Install code command in path
navigate to any project from the terminal and type code .
code . opens VS Code at the current terminal folder
If you are using VS Code [Version: 1.50.0] then open your command prompt and go to your project's directory and just run the command:
code -a .
i.e. [Also can see the photo]
[1]: https://i.stack.imgur.com/sMmkH.png
$ code . --user-data-dir=.
This will open Visual Studio Code in current working directory. I use Bash with Ubuntu 16.04LTS.
I am using VS Code Version 1.24.1 (As of the time of original posting).
If you want to open a folder through the terminal, you can execute the command:
code -n name_of_your_folder/
or
code -n path_to your_folder/
Same thing goes for a file, and these open VS Code in a new window.
Also, note that you should have VS Code installed.
You're welcome!
I checked through all of the settings available in the VSCode preferences for me, and I don't think there is a way to do this.
There are some settings related to what VSCode window instance that folders will open into, but nothing that seems similar to Sublime Text's remember_open_files setting.
The closest thing I found was running code --user-data-dir . (feel free to replace . with some other directory so you don't pollute your current working directory) from the terminal to specify that VSCode shouldn't remember ANY previous settings, but this seems like overkill for what you're trying to accomplish (as VSCode will literally run as if it's the first time it's being run after a fresh install).
EDIT: I just discovered a View: Close All Editors command in the command palette (CMD + SHIFT + P). The keyboard shortcut for OSX is CMD + K, CMD + W, and this will close all the files you have opened!
I have the same problem on Mac.
I solved it in the following steps:
I opened the "Command Pallete" on the VSCode. This can be done by CMD + SHIFT + P
Type "shell"
Click on 'Install code command in path'
Give an administrator password
You will get a message that it has been installed
Now run "code ."
NB: Make sure you already cd into the folder you want to open before you run code .
If you want to open folder with vscode, you just go to folder ( you can user terminal or file explorer) with terminal, and do "code ."
To get the right folder in VS Code v 1.50.1 Terminal I tried a lot of options which didn't work for me. At the end I found very easy solution. I went to File->Open Workspace and found that my Workspace had the wrong folders inside it, which I simply deleted (from Workspace only!). Then I opened the folder I needed in my Workspace, opened Terminal->New Terminal, and everything worked perfectly well. Please let me know if this will work for someone else.
This command works to open a specific folder in VS code using terminal
code -r Documents/VS/C++/
here -r switch is used to open a file or a folder in an already opened VS code window
and then you specify the path of the file or folder you want to open
if you want to open it in a new window use -n switch instead of -r
This works for me with VSCode on Linux:
cd path/to/project
codium .
I tried every mentioned answers, what's work from me is this:-
I created a shell script to open folders.
$ sudo nano /usr/local/sbin/code2
/usr/bin/code-oss -n --user-data-dir '/home/myusername/.config/Code - OSS2/' -a $#
$ sudo chmod +x /usr/local/sbin/code2
You can remove --user-data-dir '/home/myusername/.config/Code - OSS2/' from the script if you want to use default code-oss config folder.
When I want to open a folder, I use the command like this :-
$ code2 .
I add this command code2 to open with option thunar to open folders directly from files manger.
Go to the directory in the command pallet on your computer the navigate to the the specific folder using cd
the type code . and that will open the folder and the files in it inside vs code. works like a charm.
If Visual Studio Code is installed using flatpak then a bash alias can help launch the application from the terminal.
alias code="flatpak run com.visualstudio.code"
I did it a simpler way just by three steps. I am currently in a project folder and want to open another folder in vs code using the cli or terminal. What I first did is navigated in the folder which I wanna open in vs code inside the terminal. Once I m inside that particular directory or folder I simply typed the command :
start code .
This will open that directory or folder in a new vs code window.
The complete process is :
open the terminal is vs code
navigate to the folder u want to open
once u r inside that particular folder type the command :
start code .

How to open a file from the integrated terminal in Visual Studio Code?

Is there a way of opening a file from the terminal in Visual Studio Code that opens in the same vscode instance that runs the terminal? Similar to c9 tool in Cloud9.
I'm aware of the code tool, but when you run code something.php from the integrated terminal it opens a new vscode instance, which is not what I want...
You can use -r or --reuse-window command line option.
code -r something.php
just
code file_name
I tried it on Win10 and on Linux (Ubuntu)
I don't know what operating system you're using, but on MacOS you can just say open filename.ext in the integrated terminal, and it will open a new tab in the same VSCode instance, ready for you to edit.
If you are having command not found: code in macOS, use a full path to it.
/Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code -r [filename]
Open Visual Studio Code
Press CMD + SHIFT + P (this opens "Command Palette")
Type shell command
Select “Install code command in path”
Navigate to any project from the terminal, and type code .
If it didn't work, select “Uninstall code command from path” first, then reinstall it again.
I use code -r . to open the current directory in the main window.
You can use the code command from the CLI to open a file, but if you want it to open in the existing window, either use code -r <file> as mentioned in other answers (which does work for me on Ubuntu Linux), or, if -r does not work (under WSL?), make sure window.openFilesInNewWindow is either off or default in settings.json or the in VS Code settings UI, then use code <file>.
Many things can be found in open --help
A work around that worked for me on MacOS is:
open -a 'Visual Studio Code.app' something.php
in the version 1.31.0 that I have installed, on Windows 7, the only way I found to do this is to e.g. change the file associations in system so that .cproj and .cs files are opened by Visual Studio Code by default, and type "filename.cs" in Terminal to open file by that name in the same window... -r option is not working for the first call (opens a new window), but with each subsequent call that same window is correctly reused. ok can't get to open whole directories this way - it's a bit shoddy anyway. probably it would be more convenient to use an outside shell and work with "-r" option
VSCode 1.64 (Jan. 2022) comes with a new command:
Keyboard Navigable Links
Previously, link navigation in the terminal required the use of a mouse.
Now, links can be opened using only the keyboard via the following commands:
Terminal: Open Detected Link... to view all links (web, file, word)
Terminal: Open Last Web Link... ex: https://github.com/microsoft/vscode
Terminal: Open Last File Link... ex: /Users/user/repo/file.txt
Check if the last command Terminal: Open Last File Link... would help in your case.
See also "Terminal shell integration"

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