Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
C++ (Unix): Open a text file with the default editor

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.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!