Does vim load /etc/vimrc after ~/.vimrc? - vim

From :h initialization, it seems to say in step 3:
b. For Unix, MS-DOS, MS-Windows, OS/2, VMS, Macintosh, RISC-OS and Amiga, the system vimrc file is read for initializations...
c. Four places are searched for initializations... "$HOME/.vimrc" (for Unix and OS/2)
that /etc/vimrc is loaded ahead of ~/.vimrc.
However, when I added:
set textwidth=0
in my ~/.vimrc, I see via :verbose set textwidth? that it is still set by /etc/vimrc after launching vim. If I then explicitly :source ~/.vimrc, textwidth is then shown to be set by ~/.vimrc.
Doesn't this seem to indicate that system vimrc gets loaded after user vimrc (which doesn't seem to make sense)?
FWIW, :scriptnames does list ~/.vimrc after /etc/vimrc (which is the first item listed), although I'm not entirely sure the order reflects the start-up loading order of vim.

Related

Inconsistent initialization

MacVim 8.0:
If there is no .vimrc file, set shows:
:set
--- Options ---
autoindent scroll=18 ttymouse=xterm2
helplang=en showmatch wrapmargin=10
langmenu=none ttyfast nowrapscan
backspace=indent,eol,start
fileencodings=ucs-bom,utf-8,default,latin1
pythondll=/usr/local/Frameworks/Python.framework/Versions/2.7/Python
pythonhome=/usr/local/Frameworks/Python.framework/Versions/2.7
If I create an empty .vimrc file, set shows:
set
--- Options ---
helplang=en scroll=18 ttymouse=xterm2
langmenu=none ttyfast
backspace=indent,eol,start
fileencodings=ucs-bom,utf-8,default,latin1
pythondll=/usr/local/Frameworks/Python.framework/Versions/2.7/Python
pythonhome=/usr/local/Frameworks/Python.framework/Versions/2.7
This differs from the first case in that autoindent, showmatch, and wrapscan are missing. Why is there a difference? These options do not seem to be set in /Applications/MacVim.app/Contents/Resources/vim/vimrc.
Starting vim with -u file is supposed to use file as the startup file. So if I remove the empty .vimrc--so now I have no .vimrc--and use -u emptyfile, where emptyfile is an empty file, I would expect that set would show what is listed immediately above. However, set shows something very different:
:set
--- Options ---
helplang=en scroll=18 ttyfast ttymouse=xterm
fileencodings=ucs-bom,utf-8,default,latin1
iskeyword=#,48-57,_,192-255
Why is this different from the previous case?
(Why do I care? Because I'm trying to figure out why an initialization file executes properly when it's .vimrc, but not when I run it with -u.)
Without a vimrc at any of the expected locations, Vim sources:
$VIM/vimrc
$VIMRUNTIME/defaults.vim
...
This is new in Vim 8.0. Before that version, Vim would only source $VIM/vimrc in this scenario. The reasoning behind that new feature was allegedly to provide more useful defaults to infrequent users without asking them to write their own vimrc.
See :help defaults.vim.
With a vimrc at one of the expected locations (assuming ~/.vimrc but it can be ~/.vim/vimrc), Vim sources:
$VIM/vimrc
$HOME/.vimrc
...
This is the optimal scenario for frequent users, experienced or not: you get the basic behavior plus all your fancy stuff.
When pointing Vim to a specific vimrc with -u, Vim only sources that specific vimrc.
This is the absolutist scenario that gives you complete control over Vim's settings. I wouldn't recommend it to inexperienced users.
I have found part of the answer. (I'll post it here for anyone with a similar question, but I don't expect upvotes for either the question or the answer.)
help initialization says, among other things:
If Vim was started with "-u filename", the file "filename" is used.
All following initializations until 4. are skipped. $MYVIMRC is not
set.
Most of what is between that point and 4. was not relevant to my situation; these are steps that occur only in special circumstances. However, one item between that point and 4. is:
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.
So the system initialization file is not run when -u is used. (This still doesn't fully answer my first question, which mentions the system initialization file on my system.)

Are plugins loaded by vimrc or afterwards?

I'm confused about the order in which Vim loads plugin files, and seem to be finding mixed answers online. Some answers seem to suggest that vimrc is loaded before plugins, while others suggest that they are loaded during the sourcing of vimrc, at the line filetype plugin indent on. Can someone please clarify the order in which vimrc, plugins, and plugins in the after/ directory are loaded, what causes each to load, and when each could be reloaded during a vim session (e.g. what happens when sourcing vimrc again, what happens when setting the filetype, etc.)?
Some answers seem to suggest that vimrc is loaded before plugins, while others suggest that they are loaded during the sourcing of vimrc, at the line filetype plugin indent on.
All plugins are sourced (the correct term) after your vimrc unless you source them manually. The filetype plugin indent on line doesn't change anything to that order.
Can someone please clarify the order in which vimrc, plugins, and plugins in the after/ directory are loaded,
Assuming you have filetype plugin indent on in your vimrc:
The system vimrc if there is one.
Your vimrc.
Built-in plugins.
Your plugins.
Built-in filetype-specific plugins.
Stuff in the after/ directory.
The whole thing is explained in :help startup and can be seen very clearly with :scriptnames.
what causes each to load,
The value of &runtimepath in general and the :filetype command for filetype-specific stuff.
and when each could be reloaded during a vim session (e.g. what happens when sourcing vimrc again, what happens when setting the filetype, etc.)?
:source $MYVIMRC re-executes each command in your vimrc.
Most plugins are written in a way that prevent them from being sourced twice. Read their documentation/code if you want to reset them.
:help :filetype.
.vimrc is executed before loading plugins:
At startup, Vim checks environment variables and files and sets values
accordingly. Vim proceeds in this order:
(...)
Execute Ex commands, from environment variables and/or files
An environment variable is read as one Ex command line, where multiple
commands must be separated with '|' or "".
vimrc exrc
A file that contains initialization commands is called a "vimrc" file.
Each line in a vimrc file is executed as an Ex command line.
(...)
Load the plugin scripts.
Just use :scriptnames to see all the sources files and their order in which they are loaded during startup.

How to add a vimrc path to vim

currently there are a few paths where .vimrc files are being searched. (as can be seen in :scriptnames command).
How do I add another path?
You're mistaken. :scriptnames tells you which scripts were loaded. It will be:
.vimrc,
possibly .gvimrc,
plus all plugins in 'runtimepath',
plus all ftplugins, syntax files and indent files in runtimepath that apply for all the buffers you have edited,
plus the autoload plugins loaded by the previous files,
plus the files you've sourced manually.
If you want to change the places where to search for your .vimrc, it will be more complex as vim has a very specific heuristic to search for a .vimrc. See :h startup.
Any way. If you really want to add a path where the .vimrc file will be searched, it's not possible unless you define an alias to vim that use the -u flags.
If you want to add other paths where to look for plugins, you'll have to set the 'runtimepath' option in your .vimrc. For instance, :set rtp+=~/.vim/addon/foobar will have all plugins named $HOME/.vim/addon/foo/plugin/*.vim and $HOME/.vim/addon/foo/after/plugin/*.vim loaded automatically, plus the ftplugin/syntax file/indent files loaded automatically as well if you enable them, and when you enter a buffer related to them.
The 'runtimepath' option specifies the locations of the Vim configuration subdirectories (i.e. directories containing autoload/, plugin/, syntax/, etc.) The Pathogen plugin made it popular to extend this so that each plugin is installed into a separate such subdirectory, and other plugin managers (like Vundle) do that as well.
Now, there's only one .vimrc (and you can change its location via the -u command-line argument), but nothing prevents you from using :source path/to/another/script.vim to load other Vim scripts during startup.
TL;DR
To execute a separate Vimscript file during startup, just :source it from your ~/.vimrc. If you have a plugin(s) that you want to install in a separate location, use :set runtimepath+=path/to/pluginroot in your ~/.vimrc, or just use Pathogen or another plugin manager.

vundle: ConqueTerm: not an edit command

I tried to install Conque-Shell via Vundle, and when I input :ConqueTerm bash in vim, it showed 'ConqueTerm: Not an edit command'. I thought there was something wrong with my path. But I did write set rtp+=~/.vim/bundle/vundle in my .gvimrc. And the configure of Vundle is at the beginning of the .gvimrc.
I copied the .vim/bundle/Conque-Shell/plugin/conque_term.vim to .vim/plugin/conque_term.vim and then it worked.
So, is there anything wrong with my .gvimrc?? Thanks!
My .gvimrc: https://gist.github.com/guori12321/5506991
If you observe the output of :scriptnames (the list of scripts sourced during startup), you'll note that .gvimrc comes last, after .vimrc and the plugins. Therefore, any changes to the 'runtimepath' there are too late; plugins (like ConqueTerm) have already been loaded. You can read more about the startup process at :help initialization.
Even if you only use the GUI GVIM, put the common settings into ~/.vimrc; the ~/.gvimrc file is for GUI-only stuff that doesn't apply to the terminal Vim, e.g. setting 'guifont' and similar options.

Where is my .vimrc file?

I have been using Vim, and I would really like to save my settings. The problem I am having is that I cannot find my .vimrc file, and it is not in the standard /home/user/.vimrc location. How might I find this file?
You need to create it. In most installations I've used it hasn't been created by default.
You usually create it as ~/.vimrc.
These methods work, if you already have a .vimrc file:
:scriptnames list all the .vim files that Vim loaded for you, including your .vimrc file.
:e $MYVIMRC open & edit the current .vimrc that you are using, then use Ctrl + G to view the path in status bar.
Short answer:
To create your vimrc, start up Vim and do one of the following:
:e $HOME/.vimrc " on Unix, Mac or OS/2
:e $HOME/_vimrc " on Windows
:e s:.vimrc " on Amiga
Insert the settings you want, and save the file.
Note that exisitence of this file will disable the compatible option. See below for details.
Long answer:
There are two kinds of vimrc:
the user vimrc in $HOME
the system vimrc in $VIM (on Amiga systems, s:.vimrc is considered a user vimrc)
The user vimrc file often does not exist until created by the user. If you cannot find $HOME/.vimrc (or $HOME/_vimrc on Windows) then you can, and probably should, just create it.
The system vimrc should normally be left unmodified and is located in the $VIM* directory. The system vimrc is not a good place you keep your personal settings. If you modify this file your changes may be overwritten if you ever upgrade Vim. Also, changes here will affect other users on a multi-user system. In most cases, settings in the user vimrc will override settings in the system vimrc.
From :help vimrc:
A file that contains initialization commands is called a "vimrc" file.
Each line in a vimrc file is executed as an Ex command line. It is
sometimes also referred to as "exrc" file. They are the same type of
file, but "exrc" is what Vi always used, "vimrc" is a Vim specific
name. Also see |vimrc-intro|.
Places for your personal initializations:
Unix $HOME/.vimrc or $HOME/.vim/vimrc
OS/2 $HOME/.vimrc, $HOME/vimfiles/vimrc
or $VIM/.vimrc (or _vimrc)
MS-Windows $HOME/_vimrc, $HOME/vimfiles/vimrc
or $VIM/_vimrc
Amiga s:.vimrc, home:.vimrc, home:vimfiles:vimrc
or $VIM/.vimrc
The files are searched in the order specified above and only the first
one that is found is read.
(MacOS counts as Unix for the above.)
Note that the mere existence of a user vimrc will change Vim's behavior by turning off the compatible option. From :help compatible-default:
When Vim starts, the 'compatible' option is on. This will be used when Vim
starts its initializations. But as soon as a user vimrc file is found, or a
vimrc file in the current directory, or the "VIMINIT" environment variable is
set, it will be set to 'nocompatible'. This has the side effect of setting or
resetting other options (see 'compatible'). But only the options that have
not been set or reset will be changed.
* $VIM may not be set in your shell, but is always set inside Vim. If you want to see what it's set to, start up Vim and use the command :echo $VIM
As additional information, mostly in macOS, the .vimrc file is located at directory:
/usr/share/vim/.vimrc
:echo($MYVIMRC)
will give you the location of your .vimrc file.
:e $MYVIMRC
will open it.
For whatever reason, these answers didn't quite work for me. This is what worked for me instead:
In Vim, the :version command gives you the paths of system and user vimrc and gvimrc files (among other things), and the output looks something like this:
system vimrc file: "$VIM/vimrc"
user vimrc file: "$HOME/.vimrc"
user exrc file: "$HOME/.exrc"
system gvimrc file: "$VIM/gvimrc"
user gvimrc file: "$HOME/.gvimrc"
The one you want is user vimrc file: "$HOME/.vimrc"
So to edit the file: vim $HOME/.vimrc
Source: Open vimrc file
on unix vim --version tells you the various locations of the vim config files :
system vimrc file: "$VIM/vimrc"
user vimrc file: "$HOME/.vimrc"
2nd user vimrc file: "~/.vim/vimrc"
user exrc file: "$HOME/.exrc"
defaults file: "$VIMRUNTIME/defaults.vim"
fall-back for $VIM: "/usr/share/vim"
Open Vim, and in normal mode type:
:echo $VIM
Useful Information can be obtained using the find command
find / -iname "*vimrc*" -type f 2>/dev/null
There are many answers already, but it can sometimes be useful to simply run a "find" for anything containing the name "vimrc".
The reason is that this will show you what files you actualy have available on the system currently, rather than what you might put on your system. (The information for which you would obtain from :version as explained in other answers.)
Example result on my system
On my system this produces
/usr/share/vim/vim82/vimrc_example.vim
/usr/share/vim/vim82/gvimrc_example.vim
/etc/vim/gvimrc
/etc/vim/vimrc
/etc/vim/vimrc.tiny
Which is quite useful because it tells us that there are 2 example files installed in the share directorys for both gvim and vim, and that there are also some system-wide config files below /etc/.
On my system, I also have a file at ~/.vimrc but this does not appear in this list because it is a link to another file, stored under ~/Linux-Config. But you won't have this directory, it's specific to machines I use on my own network.
Detailed Explanation of find syntax used
Explanation:
find starting at the root directory / (find works recursively)
anything containing the case insensitive regex *vimrc* which means any name with vimrc (case insensitive) in it somewhere, can be preceeded or followed by anything or nothing (*)
type = files (not directory/symlink etc)
throw all errors to /dev/null otherwise the output is spammed with unreadable errors from /proc
Here are a few more tips:
In Arch Linux the global one is at /etc/vimrc. There are some comments in there with helpful details.
Since the filename starts with a ., it's hidden unless you use ls -a to show ALL files.
Typing :version while in Vim will show you a bunch of interesting information including the file location.
If you're not sure what ~/.vimrc means look at this question.
Where is the .vimrc file? It depends on the OS. As you can see, you were looking for /home/$user/.vimrc, which probably means you are using BSD / Linux. Here are the locations for each OS...
BSD / Linux : /home/$user/.vimrc
SunOS / Solaris : /export/home/$user/.vimrc
MacOS : /Users/$user/.vimrc
Android : /data/media/$userid/.vimrc
Unix : $root/home/$user/.vimrc
AT&T Unix : $root/usr/$user/.vimrc
Unix-Derived :
/var/users/$user/.vimrc
/u01/$user/.vimrc
/usr/$user/.vimrc
/user/$user/.vimrc
/users/$user/.vimrc (Source: Wikipedia: Default home directory per operating system.)
If it doesn't exist, create it with ~/.vimrc.
In addition, the root user has their own special .vimrc file, which can be found in /root/.vimrc on BSD / Linux (and in equivalent locations for the other OS's).
The location is set in the $HOME variable, which is always set in Linux environments. (Source: StackExchange->Unix & Linux.)
I'd like to share how I set showing the line number as the default on Mac.
In a terminal, type cd. This will help you go to the home folder.
In the terminal, type vi .vimrc. This will create an empty vimrc system file which you want to use.
In the file, type set number, and then hit Esc on the keyboard and type in :wq. This will set the line number shown in the default setting file vimrc and save it.
vi something to see if this works. If not, try to restart the terminal completely.
If in a terminal, type in cd /usr/share/vim/, go to that folder, and type in ls. You can directly see a file named vimrc. But it's a system file that says read only. I feel it's not a good idea to try modify it. So following the above steps to create a vimrc by yourself is better. It worked for me.
actually you have one vimrc in
/etc/vimrc
when you edit something in there the changes will effect all users
if you don't want that you can create a local vimrc in
~/.vimrc
the changes here will only effect the one user
I tried everything in the previous answer and couldn't find a .vimrc file, so I had to make one.
I copied the example file, cp vimrc_example.vim ~/.vimrc.
I had to create the file, copying from /usr/share/vim/vim74/vimrc_example.vim to ~/.vimrc. Those were the instructions in the vimrc_example file.
My solution is for Unix for other operating systems. According to the Vim documentation, your destination path should be as follows:
For Unix and OS/2 : ~/.vimrc
For Amiga : s:.vimrc
For MS-DOS and Win32: $VIM\_vimrc
For OpenVMS : sys$login:.vimrc
The vimrc file in Ubuntu (12.04 (Precise Pangolin)): I tried :scriptnames in Vim, and it shows both /usr/share/vim/vimrc and ~/.vimrc.
But I had manually created ~/.vimrc.
In SUSE Linux Enterprise Server (SLES) and openSUSE the global one is located at /etc/vimrc.
To edit it, simply do vi /etc/vimrc.
From cmd (Windows):
C\Users\You> `vim foo.txt`
Now in Vim, enter command mode by typing: ":" (i.e. Shift + ;)
:tabedit $HOME/.vimrc
Unfortunately, there are so many answers and none of them helped me.
Until I ran
:checkhealth
in vim and found out that in my case, the vim config file should be named init.vim (under ~/.config/nvim/init.vim).
the name of the file in my system, installed with Garuda Linux is file:///etc/xdg/nvim/sysinit.vim, so try to find this file and add your custom changes.
I was attempting to edit my .vimrc file and this worked for me (macOS Ventura 13.0.1 December 2022).
touch ~/.vimrc
vim ~/.vimrc
I was then able to edit the file to my heart's content, and the next time I ran vim it picked up my changes.
if in Windows, it could be in your C directory under Program Files(x86) in the folder "vim"

Resources