sudo vim and vim open different editors

Mohamed Yasser

I have just finished installing Fedora 36 and was in the process of installing my usual software. I personally prefer nvim over vim, but I got used to typing vim; so, I just use an alias alias vim='nvim' in .bashrc.

Using vim directly uses nvim and uses the init.vim; however, using sudo vim doesn't seem to use any of the mappings I wrote. (I linked init.vim with .vimrc with ln -s .config/nvim/init.vim .vimrc). I read that sudo uses another file other than .bashrc, but I don't want to create aliases everywhere.

Found some answers recommending using sudo update-alternatives --config vim and choosing nvim from a "list", but I don't get any output when running the command. It just gives me a new terminal line. How do I make sudo update-alternatives --config vim return the "list"? Or is there a better way to do it other than update-alternatives?

bananabook

As far as I can see, sudo changes your user id, so all configurations you made specifically for your normal user do not apply when you call something with sudo (meaning if you have a .vimrc it will not apply when calling vim with sudo). In addition to that, as far as I know sudo cannot read 'aliases' you created for bash anyway, meaning if you type in alias l=ls;sudo l sudo would not know what the command l means.

update-alternatives is a useful tool, that creates and handles links to binaries, so if you have for instance different version of vim on your system, you can use update-alternatives to change what version is called when you call vim. An alternative can be configured with the command you tried with the --config flag, but only after it has been created with the --install flag.

Here is a tutorial going through how to do that for python: link.

However I am not sure how this is going to help you with your problem, but it could be, that by creating an alternative you allow sudo to find the program you want.

I am not sure if this is advisable, probably not, but a simple and dirty solution to your problem would be to just rename the binary or create a link to the binary of nvim with the name vim. This way every environment will use this binary if it calls it. But learning and using update-alternatives is the cleaner and more resistant way I think.

Personally I go the lazy route and type out the correct binary names when using sudo. I have an alias to v=vim but when using sudo I am probably doing something serious, so I may as well be precise in what I want, but I can understand if you want to use your aliases everywhere.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related