Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to open a text file from the command line with the default application. During that time the default app is opened, the terminal must be disabled (cannot execute another command). For example, I use KDE and the default .txt reader is Kate, then I want to execute kate test.txt and the terminal is disabled until I terminate Kate.

What I want?

  • execute a command that starts the default text editor
  • meanwhile the c++ program is waiting at that specific point which executes the command described above
  • when the user is ready, saves the document, exit the default text editor(terminates) and the the execution of the c++ program continues
share|improve this question

closed as unclear what you're asking by slm, jasonwryan, Braiam, Anthon, vonbrand Feb 8 '14 at 22:36

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

2  
Just open a terminal and run kate test.txt this is the default behavior. –  terdon Feb 8 '14 at 17:14
    
I am developing project which will use this feature and I don't know which will be default text reader on another system. –  user1163511 Feb 8 '14 at 18:00
    
Could you explain for what purpose do you need this? Often there is a better solution for common problems. –  user55518 Feb 8 '14 at 18:28
    
@Braiam It's the opposite: user1163511 wants to wait for the program to terminate. –  Gilles Feb 8 '14 at 22:03
    
If you want to do this from C++ use a call to system() ( man 3 system. Your program will continue after your editor exits. If you want to detect just saving you need to do something more complicated and look at popen() and observe the saving of the file programmatically (i.e. the program cannot just wait). BTW. This belongs on Stack Overflow. –  Anthon Feb 13 '14 at 5:35

1 Answer 1

up vote 3 down vote accepted

The default text editor on a system is usually stored in the $EDITOR environmental variable. For example, on my system I have:

$ echo $EDITOR
/usr/bin/emacs

So, you can simply run

$ $EDITOR test.txt

NOTE: This is not necessarily the same editor defined in the graphcial environment's settings. Use the method below to get that.

Alternatively, if the system is configured to use it, you can launch the default program associated with a mime type with xdg-open (open on OSX):

$ xdg-open test.txt

But that will not hold the terminal until the command closes. However, you can use the mime setup to find out what program would be opened and then call the program yourself. To do this, get the program associated with the text mime type:

$ xdg-mime query default text/plain
pluma.desktop;sublime_text.desktop

So, now you can parse that line to get the program name:

editor=$(xdg-mime query default text/plain | sed 's/\..*//')
$editor test.txt

NOTE: This assumes that the .desktop file has the name of the actual executable. A safer way might be to locate the desktop file itself and grep the executable from it. Then, launch the program you found. You can do the whole thing with this command:

editor=$(grep -i ^exec $(locate -n 1 $(xdg-mime query default text/plain |
                        cut -d';' -f 1)) | 
           perl -pe 's/.*=(\S+).*/$1/') 
$editor test.txt
share|improve this answer
    
Thank you very much! –  user1163511 Feb 8 '14 at 18:58

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