Vim .vimrc custom location - vim

I have used the bare bones of Vim under Ubuntu and now I would like to update my .vimrc file. Although Vim is indeed installed on my PC I don't seem to have a .vimrc file. I would like to add my .vimrc file but it really should go under my ~/.vim/ folder (I like to keep all my configurations in their respected folders).
How does one create a new vimrc file under this location in Ubuntu 13.04?

Two options. (Take a look at :h vimrc which describes the default location and the -u option)
Create an alias to vim -u ~/.vim/vimrc this will cause vim to use that vimrc instead of ~/.vimrc
Or
Upgrade to vim 7.4. One of its default vimrc locations is ~/.vim/vimrc
You can also simulate the vimrc being in a different place.
Create a ~/.vimrc file that contains
runtime vimrc
It's job is to load the first file named vimrc in your runtime path. Which should be ~/.vim/vimrc
Or
You could just symlink ~/.vim/vimrc to ~/.vimrc
ln -s $HOME/.vim/vimrc $HOME/.vimrc
(I've heard some people have had problems with this but I haven't so far)

Related

Why are Vim filetype options ON when ~/.vimrc is missing but OFF when it is present?

I am using Vim 8.1 on a Ubuntu system. Here are the three cases I have found.
Case 1
$ ls -l ~/.vimrc
ls: cannot access '/home/lone/.vimrc': No such file or directory
$ vim -u NONE +':filetype'
filetype detection:OFF plugin:OFF indent:OFF
Case 2
$ ls -l ~/.vimrc
ls: cannot access '/home/lone/.vimrc': No such file or directory
$ vim +':filetype'
filetype detection:ON plugin:ON indent:ON
Case 3
$ touch ~/.vimrc
$ ls -l ~/.vimrc
-rw-r--r-- 1 lone lone 0 Nov 2 18:41 /home/lone/.vimrc
$ vim +':filetype'
filetype detection:OFF plugin:OFF indent:OFF
Question
Why is it that the filetype options are ON when ~/.vimrc is present but OFF when ~/.vimrc is present?
Where in the Vim :help documentation can I find more about this behavior?
-u NONE disables all vimrc's (including the defaults, more on that below)
With no vimrc, the defaults come in to play (assuming you have vim >7.4.2111 or >8)
With a vimrc, the defaults are not sourced.
From :help defaults.vim:
Defaults without a .vimrc file ~
*defaults.vim*
If Vim is started normally and no user vimrc file is found, the
$VIMRUNTIME/defaults.vim script is loaded. This will set 'compatible' off,
switch on syntax highlighting and a few more things. See the script for
details. NOTE: this is done since Vim 8.0, not in Vim 7.4. (it was added in
patch 7.4.2111 to be exact).
This should work well for new Vim users. If you create your own .vimrc, it is
recommended to add these lines somewhere near the top: >
unlet! skip_defaults_vim
source $VIMRUNTIME/defaults.vim
Then Vim works like before you had a .vimrc. Copying $VIMRUNTIME/vimrc_example
is way to do this. Alternatively, you can copy defaults.vim to your .vimrc
and modify it (but then you won't get updates when it changes).
If you don't like some of the defaults, you can still source defaults.vim and
revert individual settings. See the defaults.vim file for hints on how to
revert each item.
*skip_defaults_vim*
If you use a system-wide vimrc and don't want defaults.vim to change settings,
set the "skip_defaults_vim" variable. If this was set and you want to load
defaults.vim from your .vimrc, first unlet skip_defaults_vim, as in the
example above.
vim -u NONE starts vim without any config file and sets all options to default values.
vim without ~/.vimrc reads configuration values from a system config file. Try /etc/vim/vimrc or /usr/share/vim/vimrc.
See http://vimdoc.sourceforge.net/htmldoc/starting.html#initialization

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"

Trouble with vimrc file recognition with MacVim

I'm having difficulty getting MacVim (7.3-64) to recognize my .vimrc and .gvimrc files since upgrading to OS X 10.7.3. Previous, I've simply symlinked my .vimrc and .gvimrc using these commmands:
$ ln -s ~/.vim/vimrc /usr/local/Cellar/macvim/7.3-57/MacVim.app/Contents/Resources/vim/.vimrc
$ ln -s ~/.vim/gvimrc /usr/local/Cellar/macvim/7.3-57/MacVim.app/Contents/Resources/vim/.gvimrc
However, when I currently symlink my rc files, I can not get MacVim to recognize them. I've installed MacVim via Homebrew. Does anyone have any suggests as to what the problem could be?
The ~/.vim/ folder (for colorschemes and scripts) and the ~/.vimrc file (for custom settings and mappings) are guaranteed to work on every UNIX-like systems but neither of these are required for Vim (and MacVim) to work properly.
Start without symlinks or whatever : no .vim folder and no .vimrc or .gvimrc in your home folder. Does MacVim work? You are supposed to see a window with some introductory text, do you see that?
Quit MacVim and turn to the original .(g)vimrc files you want to use: where are they located? Where did you get them from? What is their content? Do you actually, really need them? Do their names actually start with a dot (do you see them or not in the Finder)? What is their encoding (in vim, :set fileencoding? and in the terminal, $ file /path/to/original/.vimrc)? Please, paste the content of the .vimrc file you want to use in your question.
If you are absolutely certain you need these files to work efficiently in Vim and assuming you actually have a custom .vimrc somewhere on your Mac, open Terminal.app and type this command (without the $):
$ cp /path/to/original/.vimrc ~/.vimrc
to copy your .vimrc to the canonical location.
Now launch MacVim. What do you see? You are supposed to see a window with some introductory text, is that what you see?
If MacVim doesn't work correctly with your ~/.vimrc you might want to comment its content, relaunch MacVim, uncomment a few lines and so on until you eventually find a bad setting.
Just put your .vimrc and .gvimrc into your home directory, i.e. ~/ It's picked up there allright.
BTW, you don't need to link to the homebrew dir. Those rc files are default files which you can override with your local user files.

What is the issue with vim -u /path_to/vimrc?

I share an user with other people.
Everyone has created a directory into home directory and everyone is working in his "own" directory.
I want to use my own setting when I use vim and I don't want to bother others with my preferences.
I created my .vimrc file into $HOME/my_directory
I've defined an alias my_vim="vim -u /full_path_to_home/my_directory/.vimrc"
When I edit a file with my_vim, I don't have the right colors.
I have the same problem when I use the command
:source /full_path_to_home/my_directory/.vimrc
If I copy my .vimrc file into $HOME directory, everything is fine.
Where is the problem ?
From :help vimrc
If Vim was started with "-u filename",
the file "filename" is used.
All following initializations until 4.
are skipped.
So by specifying a vimrc file, its ignoring the system-wide vimrc (/erc/vimrc/) where syntax highlighting and other things are configured. You can work around this problem by adding the following code to the top of your vimrc:
if filereadable("/etc/vimrc")
source /etc/vimrc
endif
If this sort of thing comes up a lot, I would recommend changing your $HOME to point to the current $HOME/my_directory whenever you log in.

Vim commands :Explore :Sexplore :Hexplore doesn't work in cygwin

3.3 in cywing 2.721, the installation was made using cywing, every thing works but when I try to use the following command.
:Explore
vim said E492: Not an editor command
also neither :Sexplore or :Hexplore works.
is there any way to activate this functionality?
This is in machine with windows xp.
Well I solved reading the
:help usr_01.txt
It said that is necessary to run this command !cp -i $VIMRUNTIME/vimrc_example.vim ~/.vimrc inside vim, it just copy a .vimrc to home user.
I close and opened vim and :Explore, Hexplore, Vexplore worked.
I had the same problem as well. Since it's a fresh install of Vim you don't have a .vimrc (or _vimrc on Windows). A vimrc is Vim's configuration file, and once you have one Vim will no longer try to be compatible with Vi (usually what you want).
So all you have to do is create a vimrc file and you should be ready to :Explore all you want. Below are locations you can put your vimrc file, and the filename to use for it. (depending on your system)
Unix/Linux/OSX: $HOME/.vimrc or $HOME/.vim/vimrc
MS-Windows: $HOME/_vimrc, $HOME/vimfiles/vimrc
or $VIM/_vimrc
sources:
* :help 'compatible'
* :help 'vimrc'
I also recommend following atomsfat's answer as well to give you a simple vimrc to start out with.
Those commands are provided by the netrw plugin. Check :scriptnames to see if that plugin is loaded.

Resources