7

The question is above and my Google search wasn't succesfull. I guess I need to get the default editor and then use system("editor file.txt");? How could I get the default editor?

Edit: I don't know why but stackoverflow doesn't like my "Hey,"... then not.

6
  • 1
    Possible duplicate of Open file in default editor from bash. Commented Jan 10, 2014 at 13:31
  • @FrédéricHamidi Sorry but I can't aggree. That's not C++ and someone could give me an alternative to system(). Commented Jan 10, 2014 at 13:36
  • Fair enough. I was only trying to point you to xdg-open. Commented Jan 10, 2014 at 13:41
  • @FrédéricHamidi Ok thanks. Are there any disadvantages by using xdg-open instead of bin/sensible-editor(pts's answer)? Commented Jan 10, 2014 at 13:51
  • xdg-open is a freedesktop.org tool, sensible-editor is a Debian tool... I guess it depends on the platforms you want to support. Commented Jan 10, 2014 at 13:53

2 Answers 2

9

There is no official solution. Here is my recommendation for opening up a text editor:

If the filename extension is .txt, and xdg-open is avaliable on $PATH and the $DISPLAY variable is nonempty, then use xdg-open. Otherwise use /usr/bin/sensible-editor if it exists. Otherwise, use getenv("EDITOR"), getenv("VISUAL") or getenv("SELECTED_EDITOR"). If none of them are set, try nano, nano-tiny and then vi.

Sign up to request clarification or add additional context in comments.

1 Comment

Worth adding: xdg-open is not "official part of C++", but it is part of the "most widely?" followed Linux desktop standard: freedesktop.org/wiki
4

There is an example to get default editor environment, from visudo (It use default editor to open sudoers file ) source

/*
     * Check EDITOR and VISUAL environment variables to see which editor
     * the user wants to use (we may not end up using it though).
     * If the path is not fully-qualified, make it so and check that
     * the specified executable actually exists.
     */
    if ((UserEditor = getenv("EDITOR")) == NULL || *UserEditor == '\0')
    UserEditor = getenv("VISUAL");
    if (UserEditor && *UserEditor == '\0')
    UserEditor = NULL;
    else if (UserEditor) {
    if (find_path(UserEditor, &Editor, getenv("PATH")) == FOUND) {
        UserEditor = Editor;
    } else {
        if (def_flag(I_ENV_EDITOR)) {
        /* If we are honoring $EDITOR this is a fatal error. */
        (void) fprintf(stderr,
            "%s: specified editor (%s) doesn't exist!\n",
            Argv[0], UserEditor);
        Exit(-1);
        } else {
        /* Otherwise, just ignore $EDITOR. */
        UserEditor = NULL;
        }
    }
    }

You can check http://www.opensource.apple.com/source/sudo/sudo-9/sudo/visudo.c for the complete code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.