This is a reminder to user sudoedit. Especially useful for Vim and Neovim users who have a rich and personal configuration.

sudoedit /etc/fstab

is also an option. sudoedit is a short form for sudo -e. It uses the default editor set int EDITOR or VISUAL variable. The difference to sudo vim FILE or sudo nano FILE is, that sudoedit FILE will use the editor configuration from the current user instead from the root. For me this makes a huge difference, because my plugins and settings for Neovim are not used when doing sudo vim.

Man page: https://linux.die.net/man/8/sudoedit

-e’ The -e (edit) option indicates that, instead of running a command, the user wishes to edit one or more files. In lieu of a command, the string “sudoedit” is used when consulting the security policy. If the user is authorized by the policy, the following steps are taken:

  1. Temporary copies are made of the files to be edited with the owner set to the invoking user.

  2. The editor specified by the policy is run to edit the temporary files. The sudoers policy uses the SUDO_EDITOR, VISUAL and EDITOR environment variables (in that order). If none of SUDO_EDITOR, VISUAL or EDITOR are set, the first program listed in the editor sudoers(5) option is used.

  3. If they have been modified, the temporary files are copied back to their original location and the temporary versions are removed.

If the specified file does not exist, it will be created. Note that unlike most commands run by sudo, the editor is run with the invoking user’s environment unmodified. If, for some reason, sudo is unable to update a file with its edited version, the user will receive a warning and the edited copy will remain in a temporary file.

  • thingsiplay@beehaw.orgOP
    link
    fedilink
    arrow-up
    2
    ·
    8 days ago

    I use LazyVim too BTW. You mean it would not use in sudo environment or in your current environment?

    • Yingwu@lemmy.dbzer0.com
      link
      fedilink
      English
      arrow-up
      1
      ·
      7 days ago

      It worked fine in my current environment, but not with sudoedit no. Can’t remember exactly why, might look at it again. It worked for you just by setting the default editor variable?

      • mbirth@lemmy.ml
        link
        fedilink
        arrow-up
        2
        ·
        7 days ago

        It’s probably loading the home environment of root similar to sudo -H vim … instead of just elevating privileges but keeping your home environment.

      • thingsiplay@beehaw.orgOP
        link
        fedilink
        arrow-up
        1
        ·
        7 days ago

        I didn’t do anything special to make that work. The variable export EDITOR='nvim' is set in my “.bash_profile” file in “Home” directly. The point of sudoedit is to use your personal environment, so it should pick it up. If there is any configuration needed to make this work, then I"m not aware of it.