Vim use a variable interpolated within a String? - vim

I have a .zshrc and .vimrc file
In my .zshrc file I have syncfolder="$HOME/Google Drive/Dropbox"
I now want to reference syncfolder within my .vimrc file, is this possible (if so, how)?
Also, within my .vimrc file I'm sourcing lots of other files and I want to make that more efficient.
The following doesn't work:
let vimfolder="~/Google Drive/Dropbox/Fresh Install/Shell/vim"
source "$vimfolder/settings.vim"
source $vimfolder/vundle.vim
source $vimfolder/mapping.vim
source $vimfolder/filetypes.vim
source $vimfolder/commands.vim
...I've tried multiple variants where vimfolder included $HOME and in another I've tried exporting a variable from my .zshrc file, like so...
export SYNCFOLDER=$syncfolder
...in the hope that I could access it from within my .vimrc but it didn't work.
Any ideas?

You need to use :execute to evaluate a variable in most Ex commands. Vim variables do not have a $ prefix, those are environment variables (which you can reference from Vimscript, too):
execute 'source' vimfolder . '/settings.vim'
However, in this particular case, I'd rather add the location to 'runtimepath' and then use :runtime:
set runtimepath+=~/Google\ Drive/Dropbox/Fresh\ Install/Shell/vim
runtime settings.vim
Another alternative is symbolic links, so you don't have change anything in your Vim setup.

Related

vi, set user settings

When using vi I almost always need to do
:set hlsearch
:set number
How can I make my system always load these as presets or something when I use vi.
In one machine I am root, in another not. So please include an answer for both. Thank you!
just create a .vimrc file with the following entries:
set hlsearch
set number
Put this file in the $HOME directory of the user you are using on the particular machine:
/root/on the machine where you are root.
/home/USERNAMEon the machine where your user is USERNAME
if .vimrc does not exist, create it.
You can create a file named .exrc in your home directory and write all the commands (without the preceding : ) there.
When you start the vi editor, the editor searches for the environment variable $EXINIT and uses the contents of the file it points to as configuration commands, if it exists. If EXINIT is not defined, vi looks for the .exrc file in your HOME directory, and uses its configuration commands. Finally, vi looks in your current directory for a file named .exrc and executes the commands in that file, if it exists. In this manner, you can have a different vi configuration for each directory or project that you're working on.
(http://alvinalexander.com/unix/edu/un010003/)
The corresponding file for vim is called .vimrc.

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.

How to prevent syntastic from creating a directory for every vim instance?

When using the syntastic plugin with vim, I see a new /tmp/vXXXXXXX directory every time I open a new vim instance. When the syntastic plugin gets disabled, no such directories are created.
When I ran inotify, I found that a numeric file is created in that directory every time I save a file. Is it possible to make syntastic (or vim) create a temporary directory on demand? Failing on that, can I make it use a single directory instead? For example, /tmp/vim-syntastic/vXXXXXXX/?
According to the developer, syntastic does not create temporary directories by itself, that is handled by vim. Looking a bit further, I found that vim uses $TMPDIR to set a temporary directory. If the directory is unwritable, then it gets ignored.
So, as a solution, the following lines set the temporary directory to /tmp/vim-USERNAME, and then create it (ignoring errors that normally occur when the directory exists):
" Keep all vim-related temp files in a single directory
let $TMPDIR = '/tmp/vim-' . $USER
silent! call mkdir($TMPDIR, '', 0700)
Now, I do not have a lot of /tmp/vXXXXXX/ directories anymore. Instead, they appear in /tmp/vim-peter/vXXXXXX/ which is great.
If you look in the syntastic helpfiles, you'll see that syntastic uses a 'tail' file for storing the output of a given make program. You can override the default tail for a given filetype and subchecker by adding the following to your vimrc:
let g:syntastic_<filetype>_<subchecker>_tail = "> /tmp/vim-syntastic/your-file-here"
So for example if you wanted mri to output to /tmp/vim-syntastic/ruby-mri, you would write:
let g:syntastic_ruby_mri_tail = "> /tmp/vim-syntastic/ruby-mri"
See :help syntastic-config-makeprg for more info. Here's a direct link on git. As far as I know there's no built-in way to set the default directory for all syntastic output, unfortunately.
Edit: Lekenstein found another solution, which he posted in the linked Github issue.
let $TMPDIR = '/tmp/vim-' . $USER
silent! call mkdir($TMPDIR, '', 0700)
This will make a special directory for all vim-related temporary files. That means it will also affect temporary files not related to syntastic.

Change default location of vimrc

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

What is the most elegant way to deal with sourced files that themselves source (relative) source files in VIM?

I am editing a file like
/path/to/file.txt with vim, hence the current directory is
/path/to.
Now, I have a directory
/other/path/to/vim/files
that contains sourceA.vim. Also, there is a sourceB.vim file in
/other/path/to/vim/files/lib/sourceB.vim
In sourceA.vim, I want to source sourceB.vim, so I put a
so lib/sourceB.vim
into it.
Now, in my file.txt, I do a
:so /other/path/to/vim/files/sourceA.vim
which fails, because the sourcing system is obviously not prepared for relative path names along with sourcing from another directory.
In order to fix this, I put a
execute "so " . expand("<sfile>:p:h") . "/lib/sourceB.vim"
into sourceA.vim which does what I want.
However, I find the solution a bit clumsy and was wondering if there is a more elegant solution to it.
I cannot put the sourceA.vim nor sourceB.vim into vim's plugin folder.
Maybe you could modify your runtimepath in your vimrc or elsewhere:
set runtimepath+=/other/path/to/vim/files
Then use :runtime instead of :source in your sourceA.vim file:
runtime lib/sourceB.vim
You can then use the same ":so /../../../sourceA.vim" command as before...

Resources