close vscode window via script executed in integrated terminal - visual-studio-code

Is it possible to write a python or bash script such that when I run it from the integrated terminal in vscode it closes the current window?
This may sound useless, so let me explain one use case in case you're wondering why I need to do this, and maybe you also have other suggestions to achieve it. I have a python script that allows me to easily rename a github repo name using github's api: something like rename-github-repo newname, which automatically detects the git project directory, makes the changes on github via the api, and renames the directory in my local filesystem as well. After the directory rename, my current vscode window is messed up because it references the old directory, so I'd like to close it and open a new one with the new directory as part of the script rename-github-repo so I don't have to do it manually.

Related

Renaming files doesn't take effect in VScode while using WSL2

I have a very annoying problem in my VScode setup.
I'm using WSL as a terminal to work on my projects and occasionally, mistype the name of one of the folder or file that I'm working with.
For example:
I accidentally created the Mainheader.js file in layout folder (without the capitalized L) therefore, I decided to rename the folder with a capital L.
Now on my React app, any changes made to MainHeader.js file will not be reflected. I did update the related import.
So I decided to delete the entire folder and recreate it with MainHeader.js but this is what I end up with.
The file is there in my folder but when I click on it, I get
"Unable to open Mainheader.js - File not found"
So I try to create it then I get this error:
Unable to create file 'wsl\path]to\MainHeader.js' that already exists when overwrite flag is not set
Has anyone run into this type of behavior in WSL before? It's quite annoying because the only workaround I've found so far is to create a completely different folder with a different name...
Any help would be appreciated. I can't really work like this.
I've been having the same problem for the past 2 days. I presume its a permissions issue, but unfortunately I don't know how to permanently fix it.
I did find this work-around though:
Open a new VS Code window. (I'd recommend closing any VS Code window that had
your project directory open.)
Create a duplicate or copy the contents of the problem file so you don't lose
your code.
Delete the problem file.
Now create the file again using VS Code. Go to File > New Text File. Next paste
in your code.
Now save your new file. Go to File > Save As and save your new file with at the same path + filename + extension that was giving you problems previously. VS Code should allow you to save the new file without any issue.
Now you can open this new VS Code window to your project directory and you should be able to continuing accessing the file that was a problem before.
Basically we just deleted the problem file and then created it again from scratch in a new VS Code window.
Hope this work-around works for you!
EDIT 09/20/2022
Following Baza86's answer here solved the issue for me. Seems like it was a permissions issue of sorts, but if you use the Remote-WSL extension VS code can directly access the linux filesystem.
How to run VScode in sudo mode in WSL2?
You may need add the case option to you options in the wsl config. The default is set to off, however you can set this to off, dir or force.
Open your wsl.conf using sudo with any text editor while running window subsystem Linux. The config file resides in /etc/wsl.conf. This file is used to configure settings per-distribution for Linux distros running on WSL 1 or WSL 2.
My default config looked like this yours may be different:
[automount]
options = "metadata"
add
[automount]
options = "metadata,case=dir"
Here is the official Microsoft docs for Advanced settings configuration in WSL - https://learn.microsoft.com/en-us/windows/wsl/wsl-config

Opening WSL terminal in current VSCode directory without using VSCode server

I've used Git Bash for most bash-required tasks before, but since I've found WSL to be much more feature-rich as it's basically an entirely new subsystem. However, when I used to use VSCode along with Git Bash, it would simply cd into the working directory of the project for ease of use. However, it doesn't do that here. The only solutions I've found online are to create an entirely new Remote WSL VSCode window, but this is way too high maintenance for what I'm trying to do as I'm literally just trying to get VSCode to automatically cd into the correct directory. Thanks in advance.

How does VSCode's Remote Extension open files in my local editor through its internal terminal?

I use VSCode's remote development extension fairly regularly. I can use the terminal in VS Code as if it were on my own machine, and even the code command works correctly. That is, when I'm in a remote VSCode session, I can type code path/to/some/file and it will open another editor tab with that file. The terminal session and the file being opened are on the remote machine to which I've connected.
I have VSCode installed on the remote machine, and the code executable is in my PATH. So my question is, how is this functionality implemented behind the scenes? That is, how does VSCode know that when I type code path/to/some/file it should open that file into another editor tab on my machine instead of trying to fire up VSCode on the remote machine?
Literally seconds after I wrote the question I found the answer.
If I run which code in the terminal, it doesn't resolve to the usual VSCode executable, but instead it resolves to one located at $HOME/.vscode-server/bin/a5d1cc28bb5da32ec67e86cc50f84c67cc690321/bin/code.
If I echo $PATH I can see that $HOME/.config/bin and $HOME/.vscode-server/bin/a5d1cc28bb5da32ec67e86cc50f84c67cc690321/bin has been appended to the beginning of the PATH env var that my bash profile generates.
I assume this means that VSCode is executing bash with a different profile script that
Sources my usual bash profile
Creates the directories above and copies some helper programs into them
Modifies my path to include these directories
I also assume that the injected code executable is communicating with my local instance of VSCode in some way, instructing it to open the file in its editor.

How to block certain files from opening/previewing in vscode?

Im working on a large front end project in vscode. Whenever I have to discard/delete/move/... the output build.js files, it tries opening these files in the editor which takes ages to load and freezes vscode (more than 5k lines each).
The only work-around I currently have to delete these files, is to cd to their directory and run rm -rf ./*. But even then, I'm looking for a more convenient way of deleting these files without vscode trying to open them.
So my question: Is there a way, by editing the settings maybe, of telling vscode to ignore and never preview files that are of a certain type or filename like "*build.js" (I have multiple of these big files called a.build.js, b.build.js, ...
running git bash terminal on windows 10, vscode verson: 1.25.1
Edit
adding the following to User Settings
"files.exclude": {
...,
"**/*.build.js": true
},
Will not really help as it will not hide the files from my git changes. Right-clicking the files to discard, will still take ages to
complete.
Adding /public/dist* to .gitignore will also not work as
sometimes these files need to be pushed to the server, and sometimes
they just need to be discarded.
Because the vscode doesn't support this, for the "discard changes" scenario, you can use e.g. npm script, a gulp task, etc. and run it via vscode.
Example - npm script:
"scripts": {
"discard-build-files": "git checkout -- ./src/config/env/*.js"
}
Enable NPM Script Explorer:
In vscode preferences, set up this option
"npm.enableScriptExplorer": true
You should then get a new section inside your Explorer view.
Vscode currently doesn't support pattern filtering for previews.
The best I can think of:
Exclude your desired pattern altogether from the file explorer
Disable the single-click preview, and make it a more explicit action
via double-click
Ad 1.
Go to vscode's preferences and search for files.exclude.
Then edit the patterns to your liking.
Ad 2.
Go to vscode's preferences and search for workbench.list.openMode.
Then change the value from singleClick to doubleClick.

Whats an efficient workflow for my IPython / IPython Notebook projects?

Whats an efficient workflow for my IPython projects?
Requirements:
easily open notebooks from anywhere
easily shift between many notebooks in different locations
support the rest of my workflow (ie. version control, manipulating project files outside of IPython
Motivation:
If you’re like me, you often work in IPython notebooks, continually open and close many different notebooks as you wind through your work day. Its often suggested to launch the IPYNBs from the command line with something like ipython notebook --pylab=inline but navigating back and forth between deeply nested dirs gets old fast. What’s the best way to get around this?
Use a .bat file!
An example of how to construct one for easy launching of IPython notebooks is shown below. Save the file as go.bat and then from the command line you can execute go “ipython_notebook’s name” to easily launch it from anywhere. (you can name it anything, go is just convenient.)
Because your working dir can now be easily pointed to your project dir: The project workflow conveniently supports some helpful operations from the command line.
Easy git commands -- push, pull, and version the crap out of the project
Easy project inspection – use start . to open your projects directory an easily manipulate files outside of IPython
Easy starting of an IPython cluster by adding pcluster start -n 4 to the start ipython notebook line in the batch file
Know of way to improve the workflow or a better way to do this? Let me know!
batch file:
#echo off
GOTO %1
:titanic
cd C:\Users\Andrew\Documents\Kaggle\Titanic\Dups\Kaggel-Titanic
start ipython notebook --pylab=inline
GOTO END
: NB
cd C:\Users\Andrew\Documents\IPython NoteBooks
start ipython notebook --pylab=inline
GOTO END
:END