Enhancing Your Workflow: Integrating Visual Studio Code into Nautilus File Manager in Ubuntu 22.04

Ali Soufali
4 min readJul 27, 2023

--

Boost Your Productivity: A Step-by-Step Guide to Building a Nautilus Context Menu Extension for Opening Directories in Visual Studio Code

A beautiful picture of python code in visual studio code.

Are you a software developer or a coding enthusiast using Ubuntu 22.04? Do you love working with Visual Studio Code (VSCode) and wish for a seamless way to open directories from your Nautilus file manager directly into your favorite code editor? Well, today’s your lucky day! In this tutorial, we’ll guide you on how to create an extension script for the Nautilus file manager, allowing you to open directories directly in Visual Studio Code. We promise, it’s easier than you think!

Prerequisites

Before we dive in, you should have a basic understanding of Python, specifically how to create classes and import libraries. Additionally, you should have Python3 and Visual Studio Code installed on your Ubuntu system. Don’t worry if you don’t have the python3-nautilus package installed yet, we'll guide you through it. This package is vital to our script since it provides Python bindings for Nautilus components.

A picture showing a screenshot in which the “Open In VSCode” option in the right click context menu in nautilus file manager.

Step 1: Installing the python3-nautilus Package

On Ubuntu 22.04 LTS, the package we need is called python3-nautilus. However, the name of this package may vary depending on your Linux distribution or the version of Ubuntu you're using. If python3-nautilus doesn't work for you, try using python-nautilus instead.

Open a terminal window and type in the following command:

sudo apt-get install python3-nautilus

Step 2: Creating the Nautilus Extension Directory

Our script will live in the ~/.local/share/nautilus-python/extensions directory. However, this directory might not exist in your system by default. To create it, use the following command:

mkdir -p ~/.local/share/nautilus-python/extensions

Step 3: Creating the Extension Script

We’ll now create a Python script inside the directory we’ve just created. This script will define our new “Open in VSCode” context menu item.

Here is the script:

import os
import subprocess
from gi.repository import Nautilus, GObject

class OpenInVSCodeExtension(Nautilus.MenuProvider, GObject.GObject):
def __init__(self):
pass

def menu_activate_cb(self, menu, file):
subprocess.call(['code', file.get_location().get_path()])

def menu_background_activate_cb(self, menu, file):
subprocess.call(['code', file.get_location().get_path()])

def get_file_items(self, window, files):
if len(files) != 1:
return None

file = files[0]
if not file.is_directory() or file.get_uri_scheme() != 'file':
return None

item = Nautilus.MenuItem(name='NautilusPython::open_vscode_file_item',
label='Open in VSCode',
tip='Open this directory in VSCode')
item.connect('activate', self.menu_activate_cb, file)
return (item, )

def get_background_items(self, window, file):
item = Nautilus.MenuItem(name='NautilusPython::open_in_vscode',
label='Open in VSCode',
tip='Open this directory in VSCode')
item.connect('activate', self.menu_background_activate_cb, file)
return (item, )

Create a new Python file named open_in_vscode.py in the directory we've created before, and copy the script into this file.

Step 4: Understanding the Script

Now that we’ve created our script, let’s break it down.

  • Imports: We import the os and subprocess modules from Python's standard library, as well as Nautilus and GObject from gi.repository. Nautilus gives us access to the file manager's functionalities, and GObject is a base class needed for all extensions.
  • Defining the Extension Class: We create a class OpenInVSCodeExtension which inherits from Nautilus.MenuProvider and GObject.GObject. Nautilus.MenuProvider provides the APIs necessary to add menu items to Nautilus' context menu.
  • The __init__ Method: This method initializes our class. In our case, it doesn't do anything.
  • The menu_activate_cb and menu_background_activate_cb Methods: These methods define the actions to be taken when our menu item is clicked. They both open the selected directory in VSCode by calling subprocess.call(['code', file.get_location().get_path()]), which is equivalent to running the command code <directory path> in the terminal.
  • The get_file_items Method: This method defines when our menu item should appear. In this case, it only appears when a single directory is right-clicked.
  • The get_background_items Method: This method adds our menu item to the context menu that appears when you right-click on a directory's background.

Step 5: Restarting Nautilus

After creating our script, we need to restart Nautilus to see our changes. You can do this by typing the following command in the terminal:

nautilus -q && nautilus &

And voilà! You’ve now added a new “Open in VSCode” option in your Nautilus context menu.

Troubleshooting

If you face any issues, check that Python3 and the python3-nautilus package are properly installed. Also, ensure that the script file is located in the correct directory. The name of the python-nautilus package might be different based on your installed Linux distribution. If you still can't get it to work, try looking for the correct package name on the internet or in your package manager.

Conclusion

Building custom extensions for your tools can significantly enhance your productivity by tailoring your environment to your workflow. With the steps outlined in this tutorial, you’ve not only added a handy feature to your Nautilus file manager, but you’ve also gained a glimpse into the power of Python scripting in Linux. Remember, this is just a starting point — feel free to experiment and expand on this script to further customize your coding environment.

If you found this tutorial helpful or if you’ve improved upon this script in any way, feel free to share in the comments below. Let’s learn and grow together as a community. Happy coding!

--

--

Ali Soufali
Ali Soufali

Written by Ali Soufali

Data Scientist and Python Developer. Passionate about learning, teaching, and building innovative solutions.

No responses yet