2

On my Raspberry 3 running Raspbian GNU/Linux 9 (stretch) I get different behaviour for vim depending if I use vim file or sudo vim file.

Especially two things:

  1. With sudo vim I can use the Page Up and Page Down keys to go forward and backwards a page. Without sudo I can't.

  2. After I close a file, without sudo the terminal shows the last part of the file I was just in, and without sudo it doesn't.

  3. I set hlsearch in my .vimrc, but when I use sudo the searched terms don't get highlighted, without sudo they do.

I want the sudo behaviour (except for point 3) without having to use sudo all the time (because of obvious reasons), but I don't even know where to start. Is it a problem with my terminal, is it a problem with my vim?

My .vimrc:

set term=ansi
syntax on
set hlsearch

I hope somebody has an idea what is happening here...

3 Answers 3

4

Note that as a workaround you can explicitly specify a vimrc file when you launch with vim -u <vimrc>. But I'm sure you want to find a more permanent "fix".

I can't tell you exactly what is going on with your system except that it's likely related to environment differences between sudo and non-sudo runs. Here's some info to help you investigate further...

Start by looking at :help vimrc. This describes how vim finds vimrc files. It's system dependent (e.g. Unix looks in $HOME/.vimrc first, Windows $HOME/_vimrc). You'll want to check, for example, the value of $HOME when running with/without sudo.

To get some ideas about what is being loaded you can run :scriptnames from vim. This shows all scripts that have been loaded, in order. Look near the top of the output as that's usually where loading of your vimrc file appears.

You can also do vim -Vn which will print out useful information depending on value of n:

>= 1    When the viminfo file is read or written.
>= 2    When a file is ":source"'ed.
>= 5    Every searched tags file and include file.
>= 8    Files for which a group of autocommands is executed.
>= 9    Every executed autocommand.
>= 12   Every executed function.
>= 13   When an exception is thrown, caught, finished, or discarded.
>= 14   Anything pending in a ":finally" clause.
>= 15   Every executed Ex command (truncated at 200 characters).

Update: minnmass' helpful answer led me to a fact I wasn't aware of. I knew that sudo -e/sudoedit helps protect against accidental clobbering of critical files by using temp files but I didn't know this: unlike most commands run by sudo, the editor is run with the invoking user's environment unmodified. So you may be able to keep $HOME/.vimrc and do sudo -e <file>. Give it a try if you are so inclined. (Depending on exiting values, you may first need to do export ENVVAR=vim where ENVVAR is any one of SUDO_EDITOR, VISUAL or EDITOR.)

1
  • Thank you very much. When using :scriptnames I can see that sudo vi doesnt load the ~/.vimrc, only the /usr/share/vim/vimrc and vi loads /usr/shar/vim/vimrc but after that it loads ~/.vimrc. Deleting the ~/.vimrc and putting set hlsearch into the /usr/share/vim/vimrc fixed everything. Thank you.
    – Fl.pf.
    Commented Oct 14, 2017 at 13:17
8

You're running vim as root, not as your normal user (that's what sudo does). Since you're not running vim as your normal user, your user's .vimrc isn't being consulted (this is the correct, expected behavior).

If you need to edit a file as root, but want to use your regular editor and configuration, look at sudo -e or sudoedit (which is basically an alias to sudo -e). The man page (here, or just man sudo) may be useful in configuring your system to use vim for that purpose.

1
  • +1 for using sudo -e, which also prevents the otherwise trivial method of getting a root shell from a root file editor using :shell.
    – Wildcard
    Commented Oct 17, 2017 at 12:28
0

Per what minnmass said above, running vi as root (using sudo) will bypass your ~/.vimrc.

You can however specify your own vimrc while running vi with sudo.

Add the following lines to your ~/.bashrc.

alias sudo='sudo '
alias vis='vi -u ~/.vimrc'

After sourcing the bashrc, you should be able to run sudo vis <filename>, and keep your regular vi editor behavior in superuser.

1
  • Welcome to Vi and Vim! Just note that you could use sudo -e without all the alias fuss, which still doesn't correctly process any files I have in ~/.vim.
    – D. Ben Knoble
    Commented Oct 21, 2022 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.