Why is gvim not loading my _vimrc - vim

I created the following script, by my _vimrc is not loaded...
gvim opens but no vimrc
NOTE: If I put the _vimrc in %USERPROFILE% it is loaded but this is not what I want
#echo off
set GVIMPATH="C:\Program Files\Vim\vim73"
set PATH=%PATH%;%GVIMPATH%
set MYVIMRC=U:\Work\vim\_vimrc
set MYGVIMRC=U:\Work\vim\_vimrc
set VIMHOME=U:\Work\vim
start gvim.exe

What I mentioned here was wrong, MYVIMRC apparently is read-only; one cannot use it to override the .vimrc location. In a wrapper script, better pass the location via the -u command-line argument:
#echo off
set GVIMPATH="C:\Program Files\Vim\vim73"
set PATH=%PATH%;%GVIMPATH%
set VIMHOME=U:\Work\vim
start gvim.exe -u U:\Work\vim\_vimrc -U U:\Work\vim\_gvimrc

You can pass the -u option when you execute vim to load a custom vimrc file from wherever you want, like: vim -u C:\Windows\whatever\vimrc_files\statquant_custom_vimrc, for example.
You can take a look on this SuperUser question, maybe it can help you.

Related

How to fix vim throwing error when SHELL is set to xonsh?

When I start vim I get:
Error detected while processing /home/kossak/.vimrc:
line 1: E484: Can't open file /tmp/vdf7WFR/0
Press ENTER or type command to continue
My first line in ~/.vimrc is let s:uname = system("uname -s").
The error happens because SHELL env var is set to /home/kossak/.local/bin/xonsh. It is set automatically by tmux, because of set -g default-shell /home/kossak/.local/bin/xonsh in my .tmux.conf. I'd like to manually set $SHELL to /bin/bash, without changing default-shell of tmux. Is it possible?
I know I can run tmux setenv -g SHELL '/bin/bash' in CLI and then new panes will have new value, but how to do it in .tmux.conf, so tmux does it automatically for me?
I tried adding setenv -g SHELL '/bin/bash' to .tmux.conf but it doesn't work (the value of $SHELL is not changed).
Just tell vim to prefer bash, to make your vim consistent.
if executable('bash')
set shell=bash
endif
You can't make tmux swap env-vars around, but you can make xonsh do it pretty easily, e.g.
with ${...}.swap({"SHELL": "/bin/bash"}):
vim
would open vim with your environment mutated to set $SHELL=/bin/bash but only for the body of the context manager.
You could set this as an alias for vim in your xonshrc
def _vim():
with ${...}.swap({"SHELL": "/bin/bash"}):
vim
aliases["vim"] = _vim

how to set vi options only for current session?

I often "set number" to see the line numbers, and also "set ic" in Linux vi.
I guess I could set this permanently in .viminfo, but that would affect everyone. Other users might have a different preference.
Is it possible to set them once at the command prompt when I login? Then I wouldn't have to set them every time I open a file.
You can create file named .vimrc in your home directory
and add your preferred config there.
This will effect only in your user.
Example the command below will add set number option permanently only for your user.
echo 'set number' >> ~/.vimrc
Or jest edit the file using vim
vim ~/.vimrc

Separate settings for Easy Vim (with -y flag)

Is there a way to have some settings which will work only for Easy Vim (Vim with -y flag, i.e. vim.exe -y), but not for "normal" Vim?
This works for me:
_vimrc file + _gvimrc file
However, this doesn't:
_vimrc file + _evimrc file
Probably I shouldn't try to use separate file for it, but incorporate such settings into _vimrc or _gvimrc instead? How is it possible?
As far as I can see, this isn't easy, but it's doable:
From within Vimscript (= in your .vimrc), you can call ps to get the arguments vim was called with
echo split(system("ps -o command= -p " . getpid()))
This prints ['vim', '-y'] and you could then go on checking if -y is in that list (with count) and then do different things depending on that.
I see that you're using Windows though, so you have to find an alternative to the ps solution. This answer on superuser makes me believe WMIC path win32_process get Caption,Processid,Commandline might be a start..
I would guess that those "settings" that break in Easy Vim is 'insertmode', as that is the defining option of it. You can check for it in your ~/.vimrc, like this:
if ! &insertmode
" Stuff that only works in default (non-Easy) Vim.
" ...
endif

How to make vim use the same environment as my login shell when running commands with "!"?

I use !ls to execute bash command. But after i have configured something like source ~/.usr_profile in ~/.profile file, vim won't source this file as well. So when i want to execute a function declared in usr_profile , i have to run :!source ~/.usr_profile && my_command. When i using this once or twice, it's ok. But when use it frequently, the my vimrc becomes messy.
Is there any better method to solve this problem.Thanks
Adding this line to your ~/.vimrc should solve your immediate problem:
set shell=bash\ -l
When invoked with -l (--login), bash reads your ~/.profile at startup (among other files) and thus everything sourced from there.
When invoked with -i (--interactive), bash reads your ~/.bashrc at startup (among other files) and thus everything sourced from there.
Try $ man bash or :h shell and :h shellcmdflag for more info.
As for the differences between login and non-login shell I must admit my ignorance. This answer on serverfault may help, it's interesting, anyway.
Append the following two lines to your .vimrc.
set shell=/bin/bash " use bash
set shellcmdflag="-ic" " flag passed to the shell to execute "!" and ":!" commands
P.S. It seems that the accepted answer may cause some problems when editing '.bash' files or execute a 'git commit', but I don't know why.

Using VIM in read-only mode as the viewer in cscope

I tried setting the environment variables VIEWER/EDITOR to: '/usr/bin/vim -R', but, I got the error:
Cannot exec vim -R: No such file or directory
Is there a flag in cscope to open up files in read-only mode? Or, can the EDITOR/VIEWER be set to VIM in read-only mode? Thanks in advance!
this is just a problem with whitespace.
The easiest solution is to use view which is usually installed with vim.
It is the same as vim -R. Other solutions are just creating simple scripts in your path
that simply call vim -R.
Create a file view.sh with
#!/bin/sh
vim -R $#
then chmod +x view.sh and after that you can use this also.
The final option would be to try 'usr/bin/vim -R' without the quotes.
Just type 'export CSCOPE_EDITOR=view' in gnome-terminal and then do cscope -R : It will open cscope in read-only mode

Resources