Linux command

What is the sudoedit command on Linux?

Understand the sudoedit command and its use case.

--

The sudo command is the most helpful command on Linux. Every Linux developer uses it daily. The sudo command allows users to run or execute programs and commands as the superuser or another user.

Sudoedit is an unpopular command among Linux users, but it is the most helpful command; the sudoedit command edits the file as a superuser or another user.

The core difference between sudo and sudoedit commands is that sudoedit is used to edit files, and sudo commands are used to run and execute files.

The sudoedit command is an extension of the sudo -ecommand or an alias. The option is the same as sudoedit and sudo -e command.

To understand the workings behind the sudoedit command, check out this article published by Himanshu Arora.

Editing any file using the sudoedit command is very easy. Check out the following example.

# Syntax

sudoedit your-file-name

# Example

sudoedit /etc/nixos/home.sh
# or
sudoedit /etc/nixos.backup/home.nix

After running the following command, your file will open in your default editor or IDE. In my case, Nano is the default IDE or editor. Most Linux distributions come with the Nano editor, which is simple and user-friendly.

The sudoedit file is open in the default IDE or editor.
The sudoedit file is open in the default IDE or editor.

How do you change the default editor in the sudoedit command?

You can easily change your default IDE or editor with the help of environment variables.

To change the default editor in the sudoedit command. You can set or change the SUDO_EDITOR environment variable. If you use bash, edit the .bashrc file. If you use zsh, edit the .zshrc file.

# .bashrc or .zshrc

export SUDO_EDITOR=nvim # chnage default editor to neovim

export SUDO_EDITOR=/share/bin/nvim # If the command does not work, add the command full path (editor).

Lastly, to take effect, source the .bashrc or .zshrc file.

In nixos, you can change the default editor just by pasting the following code into your configuration.nix file.

# configuration.nix

# Change the deafult Editor in sudoedit command.

environment.variables= {
SUDO_EDITOR = "nvim";
};

Last, rebuild your nixos with the nixos-rebuild command.

When you open the file using the sudoedit command, your default editor changes.

Change the default IDE or Editor nano to neovim in sudoedit command.
Change the default IDE or Editor nano to neovim in the sudoedit command.

Run the following command in the terminal to verify whether your default is changed.

echo $SUDO_EDITOR
nvim

Conclusion

Sudoedit is a handy command for editing the root file. It is ready. Remember and recognize.

To learn more about Nixos and Linux stuff, follow the Linux publication on Medium and other updates. Follow me on Twitter (X) and Medium.

--

--

Rajdeep Singh
The Linux

JavaScript | TypeScript | Reactjs | Nextjs | Rust | Biotechnology | Bioinformatic | Frontend Developer | Author | https://linktr.ee/officialrajdeepsingh