I am using virtualenv, so to start I have to say

source some/long/path/bin/activate

Sometimes (very often, in fact) I want to create another tab in terminal, to edit another script, or to run django shell etc. And after another tab's creation I have to say source some/long/path/bin/activate again. In fact, it is quite boring.

Is there any way to force terminal run this magical words automatically? So if I type CtrlShiftT in terminal, and in current tab virtualenv is ran, terminal should open the very same virtualenv in the fresh new tab.

Or may be something completely different to solve this problem.

  • Posting a comment as this is a workaround not an answer, but you might try a bash alias to save typing. nano ~/.bash_aliases && source ~/.bash_aliases to create & activate one, then paste alias X='virtualenv $PWD/bin/activate' (change 'X' to be whatever you prefer, like 've'). Jun 25 '16 at 16:59

Targeting specific terminal emulators

Automatically running a command on creation of a new terminal tab would be a feature of the specific terminal you are using, and not related to python or virtualenv.

To have a broader range or answers the question would probably have to be How to autorun a shell command for terminal emulator xyz. For example, for gnome-terminal you can use custom profiles as described in https://unix.stackexchange.com/a/3856/15312, but this is an unportable solution if you decide to switch to another terminal or platform.

On the other hand, nobody wants to get locked to a specific terminal app, therefore the next option would be a more portable approach.

Targeting shell init scripts

If you control the shell that is running in the tab, then you can do it in your shell init script.

For example, for bash you can add the following lines in your ~/.bashrc script:

if [[ -f .venv/bin/activate ]] ; then
    source .venv/bin/activate

For zsh add the snippet to ~/.zshrc.

Whenever you create a new shell or open a new tab this snippet will check if you have a .venv folder in the current path and activate that venv automatically. If you create your virtualenvs in a folder which is not named .venv then adjust the script accordingly.

You might also want to override cd so that whenever you cd into a venv it will be automatically activated:

Below is an example for zsh:

function cd() {
  builtin cd $1

  if [[ -f .venv/bin/activate ]] ; then
    source .venv/bin/activate

Reduce typing with virtualenvwrapper

Specifically regarding virtualenv: you might want to try virtualenvwrapper which can help reduce the amount of typing, and make managing environments easier by providing autocompletion for virtualenv folders.

Backgrounding processes

And finally, as a quick workaround, instead of creating new tabs with the same env, you can suspend a currently running process (such as manage.py runserver) with Ctr+z, optionally run bg to resume it in background if you want to keep it running, then edit and save a file, and bring the original process back to foreground with fg. See this for more details.

  • Thank you, I solved that problem using wrapper; but about solution with terminal profiles: it is very interesting, but does gnome-terminal allow to change profile of current tab by some shortcut ?
    – polytheme
    Jul 18 '18 at 21:50
  • @polytheme: apparently not out-of-the box, according to askubuntu.com/questions/325235/…, but an answer in the thread suggests workarounds that rely on a utility which simulates key strokes.
    – ccpizza
    Jul 18 '18 at 21:57

I am using conda for virtual environments, and I felt the same need as your question. So I wrote a simple shell script for myself. Then I came across this question, and re-wrote my script for virtualenv usage.

  1. Save the following code as a script
  2. Name it whatever you want. Let's say tab.
  3. Give permission to execute it: sudo chmod +x tab.
  4. mv tab /usr/local/bin/ to make it a callable command from everywhere.
  5. The script depends on three other tools, xclip, xdotool, and wmctrl. If you don't have them, sudo apt install xclip xdotool, wmctrl
  6. Yay! I was too lazy to Control+Shift+T and run source /.../bin/activate but I now can do tab!

# Copy the clipboard content to restore later
original_clipboard=$(xclip -o)

# Copy the path of your virtual environment to clipboard
echo $VIRTUAL_ENV | xclip

# The following four lines open a new tab and switch to it.
# Copied from: https://stackoverflow.com/a/2191093/10953328
WID=$(xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)"| awk '{print $5}')
xdotool windowfocus $WID
xdotool key ctrl+shift+t
wmctrl -i -a $WID

# Activate your virtualenv
xdotool type --delay 0.5 --clearmodifiers "source $(xclip -o)/bin/activate"
xdotool key Return;

# Restore the original clipboard content
echo $original_clipboard | xclip

Tested with Ubuntu 18.0.4, GNOME 3.28.2, and it works fine. xdotools 3.20160805.1, xclip 0.12, wmctrl 1.07, but I don't really think that will matter much, will it?

Some part of the script is copied from this SO answer, as mentioned in the script itself.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.