Change default location of vimrc - vim

In Vim, is it possible to change the default location of the user vimrc file, i.e., from $HOME/.vimrc to some other location ?

Another solution might be to create a symlink to you preferred location. I have my .vimrc in $HOME/.vim/.vimrc and symlink to it. This way I can have it in a git repo and backup it.

You must start vim with the command vim -u ./path/to/your/vimrcfile
vim -u NONE is a good way to start Vim without any plugin or customisation.
See :help starting.txt for more information.

The VIMINIT variable is my preferred method. The problem with aliasing vim with the -u flag is that if vim is opened in some way other than from the shell command your configuration won't get pulled in. Setting $VIMINIT does not suffer from this drawback. Check this out for more information.
export VIMINIT='source $MYVIMRC'
export MYVIMRC='~/.vim/vimrc' #or any other location you want
Note that Vim normally sets the MYVIMRC variable, though I'm not sure exactly what it's used for. Based on my testing, using VIMINIT in this fashion will result in it not being automatically set on startup as it would normally be. This is why I'm setting it myself.
This works for neovim too!

On Windows, I have the _vimrc that's in my home directory contain one line, source c:\path\to\my.vimrc.
I have not yet worked out a good way to move the entirety of my vimfiles folder, but that's less critical as it's all stuff I've installed from elsewhere. I.e., it'd be easy to restore if I lost it. (I know that I can change runtimepath but my problem is more coming up with a "good" way to do so.)
Update
After six years I extended slightly from what I mention in the comments below; as I put stuff into 'after' and wanted to just keep rtp clean I got something that has been solid for a while now. Today in my %USERPROFILE%\_vimrc I do hardcode the actual paths to things and it changes on every machine I use (and I basically do the same thing on *nix) but this gets copied around mostly-manually when setting up a new PC. I also have a version which I can use to launch Vim on another connected machine on the network, e.g. a co-worker's machine, so I get my config and all that, but the gist is:
set runtimepath^=E:/dotfiles/vim
set runtimepath+=E:/dotfiles/vim/after
set runtimepath-=~/vimfiles
set runtimepath-=~/vimfiles/after
runtime vimrc
and then %USERPROFILE%\_gvimrc just has one line:
runtime gvimrc
(Both vimrc and gvimrc are in the /dotfiles/vim folder and also on Bitbucket.)

I see two options, depending on your needs.
Have ~/.vimrc import the other location
create an alias in your bashrc alias vim="vim -u otherlocation"

I edited
C:\Program Files\Vim\_vimrc
and changed both the runtimepath and sourced my own .vimrc.
I also use these settings in Cygwin (and have them version controlled). So it's this in practice (added at the bottom of the _vimrc file):
let &runtimepath = 'C:\cygwin\home\cygwinaccount\.vim,' . &runtimepath
source C:\cygwin\home\cygwinaccount\.vimrc
Bliss ! :)

In linux:
You can edit .bashrc or .zshrc startup script and add the following lines to change the default location of .vimrc file
export VIMINIT='source $MYVIMRC'
export MYVIMRC='~/.vim/.vimrc' # Note the . (dot) before vimrc. If that is what you have called it.

I feel like the simplest solution is to just have a single line in ~/.vimrc that loads the vimrc from the other location, i.e.:
source PATH/TO/OTHER/LOCATION/.vimrc

Related

NeoVIm does not automatically load ~/.nvimrc file

I was looking to get into learning a text editor for programming. However, I've quickly run into a little snag that I can't seem to find a solution to.
I have modified my /home/user/.nvimrc file to add some plugins and I can load it using :source ~/.nvimrc, however, it never loads automatically. :scriptnames shows a list of scripts in /usr/, but mysteriously absent from the list is the .nvimrc file in my home directory. Again, I can load it in the command line, but I'd like to not have to use :so ~/.nvimrc every time I open a file.
I am not using sudo to run vim.
How can I solve this problem? Is this something everybody has to do?
Could be this issue: https://github.com/neovim/neovim/issues/3530
Summary:
New location is ~/.config/nvim/init.vim
To keep ~/.nvimrc you can source it from the new location:
mkdir -p ~/.config/nvim
echo 'source ~/.nvimrc' > ~/.config/nvim/init.vim
Instead of referring to your rc file directly, consider using $MYVIMRC:
:e $MYVIMRC
:source $MYVIMRC
Reference: Learn Vim the Hard Way/Editing your vimrc
:help config lists the paths for each OS:
Unix ~/.config/nvim/init.vim (or init.lua)
Windows ~/AppData/Local/nvim/init.vim (or init.lua)
$XDG_CONFIG_HOME $XDG_CONFIG_HOME/nvim/init.vim (or init.lua)

How to edit _vimrc file?

I am having trouble saving my settings on vim. I know that in order to do that I need to open the vimrc file but when I do that it says that the file is readonly and won't let me edit it, preventing me from saving my settings.
It says that in order to force save it or override it, I need to add the ! mark after :w in the command section, which I do, but it still does not work.
I can't find any other solution to this problem.
Note: I am using Windows 8.1.
On Windows, you may need to run your editor as administrator to write to your system's _vimrc file.
You may also create a _vimrc in your home directory (e.g. C:\Users\Piet\_vimrc, see the output of :echo $HOME) without administrator access.
You can solve it this way:
Instead of :w, type :w !sudo tee %. I guess it should work.
(my answer is based on this tutotial where the whole problem is well-described)
You may be editing the one global vimrc in /etc/vimrc to which you don't have access.
Make sure you're editing ~/.vimrc and check that you have read and write priviledges to the file (ls -l ~/.vimrc). Since the file is in your home folder you should have those privilages but it won't hurt to verify.

Change location of vimrc

I'm trying to unclutter $HOME, and want to move my .vimrc into ~/.vim. I've already looked at this and this, but none of the solutions presented are what I'm looking for.
I do not want to alias vim with the -u flag because it won't be appropriately set if it is opened in some other way than from the shell.
I do not want to use a symbolic link because it still shows up in $HOME.
I will not compile it from source, I'd rather just use a symlink (which is what I'm currently doing) than deal with that mess.
Can anyone offer some new ideas?
If you are using 7.4, you can simply move your ~/.vimrc file into your ~/.vim/ directory:
~/.vim/vimrc <--- "vimrc", not ".vimrc"
Try this method if you don't use 7.4.

custom .vimrc settings in another file

I want to keep my custom vimrc settings in a file in my Dropbox called vimcustomvimrc.vim
I did some searching and found this http://blog.mojotech.com/a-veterans-vimrc/ line below to put in my .vimrc file so that it will load my custom settings but its not working. can you please help?
file .vimrc contens below
set runtimepath+=~/Dropbox/vim
source ~/Dropbox/vim/vimcustomvimrc.vim
You can maintain your ~/.vimrc under version control using git version control system, for example: I have a .dotfiles repo. In it I have zshrc bashrc and a subfolder called vim. So I made somthing like this...
ln -s ~/.dotfiles/vim ~/.vim
ln -s ~/.dotfiles/vim/vimrc ~/.vimrc
ln -sfvn ~/.dotfiles/zshrc ~/.zshrc
ln -sfvn ~/.dotfiles/bashrc ~/.bashrc
I think is a better solution because you can undo/redo some changes and can also add other good solutions shared on the internet easyly, things like this solution made by Steve Losh, or more specificlly Synchronizing plugins with git submodules and pathogen a great video made by Drew Neil on this matter.
The first line is not needed if you only want to source that specific file; the source line is enough.
The runtimepath option allows you to add or remove directories to the default list of directories where Vim expects to find other default directories and *.vim scripts. If you don't keep any plugin or whatnot in ~/Dropbox/vim/ there's no need to change runtimepath.
Also, there's a lot of bullshit in that article: if I were you I would take it with a grain of salt.

Vim record history

Vim stores the list of commands that we applied using : for the current execution.
But when I close vim and start it again, the vim command history is lost.
I tried set history = 1000 in the .vimrc file but that did not help.
Where does Vim maintain the local command history?
What is the command to retain command history?
Just an issue that caught me out the other day, which may or may not be your problem:
On some Linux systems (e.g. Ubuntu), if the very first time you run VIM, you run it as a super-user, then the $HOME/.viminfo file gets created with root owner and your local user does not have permissions to write to it. This explained why my VIM was not storing command history when it was all configured up correctly.
Bottom line: on a *nix system, locate your .viminfo file, and make sure you have read/write permissions on it.
To check whether Vim supports the 'viminfo' file (which stores the history), :echo has('viminfo'). The corresponding setting must not be empty: :set viminfo?, and :set history? should be greater than one.
If there's a problem writing the viminfo file (though Vim should complain in that case), you could try passing a different location via vim -i /tmp/viminfo
You should check the permissions of the .viminfo file. You might need to change owner of the file to your current user using chown or sudo chown.
Mr. Baint has given the answer.
Go to $HOME directory.
give ls -l .viminfo to check permissions.
change permission so that group and owner also can have write
permission. use:
sudo chown yourUserId $HOME/.viminfo
It should fix the issue.
You will have to set the viminfo option. Set it in your $MYVIMRC
Update To find out where the option was last set/changed:
:verbose set viminfo?
See http://vimdoc.sourceforge.net/htmldoc/starting.html#viminfo-file
If you exit Vim and later start it again, you would normally lose a lot of
information. The viminfo file can be used to remember that information, which
enables you to continue where you left off.
This is introduced in section |21.3| of the user manual.
The viminfo file is used to store:
The command line history.
The search string history.
The input-line history.
Contents of non-empty registers.
Marks for several files.
File marks, pointing to locations in files.
Last search/substitute pattern (for 'n' and '&').
The buffer list.
Global variables.
The viminfo file is not supported when the |+viminfo| feature has been
disabled at compile time.
You could also use Session files.
I went round in circles on this one a bit on Ubuntu and set viminfo solutions proposed above resulted in errors.
I eventually did the command "version" in the command mode and it came back with "-" for most stuff including:
-cmdline_hist
-cmdline_info
I ran the following command and it all worked fine again:
sudo apt install vim
I had the same problem. The issue was that I had vim-minimal installed (this is a default with Fedora), which does not support history. I had to uninstall it and install the full vim instead. I now have history... and syntaxic coloration !
If you are using a vimrc file, check your folders and files for existence and correctness
set history=200
set undolevels=128
set undodir=~/.vim/undodir/
set undofile
set undolevels=1000
set undoreload=10000
If the specified folder (undodir in my case) does not exist, the history will not be saved.

Resources