How to temporarily change a setting in VSCode - vscode-extensions

When I am editing a standalone Python file in VSCode (not part of a workspace) I will often need to alter the value of python.pythonPath to reflect a specific virtualenv I am using to run that code.
As the setting is just for the one file, I don't want to change my persistent global settings, and I don't have workspace settings. Is there a way to change a setting just for this session? (Ideally, just for this one file, but I don't expect that to be possible, so I'd be happy with "just for the session"). If there isn't a built in way to do this, is there an extension which allows this? Or even an extension API that I could use to write my own extension for this?
As an alternative, is there a way to use an environment variable in a setting, and then set that environment variable for the current VSCode process? That would have the same effect, it would just require me to set up my user settings specifically to allow this usage.

If you launch vscode from the terminal after having activated your virtualenv, vscode will automatically use the aforementioned virtualenv (with no modification to your settings):
Exemple:
source venv/bin/activate
code .
Note: if vscode is already opened, use code -n . in order to open the file/folder in a new window.

Related

IntelliJ IDEA global environment variable configuration

I need to use an envirnoment variable in all of my idea run configurations. I currently use run->edit configurations->and then enter the env variables in selected configuration. However that's very tedious when I need to run isolated test scenarios because each one creates a new run configuration and I need to enter the variables all over again.
I tried to set the env variables in my linux system using export SOME_VAR="some value" in various session profile files: /etc/profile,/etc/bash.bashrc,~/.bashrc,~/.profile but intellij seems to ignore those vars during run, even though when I launch echo ${SOME_VAR} from intellij built-in terminal it displays the correct output.
I also tried using intellij .env file plugin and then set SOME_VAR=some value in .env file in project root. Didn't work either.
I found a solution to set environment variables on IntelliJ that has been working very well for me, and is incredibly simple. Let me show you.
This is the program (you can copy and paste it) we're using to test:
package com.javasd.intelijenv;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, String> env = System.getenv();
for (String envName : env.keySet()) {
System.out.format("%s=%s%n", envName, env.get(envName));
}
System.out.println("My home directory: " + System.getenv("MY_VAR"));
}
}
This program basically loads all environment variables, show them on the console, and try to show an env variable. Also, it assumes that you had created the MY_VAR env variable before calling IntelliJ IDEA, by doing something like:
$ export MY_VAR="This is my adorable var :)"
$ idea
Please, notice that we're calling IntelliJ IDEA in the same terminal (or session, or window) where we created the environment variable. If you create the variable and call the IDEA from the icon, the solution won't work because the IDEA will create its own session.
So, if run it without the correct configuration you will get something line this in your console:
Please, notice that you have just a few variables, and that MY_VAR is null.
Here's configuration I use to load the environment variables:
Click on the "Select Run/Debug Configurations" in your project and select "Edit Configurations":
Then, click on the the button with "..." on the right side of the "Environment Variables" section:
You'll see a pop-up. Check the checkbox on the bottom-left which has the label "Include parent environment variables":
That's it!!!
If you run your program now you will see something like this on your console:
You can see all the environment variables and, of course, your "MY_VAR" variable, with the right value!
Beyond the Basics
Usually, for security reasons, we don't want to keep all the environment variables visible. What we want to do is to make those variables visible only while the IntelliJ (or our program) is running.
So, no sensitive variables should be visible on the environment neither before you call Intellij nor after you close it.
Also, you want to keep those variables in a file (typically with a .env extension) to make it easy to manipulate and for security reasons.
To achieve this, there are some useful programs (you can Google them), but my favorite one is the env-cmd.
Let's say you have a test.env file with the following content:
MY_TEST_VAR=I live in the test.env file.
If you call IntelliJ by doing this:
$ env-cmd test.env idea
And edit your program to show "MY_TEST_VAR", and run it, you will see this on the IntelliJ's console:
But if you quit the IntelliJ, and look for your variable, you will see that the var doesn't exist (you can use env to confirm):
At this point, I hope you're able to play with your own solutions: create shell scripts with variables set inside, test other programs (direnv, autoenv, etc.), and so on.
Enjoy!
...
In my opinion the real issue is what Mat said.
If you want to launch IntelliJ from a shortcut, then you have to edit it a little bit:
Open the .desktop file, and add /bin/bash -c -i to the beginning of the launch command. The file should look something like this:
[Desktop Entry]
Exec=/bin/bash -i -c "/path/to/idea/bin/idea.sh" %f
Name=IntelliJ IDEA Ultimate
Type=Application
Version=1.0
If Maven is used project specific environment variables can be configured under File->Settings->Build, Execution, Deployment->Build Tools->Maven->Runner
These are reused then in any Maven configuration.
The same mechanism to set the environment variables might also work with different runners.
The problem is, that IntelliJ does not "see" the environment variables that are set in .bashrc (Also to be found in CrazyCoders answer). The easiest way to enable IntelliJ to import those variables is to start it from bash e.g. by typing intellij-idea-community.
I tried various things listed above, and adding the environment variables to the terminal configuration and the Maven build tools worked in some contexts but not others. Then I finally found the place in IntelliJ that actually works for runtime processes. Because why just have one environment variable configuration screen when you can have several and make all but one of them wrong? ^_^
If you edit the template from which your run configurations are created, and add the environment variables to the template, then they should be included in every subsequent run configuration that started with that template.
This is especially useful for the JUnit template, since it will mean that all your custom environment variables will be loaded for unit tests, regardless of the scope from which they're executed (single method, whole test class, whole module). But in general, if you edit the templates first, then any run configuration you create thereafter will inherit your environment variables from the template.
From the top menu: Run → Edit Configurations... → expand Templates tree → (choose a template) → Environment variables: → (enter a semicolon-delimited key-value pair list OR use the input widget)
For the auto-generated JUnit configurations, you should blow away any existing ones, and let IntelliJ recreate new ones as you go; each of these will use the updated JUnit template with your environment variables.
For macOs try adding /Applications/IntelliJ IDEA.app/Contents/bin/idea.properties
...
apple.awt.graphics.UseQuartz=true
apple.awt.fullscreencapturealldisplays=false
idea.jre.check=true
SOME_VAR=some value
As no other answer mentioned it here,
Add your environment variable to /etc/environment , then log out and log in again. IntelliJ will definitely pick it up.
I found another tricky solution :)
At least for Linux users...
Just create some shell script like idea.sh in any suitable location with this content.
#!/bin/bash
export YOUR_ENV_VARIABLE=some value
cd ~/path_to_your_idea_folder/bin
bash ./idea.sh
Make this script executable and run it.
This script will always run your IDE with predefined env variables.
Got to Open 'Edit Run/Debug configurations' dialogs
Go to Modify options
Select Environment variables
New box appears for Environment variables below Active profiles

PyCharm - Send directory to run configuration from right-click menu?

I have a run configuration in PyCharm with script parameters that take in a directory. Then, I have several directories (a changing number) in my project which I would like to easily be able to run this configuration on. Is there anyway to add an option to the right-click menu of directories to run the configuration passing that directory to the configuration? Or some other method which provides similar accessibility to running the configuration on a directory?
For my specific problem, I have many log directories for TensorBoard (from TensorFlow) and I would like to selectively and easily be able to start up an instance of TensorBoard running on a given directory.
Basically if you would run it in pycharm You can write your own plugin to add some functionality to the IDE. There is a great documentation on how to create plugins in idea/pycharm:
http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started.html
We have done it before and it was a successful plugin, it really sped up the development processs. :)

Use Intellij File Watcher only on Open File

I've been using the Intellij Golang plugin recently (it's great) but I've been running into an issue with the File Watchers I set up. My working directory is set to my $GOPATH directory and I would like to be able to run golint and goimports after I save a file I'm working on. The issue I have is if I use git to clone a repo the next time I open Intellij it runs both golint and goimports on all the newly imported files which can take a long time for large projects. Is there a way to set up File Watcher so it only watches a file that is currently open? I added a screenshot of my current settings below:
Open Watcher setting -> Scope , change it to Open Files
If you wanna more control over the plugin, you can have a look at the official doc
Instead of file watcher you can get External tools configured to achieve almost the same functionality. The only difference is that this won't run on save but rather you'll have to trigger this manually. Or you could configure a macro to run them on save (but I don't have experience with macros + external tools so I'm not if this will work).

Start two instances of IntelliJ IDE

Well my question is pretty simple, how do I start two instances of IntelliJ (community edition).
When I have one instance started and I try to start another one, all that happens is that my started instance gets focus.
I'm developing Android applications using IntelliJ.
Any thoughts?
Press Ctrl+Alt+SChoose Appearance & Behavior, then System Settings, check radio button: Open project in new window.
You need to configure each instance to use its own folders for config/plugins/system locations by editing idea.properties file on Windows/Linux and Info.plist on Mac. You can find the details in FAQ.
Note that normally it's not necessary since you can open multiple projects in different IDEA frames within the same instance using File | Open or Open Recent.
CrazyCoder has roughly the right idea. However, setting the config file alone was not sufficient for me to run multiple instances. Here are my steps to get this going (in GNU/Linux, I am sure you can figure out equivalent in other systems):
Create a folder/directory per instance you want to run.
mkdir -p ~/idea/instance-0
Go to the installation directory (e.g. /opt/intellij) and copy the idea.properties (in bin) file over to your instance directory.
cp /opt/intellij/bin/idea.properties ~/idea/instance-0/
Copy 3 more directories: system, plugins, and config. I highly recommend doing this without the running instance
cp -r /opt/intellij/system ~/idea/instance-0/
cp -r /opt/intellij/plugins ~/idea/instance-0/
cp -r /opt/intellij/config ~/idea/instance-0/
mkdir ~/idea/instance-0/log
Open your idea.properties file and update the configurations for your directories:
#---------------------------------------------------------------------
# Uncomment this option if you want to customize path to IDE config folder. Make sure you're using forward slashes.
#---------------------------------------------------------------------
idea.config.path=${user.home}/config
#---------------------------------------------------------------------
# Uncomment this option if you want to customize path to IDE system folder. Make sure you're using forward slashes.
#---------------------------------------------------------------------
idea.system.path=${user.home}/system
#---------------------------------------------------------------------
# Uncomment this option if you want to customize path to user installed plugins folder. Make sure you're using forward slashes.
#---------------------------------------------------------------------
idea.plugins.path=${user.home}/plugins
#---------------------------------------------------------------------
# Uncomment this option if you want to customize path to IDE logs folder. Make sure you're using forward slashes.
#---------------------------------------------------------------------
idea.log.path=${user.home}/log
Now, you can start IntelliJ with the new setup:
IDEA_PROPERTIES=~/idea/instance-0/idea.properties /opt/intellij/bin/idea
Obviously, you probably want to put the command in a script file for invocation. This seems to work for me.
File->Settings->General and in section "Startup/Shutdown" check "Confirm window to open project in"
With Ultimate 2020.2, go to Appearance & Behavior > System Settings in the settings dialog and select the "Ask" option for "Open project in"
As per the directions from jetbrains you'll need go to the 'General' page of the 'Settings' dialog and chose 'Open project in a new window'. Then proceed to open a project as you normally do. IntelliJ should then startup a completely new instance.
There is an other very quick way of doing it. There is always an EAP version of the IDE and it can run at same time with the current one. For example I am using AppCode 2017.2 and 2017.3 EAP in parallel.
Go go to IntelliJ | Tools | Create Command-line Launcher...
Keep the defaults (which creates a binary named "idea"):
Now, go to your command line.
Cd to your project directory and type: idea .
This will create a .idea directory for IntelliJ configurations for that project, which it will re-use each time to start IntelliJ from that directory.
You can now go to a different project directory and type: idea .
Assuming you left the previous IntellJ IDE open, you will now have two IntellJ IDEs open, one for each project.
Notes:
1) If your project uses environment variables, then I'd recommending opening a separate terminal tab/window for each project and set that project's environment variables before running: idea .
2) Depending on what you're trying to accomplish, you may need to modify your classpath (or settings like Project GOPATH) for each IntelliJ instance.
My answer is not directly related to the question but its a solution for some cases where we think we need 2 Intellij instances.
For my issue I was thinking to launch 2 Intellij instances. But after careful thinking and searching for other options, I found an easy and quick solution and I wanna share with the community
If you are looking to compare files between different branches, and you wanna compare the difference, that can be done with git comparison. You don't need 2 different Intellij instances.
My Case:
In my case, I wanted to copy very specific code from 1 branch to another and I wanted to compare the difference between the code. The restriction was, I can't do git merge or cherry-pick because we didn't want full commit to be part of new branch. Just few necessary lines were required in the new branch.
My Solution:
Select the branch
Open the file where you wanna insert code
Right Click -> Git -> Compare with... (refer to pic)
Select the branch and you will get the difference
Append or Copy the difference
If you have new files or directories, you can create it manually and copy-paste the content
I know this answer doesn't directly relates to what has been asked, but sometimes we miss alternative solutions.
Hope this can be helpful as an alternative solution.
In addition to the above comments from #crazycoder and #magice, Make sure that you are not trying to load Pycharm with the same project two times which happened to me!!!.
For example, in windows10 already loaded with ONLY one project in PyCharm and tried to load another Pycharm instance by clicking on the PyCharm desktop shortcut or from task-bar if added. In this case, Pycharm will not load the second instance.
I have wasted some time here. So, wanted to share with the community as it will help someone out there!!
Cheers,

How to unload all the plugins from vim and change VIMRUNTIME?

I have an account at my hosting providers server and I can't install my own copy of vim.
So the only personalization I can make is editing .vimrc in my account, but it won't suffice
What I'd Like to do is:
on startup I'd like to unload all the plugins and loaded stuff, and tell vim to use other folder as its' runtime.
Any idea how to aproach it?
You can start your vim with -u NONE. From the man-page:
-u {vimrc} Use the commands in the file {vimrc} for initializations.
All the other initializations are skipped. Use this to
edit a special kind of files. It can also be used to skip
all initializations by giving the name "NONE". See ":help
initialization" within vim for more details.
For changing $VIMRUNTIME at runtime, use
:let $VIMRUNTIME = "/new/path/"
To answer your question: you just need to set the runtimepath option in your .vimrc file, because your .vimrc is read before any plugins are loaded.
However, the default runtimepath value usually includes the ~/.vim folder (use :set runtimepath? to check), so you should be able to add whatever plugins you need to the ~/.vim folder. Also you can prevent many plugins from loading by adding a line such as :let g:loaded_<plugin name> = 1 to your .vimrc file. Check the help docs for each plugin to find out how to do this.