0

On my Raspberry Pi, I have a global configuration file at /etc/vim/vimrc. I also have a file at /etc/vimrc. I've tried modifying this second file, probably because I read somewhere that this is where to modify a global vimrc file for the system, but it hasn't made the expected changes. (Specifically, set hlsearch.) I'd prefer not to modify that giant global file located at the first filepath, but rather to modify the 'next' configuration file that takes effect globally. How would I do this?

Bonus points: is the file at /etc/vimrc in completely the wrong spot? If so, where should I put it?

Extra info: I want to modify the global configuration file so that I have identical behaviors if I'm modifying files as root (via sudo or su).


Edit to add:

:version
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled May  7 2017 07:53:41)
Included patches: 1-488, 576
Extra patches: 8.0.0378, 8.0.0377, 8.0.0322, 8.0.0056
Modified by pkg-vim-maintainers@lists.alioth.debian.org
Compiled by buildd@
Huge version without GUI.  Features included (+) or not (-):
+acl             +comments        +ex_extra        +jumplist        -mouseshape      +path_extra      +signs           +textobjects     +writebackup
+arabic          +conceal         +extra_search    +keymap          +mouse_dec       -perl            +smartindent     +title           -X11
+autocmd         +cryptv          +farsi           +langmap         +mouse_gpm       +persistent_undo -sniff           -toolbar         -xfontset
-balloon_eval    +cscope          +file_in_path    +libcall         -mouse_jsbterm   +postscript      +startuptime     +user_commands   -xim
-browse          +cursorbind      +find_in_path    +linebreak       +mouse_netterm   +printer         +statusline      +vertsplit       -xsmp
++builtin_terms  +cursorshape     +float           +lispindent      +mouse_sgr       +profile         -sun_workshop    +virtualedit     -xterm_clipboard
+byte_offset     +dialog_con      +folding         +listcmds        -mouse_sysmouse  -python          +syntax          +visual          -xterm_save
+cindent         +diff            -footer          +localmap        +mouse_urxvt     -python3         +tag_binary      +visualextra     -xpm
-clientserver    +digraphs        +fork()          -lua             +mouse_xterm     +quickfix        +tag_old_static  +viminfo
-clipboard       -dnd             +gettext         +menu            +multi_byte      +reltime         -tag_any_white   +vreplace
+cmdline_compl   -ebcdic          -hangul_input    +mksession       +multi_lang      +rightleft       -tcl             +wildignore
+cmdline_hist    +emacs_tags      +iconv           +modify_fname    -mzscheme        -ruby            +terminfo        +wildmenu
+cmdline_info    +eval            +insert_expand   +mouse           +netbeans_intg   +scrollbind      +termresponse    +windows
   system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
  fall-back for $VIM: "/usr/share/vim"
Compilation:
gcc -c -I. -Iproto -DHAVE_CONFIG_H     -g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1
Linking: gcc   -fPIE -pie -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -o vim        -lm -ltinfo -lnsl  -lselinux -lacl -lattr -lgpm -ldl
Press ENTER or type command to continue

... and

:help system-vimrc
                                                    *system-vimrc*
 b. For Unix, MS-DOS, MS-Windows, OS/2, VMS, Macintosh, RISC-OS and Amiga
    the system vimrc file is read for initializations.  The path of this
    file is shown with the ":version" command.  Mostly it's "$VIM/vimrc".
    Note that this file is ALWAYS read in 'compatible' mode, since the
    automatic resetting of 'compatible' is only done later.  Add a ":set
    nocp" command if you like.
    For the Macintosh the $VIMRUNTIME/macmap.vim is read.

When I run cd $VIM pwd moves to /usr/share/vim... but on a whim I checked that directory...

user@raspberrypi:~ $ ls -la /usr/share/vim/
total 20
drwxr-xr-x   5 root root 4096 Feb 20 22:33 .
drwxr-xr-x 193 root root 4096 Jul 19 17:51 ..
drwxr-xr-x   4 root root 4096 Apr  8  2017 addons
drwxr-xr-x   2 root root 4096 Feb 20 22:33 registry
drwxr-xr-x  16 root root 4096 Feb 20 22:33 vim74
lrwxrwxrwx   1 root root    8 Apr 12  2015 vimfiles -> /etc/vim
lrwxrwxrwx   1 root root   14 May  7  2017 vimrc -> /etc/vim/vimrc
lrwxrwxrwx   1 root root   19 May  7  2017 vimrc.tiny -> /etc/vim/vimrc.tiny
user@raspberrypi:~ $ 

... and tada: $VIM/vimrc is symlinked to /etc/vim/vimrc. So now I know which file is being sourced as the $VIMRC file. Unfortunately, the next help section below system-vimrc tells me...

  *VIMINIT* *.vimrc* *_vimrc* *EXINIT* *.exrc* *_exrc* *$MYVIMRC*
c. Four places are searched for initializations.  The first that exists
    is used, the others are ignored.  The $MYVIMRC environment variable is
    set to the file that was first found, unless $MYVIMRC was already set
    and when using VIMINIT.
    -  The environment variable VIMINIT (see also |compatible-default|) (*)
       The value of $VIMINIT is used as an Ex command line.
    -  The user vimrc file(s):
                "$HOME/.vimrc"         (for Unix and OS/2) (*)
                "$HOME/.vim/vimrc"     (for Unix and OS/2) (*)
                "s:.vimrc"             (for Amiga) (*)
                "home:.vimrc"          (for Amiga) (*)
                "home:vimfiles:vimrc"  (for Amiga) (*)
                "$VIM/.vimrc"          (for OS/2 and Amiga) (*)
                "$HOME/_vimrc"         (for MS-DOS and Win32) (*)
                "$HOME/vimfiles/vimrc" (for MS-DOS and Win32) (*)
                "$VIM/_vimrc"          (for MS-DOS and Win32) (*)
            Note: For Unix, OS/2 and Amiga, when ".vimrc" does not exist,
            "_vimrc" is also tried, in case an MS-DOS compatible file
            system is used.  For MS-DOS and Win32 ".vimrc" is checked
            after "_vimrc", in case long file names are used.
            Note: For MS-DOS and Win32, "$HOME" is checked first.  If no
            "_vimrc" or ".vimrc" is found there, "$VIM" is tried.
            See |$VIM| for when $VIM is not set.
    -  The environment variable EXINIT.
       The value of $EXINIT is used as an Ex command line.
    -  The user exrc file(s).  Same as for the user vimrc file, but with
       "vimrc" replaced by "exrc".  But only one of ".exrc" and "_exrc" is
       used, depending on the system.  And without the (*)!

... that The first that exists is used, the others are ignored. when it comes to configuration files. So it would seem, then, that there is no way for a file to be sourced by all instances of vim, after the global vim config file is sourced, aside from the user's vim configuration file.

So then the question becomes: is it poor practice to symlink multiple users' .vimrc files to one user's .vimrc file? Because that seems to be the only way to modify a configuration file which is not the /etc/vim/vimrc gargantuan monster which I want to avoid touching (and thus risk breaking).

  • Does sudoedit work on your Pi? If so, you can use your user’s custom vimrc when editing files (or use a plugin like tpope’s eunuch that gives the :SudoWrite command) – D. Ben Knoble Jul 27 '18 at 11:54
1

You can take a look at the man page or at :help system-vimrc or :version to find out which is your real system wide vimrc file. It should probably source the other one (if it doesn't then it is clear why your changes take no effect if you put them there).

Remember that the system vimrc file is sourced in 'compatible' mode and 'hlsearch' is only available in vim but not vi so you might need to :set nocp first.

  • Running :help system-vimrc was useful, but didn't tell me which file is sourced first. Searching for /etc/vim also returned an error: E486: Pattern not found: \/etc\/vim. – user3.1415927 Jul 27 '18 at 16:51
0

TL;DR: Use source in the main global config file to source another global config file - tie to an if statement for robustness.


Thanks to @Lucas's suggestion, I looked into :help system-vimrc and the surrounding sections. I found that $VIM/vimrc was a symbolic link pointing to /etc/vim/vimrc. This is the base file sourced in my case for vim configuration across the entire system. I thought that since the help file specifies vim uses The first that exists is used, the others are ignored that I was out of luck, until I looked at the end of this configuration file and found the lines:

" Source a global configuration file if available
if filereadable("/etc/vim/vimrc.local")
  source /etc/vim/vimrc.local
endif

So I created the file /etc/vim/vimrc.local and added my global config parameters there. User .vimrc files are still sourced (confirmed by listing different configs in root and user files, comparing operation in sudo and standard vim instances) as appropriate, so users can still have their own configs.

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.