574

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Eman
  • 6,383
  • 4
  • 23
  • 29
  • 37
    If it isn't there, create it. –  Jun 06 '12 at 20:11
  • 14
    My Ubuntu machine had it in a file named `/usr/share/vim/vimrc`. – ckjbgames Mar 15 '17 at 15:20
  • 8
    Possible duplicate of [How to locate the vimrc file used by vim editor?](https://stackoverflow.com/questions/8977649/how-to-locate-the-vimrc-file-used-by-vim-editor) – o12d10 Jun 22 '18 at 15:37
  • Confirming Lubuntu 21.10 follows the convention in the @ckjbgames comment (```/usr/share/vim/vimrc``` without the period). However, this file contains the following comment: ```Vim will load $VIMRUNTIME/defaults.vim if the user does not have a vimrc. This happens after /etc/vim/vimrc(.local) are loaded, so it will override any settings in these files.``` – Greg M. Krsak Feb 14 '22 at 23:23
  • My ArchLinux (firmware version: linux-6.1.3.arch1-1) has vimrc at location: /etc/vimrc, you can use ':scriptnames' command in any vim session to display the files that vim has loaded which obviously displays "vimrc"\'s location usually at the very top. – Syed M Abbas Haider Taqvi Jan 06 '23 at 10:22

22 Answers22

661

You need to create it. In most installations I've used it hasn't been created by default.

You usually create it as ~/.vimrc.

alex
  • 479,566
  • 201
  • 878
  • 984
AlG
  • 14,697
  • 4
  • 41
  • 54
  • 59
    also check /etc/vim/vimrc – Natesh bhat Oct 06 '18 at 12:48
  • 1
    @Nateshbhat: should I keep using `/etc/vim/vimrc` or copy that to `~/.vimrc`? Or can I have both? – Mohammad Faisal Jul 23 '19 at 17:26
  • If you want to customize it, I'd copy it local first. Otherwise you are changing settings for all users. – AlG Jul 26 '19 at 10:29
  • 6
    You should be aware that if you create this file, it will ignore all settings in the system vimrc file. (For example `/etc/vim/vimrc`.) This confused me no end after I created a `.vimrc`, didn't populate it completely with all my usual options (because it was on a server that I didn't intend to do much work on) and it left my vim in a broken or nonfunctional state. – FreelanceConsultant Jun 27 '20 at 13:06
  • Hey!. I am using VIM extension in VSCode . Is there any way to change the keybindings of the extension? – Jdeep Jul 21 '20 at 08:29
  • @NoahJ.Standerson That's a good question! You should post it as a separate question to post and ask, separately, as the answer is specific to VSCode. – AlG Jul 21 '20 at 12:16
  • @AlG , Okay just I found it out and it was pretty simple. https://stackoverflow.com/a/63030352/12982627 – Jdeep Jul 22 '20 at 08:51
  • This answer was only helpful to the OP in his context but not to anybody else who came here trying to find out "Where is my .vimrc file". – papiro Aug 29 '20 at 17:15
  • Why is this not by default? It's far superior than not having it – JobHunter69 Sep 16 '21 at 21:11
  • @Nateshbhat hope this vimrc is equals to .vimrc – linjiejun May 30 '22 at 03:04
  • @linjiejun Most that I've seen have your local copy as `.vimrc` and the default one as `vimrc`. That said, it can vary by installation. See Laurence's answer below. – AlG May 31 '22 at 12:00
408

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.

mit
  • 11,083
  • 11
  • 50
  • 74
Philia Fan
  • 4,388
  • 1
  • 12
  • 6
  • 7
    @MorganWilde Not necessarily, `:scriptnames` and `:e $MYVIMRC` both return blank outputs for me. – tim-phillips Jan 28 '14 at 06:11
  • 1
    +1 for `:scriptnames`. However `$MYVIMRC` isn't guaranteed to be set, for example when starting vim with `vim -u filename`. – 1983 Sep 07 '14 at 23:26
  • 4
    :scriptnames has vimrc but not .vimrc, are they the same? I find them located in my usr/share folder, would adding ~/.vimrc (as accepted answer) even do anything then without further configuration? Is that a way to add user specific options to the vimrc perhaps? – Tyler Nov 01 '14 at 15:33
  • 27
    Note that `:e $MYVIMRC` only works when there is a `vimrc` file in the first place. Otherwise this will start edting a file literally called `$MYVIMRC`. – Tomalak Mar 12 '16 at 08:28
  • Awesome answer. Mine was sitting in `/etc/vimrc` – Shadoninja Apr 07 '16 at 17:41
  • 1
    @Shadoninja - that wasn't "yours", that is the global `rc` file. Whatever you define in your `.vimrc` file (in most cases in your home directory) will override the defaults in `/etc/vimrc`. –  Apr 06 '18 at 18:19
120

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

Laurence Gonsalves
  • 137,896
  • 35
  • 246
  • 299
31

As additional information, mostly in macOS, the .vimrc file is located at directory:

/usr/share/vim/.vimrc
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
hihell
  • 936
  • 1
  • 10
  • 20
27
:echo($MYVIMRC)

will give you the location of your .vimrc file.

:e $MYVIMRC

will open it.

Rook
  • 60,248
  • 49
  • 165
  • 242
  • 1
    I take back the following: -bash: syntax error near unexpected token '$MYVIMRC' – Yannis Dran Jan 30 '14 at 02:37
  • @YannisDran - Uhmm, what? – Rook Jan 30 '14 at 14:25
  • @Idigas I was typing :echo(xxx) on my command window but I realized now you meant to type this into the vim editor. However, even though :e xxx works, the :echo(xxx) doesn't. It says E121 Undefined Variable: xxx E15: Invalid Expression : ($xxx) – Yannis Dran Jan 30 '14 at 20:00
  • @YannisDran - Just to be clear, you got that error upon typing :echo($myvimrc) into the vim command line? Not :echo(myvimrc) by any chance? – Rook Jan 30 '14 at 22:32
  • 1
    I use the $ and I did it into the vi editor(Typing : enables command line). Btw, now vim responds nothing, it only puts the indicator at the first line of vim. (Vim-Vi Improved v.7.3 on OSX) – Yannis Dran Jan 30 '14 at 22:49
  • @Idigas Great. Could you tell me the equivalent $MYVIMRC to .bashrc file if any? – Lerner Zhang Jul 01 '15 at 06:47
  • @leoadams - I don't know of any (I'm on Windows). I believe this is only a Vim thing. But if I find out anything I'll post it here. – Rook Jul 01 '15 at 11:56
  • @Idigas Yeah, I agree with you. It's only a Vim thing. – Lerner Zhang Jul 01 '15 at 11:59
24

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"
SebMa
  • 4,037
  • 29
  • 39
droid192
  • 2,011
  • 26
  • 43
  • Can verify that this works with the VIM that bundles with Git for Windows. (Noting this only because so much that an SO answer might presume that DOESN'T work in Git Bash.) – Janet Jul 02 '19 at 13:00
  • I prefer this answer, since **it will always provide updated and authentic information** regarding the valid vimrc file paths for a given system and vim installation. – Semnodime Jan 30 '23 at 18:15
22

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aaron
  • 483
  • 1
  • 6
  • 17
  • 2
    I don't understand how the existing answers didn't quite work for you when in fact the solution you proposed is already covered by them. [This answer](https://stackoverflow.com/a/34005877/1743811) mentions `$HOME/.vimrc` and [the top answer](https://stackoverflow.com/a/10921485/1743811) mentions `~/.vimrc` which is the same as `$HOME/.vimrc`. – doubleDown Sep 05 '17 at 09:30
  • Completely agree with you. What I meant to say was lost when my answer was edited – Aaron Nov 02 '17 at 16:31
16

Open Vim, and in normal mode type:

:echo $VIM
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
9

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
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
  • if the `*` acts like a regular glob, you should do instead: `find / \( -iname "*vimrc*" -o -iname ".*vimrc*" \) -type f 2>/dev/null` , otherwise you'll miss the filenames starting with a `.` (such as `~/.vimrc` , etc) as in a regular shell glob, `*filename` doesn't grab files starting with a `.` (as those are "lightly hidden" this naming scheme in most unix/linux commands (compare `ls -l` and `ls -al`, and `echo *` will show only those who don't start with a `.`) , and thus a regular `*` glob is not including those by default) – Olivier Dulac Jan 31 '22 at 14:40
7

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.

Community
  • 1
  • 1
Ben
  • 54,723
  • 49
  • 178
  • 224
7

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 :

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

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
6

I'd like to share how I set showing the line number as the default on Mac.

  1. In a terminal, type cd. This will help you go to the home folder.
  2. In the terminal, type vi .vimrc. This will create an empty vimrc system file which you want to use.
  3. 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.
  4. 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jinggang
  • 401
  • 7
  • 13
6

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

blckCoach
  • 316
  • 2
  • 7
3

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
deb2fast
  • 990
  • 11
  • 18
2

In SUSE Linux Enterprise Server (SLES) and openSUSE the global one is located at /etc/vimrc.

To edit it, simply do vi /etc/vimrc.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Enrique Saez
  • 2,504
  • 16
  • 21
2

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mythicalcoder
  • 3,143
  • 1
  • 32
  • 42
1

From cmd (Windows):

C\Users\You> `vim foo.txt`

Now in Vim, enter command mode by typing: ":" (i.e. Shift + ;)

:tabedit $HOME/.vimrc
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jason
  • 1,974
  • 24
  • 19
1

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

buddemat
  • 4,552
  • 14
  • 29
  • 49
Bishop
  • 11
  • 2
  • 1
    This would be because you're using Neovim, which defaults to using the (preferred) $XDG_CONFIG_HOME directory for app configuration. This helps to de-clutter the $HOME directory. There's been an RFC out on Vim for a few years now to move to the same convention, though not much has come of it: https://github.com/vim/vim/issues/2034 – JustALawnGnome7 Dec 21 '22 at 03:55
0

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.

0

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.

jdempcy
  • 136
  • 1
  • 4
0

I don't know about in other versions of Vim, but in Vim 9.0, the output of vim --version includes great information about this. On my system, the output includes the following:

   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"
Steve Jorgensen
  • 11,725
  • 1
  • 33
  • 43
-2

if in Windows, it could be in your C directory under Program Files(x86) in the folder "vim"

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 21 '21 at 13:21