Skip to content

Instantly share code, notes, and snippets.

@imthenachoman
Last active August 20, 2023 20:48
Show Gist options
  • Save imthenachoman/36ecbf9147721a89825a05c07b72b828 to your computer and use it in GitHub Desktop.
Save imthenachoman/36ecbf9147721a89825a05c07b72b828 to your computer and use it in GitHub Desktop.
Bash script to open files with `kate` from the CLI and ask for `sudo` password if the current user does not have edit access to the file.

Overview

For whatever reason, openSUSE disables the polkit for Kate that will make it so Kate will prompt for a password when you try to edit root owned files.

The only way I could figure out how to get Kate to open these files is using the command VISUAL="kate -b" sudoedit <file>.

So I wrote a little helper script (to put be put in .bashrc) that opens kate for any file.

  • If the current user has write access to the file/folder, it'll open kate as the current user
  • If the current user does not have write access to the file/folder, it'll use sudoedit to open the file.

Sharing in case it helps anyone else.

How To Use

  1. Add the below script to ~/.bashrc
  2. When you want to edit a file with kate, run the command nedit <path to file>
  3. If the current user does not have edit access to the file, you'll be prompted for sudo password
# open a file in kate for editing
# if the current user has write access to the file, it'll open kate as the current user
# if the current user does not, it'll open the file in kate with sudoedit
function nedit()
{
# file passed down from input
fileToEdit="$1"
fileOrPathToCheck="$fileToEdit"
# if the file does not exist
if [[ ! -f "$fileOrPathToCheck" ]]
then
# we need to check the folder
fileOrPathToCheck=$(dirname "$fileOrPathToCheck")
fi
# if the file or folder can be written to by the current user
if [[ -r "$fileOrPathToCheck" && -w "$fileOrPathToCheck" ]]
then
# just open with kate
kate "$fileToEdit"
else
# else open with kate using sudoedit
export VISUAL="kate -b"
sudoedit "$fileToEdit"
unset VISUAL
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment