Why does vscode open code.js file instead of current folder? - visual-studio-code

Subsequent to When typing code in command line to launch vscode how to avoid this wsh error?
I can now launch vscode by typing
code .
But if folder contains code.js file it will open that file instead of current folder.
Why?
I can't see any explanation in doc https://code.visualstudio.com/docs/editor/command-line

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.

How to create a file in VS Code with Vim extension?

Using the VIM extension for VSCode, is there a way to create a file inside the folder you are currently selecting on the explorer tab?
Similar to how it works with NERDTree on NVIM itself where you can press "a" and it lets you write a filename.
Brief example where the cursor would be placed on the "pages" folder.
You can use :e <filename> to create a file and simply use :w to save it.
It creates a file in the directory in which your current file is open. So in the screenshot you provided, if you would be editing _app.tsx, the new file will be created in this directory unless differently stated in your filename.

How to NOT open Visual Studio Code from the command line

I see a lot of posts asking how to open VScode from the command line; I have the opposite problem!
When a type a file name from the CMD terminal, VSCode is launched with the file contents...why is that? I don't want that to happen! Any ideas on how to stop this from happening?
Maybe you should check your environment variables and find path for VScode, then delete it
Also maybe in your file properties VScode is set to default opening app , so it open files by default
For example (In my case Intellij IDEA) it looks something like this:
this mean you should go to properties and change "opens with" options
Thanks for the hints; they both helped on solving the problem.
The one particular file that I wanted to execute and NOT open with vscode is a bash script file with extension " .sh ".
I checked the file manager and it is not associated with vscode; instead, it says "SH Source File".
Then, I followed the link to thisdavej and after seeing the proposed registry entries, I went ahead and looked into my own registry...found that the .sh extension had an "OpenWithProgids/VSCode.sh", deleting that solved my problem. Now, when I type the name of the shell script, it actually executes.
Thanks.

Visual studio code, how to copy the file name

In the visual studio code, it has commands to Copy Path and Copy Relative Path (Ctrl+Shift+p->File: Copy Path of Active File). Is there a way I could copy the active file name instead of the whole path?
If there is no way copy the active file name. Is there a way I could write a script or something which creating a new command based on existing Copy Path and showing the new command in the Command Palette?
The extension Copy file name works like a charm!
CTRL+ALT+F gives you filename and CTRL+ALT+E gives the filename.extension.
open your file on vscode> select file as active file> Ctrl+Shift+P > Alt+Shift+C
now you can use Ctrl+V to paste this path any where you want.
step1. first install "copy file name" extension from extensions(ctrl+shift+X) in your vscode.
step2. then use ctrl+alt+e for copying the full file_name with file_extension(like .cpp or .py or .java etc..).
or ctrl+alt+f for for copying the file_name
without file_extension.
It is been a while but I found if you want to write a script using the currently opened file, create a new task in task.json and calling some scripts in the task might be a good option.
Basically, ${file}, ${fileBasename} and ${fileDirname} are the variables that can be used in your script file.
Useful reference:
https://code.visualstudio.com/docs/editor/variables-reference
As an alternative, if you see the active file in the "Explorer" view, you can type F2 once you select it.
With VSCode 1.71 (Aug. 2022), you can type F2 and:
1st press => prefix
2nd press => all
3rd press => suffix
4th press => cycles back around to prefix
This has been release to VSCode insider today, for you to test/play with it.
Without using any extensions, here's a combination of key shortcuts you can use to copy the path of the current file. It's a bit cumbersome but with some practice can be faster than doing the same using the mouse.
Ctrl+Shift+E to open and focus the Explorer pane.
The current file will be selected automatically by default.
F2 to start renaming the file.
This will select the file name without file extension.
(optional) Ctrl+A to include the file extension in the selection.
Ctrl+C to copy.
Escape to abort renaming (or just click anywhere).
no need any extensions, Ctrl+K --> Ctrl+Shift+C, then you will get relative path

How to open file using vscode terminal?

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.