How can I open a file in visual studio code's text editor from the visual studio code's terminal? - visual-studio-code

Trying to start with VSC and want to know if there is a shortcut to open VSC's text editor from it's terminal ?

Assuming you have added vscode to your PATH variable during the installation process you can just type code path/to/your/file. This will open the file in the current vscode instance.
If you want it to open in a new window add the -n flag. For example code path/to/your/file -n

Related

Is it possible to quickly swap workspaces using the integrated terminal in Visual Studio Code?

I would like to make it so that when I'm traversing directories using the integrated terminal, running code . opens the current working directory in the integrated terminal within the current VSCode window, "discarding"/closing the currently opened workspace, instead of opening a new window for that folder. Is this possible? If not, what would be a similar solution?
You can use code -r .
code --help gives the following description:
-r --reuse-window Force to open a file or folder in an already opened window.
Another source is Visual Studio Code Tips and Tricks
As #ltomase found in their answer (and exanding on their answer), you can use the -r option of the code command. The doc comment for this option in the --help menu says this:
Force to open a file or folder in an already opened window.
There are other approaches not involving the command-line:
Open command palette and use the File: Open Workspace from File... command to open workspace files (.code-workspace), or
Open command palette and use the File: Open Folder... command to open folders (keyboard shortcut: ctrl+K , ctrl+o).
Use alt+f to open the File menu, then use keyboard navigation to go to "Open Recent" and then select a recent workspace to open.

Why does code not recognize paths in a system shortcut?

I want to use a shortcut to open up a specific folder in Visual Studio Code in a new window. I am on Fedora with Gnome Shell and have tried the integrated shortcut manager that can be found in the settings. When I define a shortcut with the command code -n ~/path/to/folder, Visual Studio opens a new file with the name folder. When I run the same command in the terminal it works as expected, i.e. a new window is opened in the specified folder.
Turns out ~ cannot be used in this context, so providing an absolute path fixed the issue.

Visual Studio Code edit file

When I try to edit a file in visual studio code, I type the command 'code filename.ps1' and the code used to automatically open as a second tab in the editor.
Now, when I do the same thing, it opens in a new window. I have not changed anything in my settings and I can't find an associated Preference that controls this.
Anyone know exactly the default setting that I need to look at?
According to CLI Options this should work:
code -r filename.ps1
>
-r or --reuse-window
Forces opening a file or folder in the last active window.

In Visual Studio Code, how do you inject clink into the integrated terminal?

I was using clink with ConEmu for various node related tasks on windows, but now I'm trying Visual Studio code.
How do I inject clink into Visual Studio Code's integrated terminal, so that I can get real command history persistence between sessions, incremental history search, etc.?
With ConEmu I could inject clink by dropping the clink folder into a specified pickup directory.
I've tried using the path to the included clink bat file, and the clink exe in the VS Code setting terminal.integrated.shell.windows but these spawn and then close the command shell immediately.
Thanks!
I discovered that you can pass arguments to the integrated shell in Visual Studio Code. Combined with the cmd.exe /K option which Carries out the command specified by string but remains, clink can be injected.
In VS Code, go to File > Preferences > Settings or use Ctrl , and add the settings:
"terminal.integrated.shell.windows": "C:\\WINDOWS\\sysnative\\cmd.exe",
"terminal.integrated.shellArgs.windows": ["/K", "C:\\path\\to\\clink\\clink_x64.exe inject"]
This is the equivalent of opening a command prompt in Windows, and running clink_x64 inject.
It is not answer for your question, but there is another trick to see cmder and text editor in one window. You can open your text editor as another tab in cmder, which I described here:
https://medium.com/#WMorkowski/protip-integrating-cmder-with-text-editor-7f08a6e76de7
from article:
Run your cmder.
Go to ‘Settings -> Startup -> Environment’
Type: set EDITOR_PATH=C:\Program Files (x86)\Microsoft VS Code\Code.exe alias editor="%EDITOR_PATH%" $1 -new_console:s50V Where
in the first line you should type path to your text editor (I was
testing it on Visual Studio Code and Sublime, but it should work with
other editors).
Save your settings
Type ‘editor’ command in command line.
Whoa! We almost finished. But in most cases you don’t want console tab
to be attached to the top of the window. You should close console tab,
and open it again, paying attention to check “New console split to
bottom” checkbox and choose the right console type. Now when you
finally set everything up, you should go to ‘Settings -> Startup’, and
check “Auto save/restore opened tabs” checkbox to save our new
workflow. Now every time you run cmder, your tabs setup will be
restored.
Expanding on my comment:
Open settings.json with:
File > Open > %APPDATA%\Code\User\settings.json
And assuming you installed clink with the magic of chocolatey:
choco install clink-maintained
Then your clink_x64.exe lives here:
C:\Program Files (x86)\clink\clink_x64.exe
And the lines you add to settings.json look like:
"terminal.integrated.shell.windows": "C:\\WINDOWS\\sysnative\\cmd.exe",
"terminal.integrated.shellArgs.windows": [
"/K",
"C:\\Program Files (x86)\\clink\\clink_x64.exe",
"inject",
"--profile",
"~\\clink"
],
Note the addition of --profile ... this allows the history to be persistent between vscode sessions.

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"