Skip to content

Instantly share code, notes, and snippets.

@boostup
Last active August 18, 2020 09:23
Show Gist options
  • Save boostup/84e3e4d68815a45fcb28555e7b53e975 to your computer and use it in GitHub Desktop.
Save boostup/84e3e4d68815a45fcb28555e7b53e975 to your computer and use it in GitHub Desktop.
This script is to open a particular directory directly in VSCode using a custom hot key in Ubuntu.
#!/usr/bin/env python3
import subprocess
import pyperclip
import os
import sys
subprocess.call(["xdotool", "key", "Control_L+c"])
prefix, absPath = pyperclip.paste().split('file://')
#Dealing with paths containing whitespaces which are replaced with "%20" in the clipboard string value of absPath variable
dirPath = absPath.strip().replace("%20", " ")
print(dirPath)
subprocess.run(["code", os.path.abspath(dirPath)])
#Uncomment the following line if you wish to keep the terminal window open to peruse the values printed by this script
#input("Press enter to exit ;)")
sys.exit()
@boostup
Copy link
Author

boostup commented Aug 18, 2020

Since this python script uses the content of the clipboard to open the right directory in VSCode, I must first click on the desired directory in Nautilus, then Ctrl-C, then I do my hotkey (as an example, Ctrl+Shift+i was my choice in this case).

Note
This is the command set for the hot key:
gnome-terminal -e 'bash -c "python3 /home/boostup/openVSCodeHotKeyUbuntu.py; read line"'

remove ; read line if you want the terminal window to close automatically once VsCode is open

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment