Question

I'd like to create a program that causes a running gedit process to open a .txt file in a new tab using C. The file would be the argument I supply to the program. However, I'd like to avoid using system() altogether.

I'm on Linux. Is this possible? If so, by what means?

Was it helpful?

Solution

You don't need system(). You can use fork/execlp

if(!fork())
    execlp("gedit", "gedit", filename, NULL);

The version of gedit that's on my laptop uses a new tab by default if there's a running instance already, but I'm not sure about other versions.

OTHER TIPS

The standard way to do this is to not hardcode the editor's executable name into your application. Users might not have gedit installed at all (it's mostly only there if the system runs Gnome.) What you do instead is use xdg-open with the file you want to open as its argument. For example:

system("xdg-open myfile.txt");

If the user is instead using gedit as the default editor, this will open the file in a new tab if gedit is already running. If it's not running, it will be started first.

Here is a possible solution:

Step 1. Modify gedit source file (function: is_in_viewport() to return TRUE) (see this link).

Step 2. Use fork()/execl() in program and call this modified gedit (there are a few examples here).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top