1

I noticed that when I open a file like ~/.bashrc in Vim as root, the syntax highlighting (SH) when editing a bash script as normal user (blue for comments, orange for keywords, red for numbers, etc.) is not applied. Can anyone tell me which configuration this depends on? I'm using Debian Stretch.

Edit: I found out there is an option syntax on in /etc/vim/vimrc you can un-comment to enable syntax highlighting by default. I wonder why my normal user had this feature turned on by default whereas the super user had not.

  • Not sure if this tip is applicable, but consider using sudo -e <file> instead of sudo vim <file>. It's safer (see man sudo). – Ben Oct 25 '18 at 19:36
1

If you are using sudo -H <command>, sudo -i or your version of sudo has Defaults always_set_home set (either in /etc/sudoers or as a compiled-in default), then the shells run as root will use /root/.bashrc for their settings instead of ~original_user/.bashrc.

Root's default environment is usually rather more conservative than regular user's defaults. This is because when a system has problems, you might need to log in as root on the console to fix it.

That console might be a serial port, or a Java- or HTML5-based terminal emulator on a webpage generated by a cloud platform (on a virtual machine) or a management processor (on a physical machine). Some of those are limited to a pretty basic set of terminal features, and attempts to use advanced features may cause garbage on the terminal display.

So it's better to have the colors disabled by default and let the sysadmin enable them if it's safe to do.

  • I use the su command, so /root/.bashrc is sourced when I change to root. As mentioned in my edit, I can turn it on by uncommenting syntax on in the system-wide config /etc/vim/vimrc or by typing :syntax on in vim. I now wonder why this is turned off by default for root, but turned on for user. – Stefan Hamcke Oct 25 '18 at 13:47
  • 1
    Root's default environment is very conservative because when a system has problems, you might need to log in as root on the console to fix it. And that console might be a serial port, or a Java- or HTML5-based terminal emulator on a webpage generated by a cloud platform (of a virtual machine) or a management processor (on a physical machine). Some of those are limited to a pretty basic set of terminal features, and attempts to use advanced features may cause garbage on the terminal display. So it's better to have the colors disabled by default and let the sysadmin enable them if it's safe. – telcoM Oct 25 '18 at 13:55
  • I see, thanks for the explanation. So is it then Vim which notices that it is run as root and therefore decides to turn off colors? – Stefan Hamcke Oct 25 '18 at 14:02
  • No, it's more like the root user has its own identity and its own set of defaults, and if you use a method of switching users that also updates the $HOME environment variable, the shell running as root gets its settings from /root/.bashrc rather than /home/<original_user>/.bashrc. The same applies to any program that stores its settings under $HOME. – telcoM Oct 25 '18 at 14:06
  • 1
    The relevant file here is ~/.vimrc rather than ~/.bashrc. It's the fact that /root/.vimrc either does not exist or does not have a lot of customizations that is relevant. it's a good idea not to customize the root account, of course. – JdeBP Oct 25 '18 at 14:14
0

While what telcoM writes in their answer makes sense, in my case it boiled down simply to the presence of a /root/.vimrc without an explicit syntax on in it. I once created that file for preferred settings, and had not had the need for such a file for the normal user. Therefore there was no /home/stefan/.vimrc, and this seems to enable syntax highlighting by default, even though the line syntax on is commented out in /etc/vim/vimrc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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