Q:

Different behaviour for `vi` and `sudo vi`

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:

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. 
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.
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...

A:

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.

A:

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.)

