Options for .vimrc from command-line arguments? - vim

I have a custom .vimrc file which I use across many different machines.
Some machines are less powerful than others, and it would be nice to be able to load a "stripped-down" version of the .vimrc file. However, I'd like to maintain a single .vimrc to avoid fragmentation.
Ideally, I'd like to pass arguments to my .vimrc from the command line. If the "minimal" option is selected, the .vimrc would skip over loading the most resource-intensive plugins.
Does anyone know the best/cleanest way to do this?
Thanks!
Edit: The slower machine I'm talking about is a Raspberry Pi over SSH. Vim itself isn't slow, although I have several plugins including NERDTree and Syntastic that take lots of time to load on the Pi's limited CPU. By taking out most plugins for my "minimal" configuration, I cut down the loading time from 3.5 seconds to 0.5 seconds.

You can use the --cmd switch to execute a command before any VIMRC is loaded.
So on your less powerful machines you could alias vim to something like vim --cmd 'let weak=1' and then in your $VIMRC you can say:
if exists('weak')
echo "poor machine"
endif

take a look at source:
source /foo/bar/another_vimrc
Your 'heavy' vimrc can just source the basic vimrc and add what you want. This is also really handy for project/machine specific abbreviations, ctags, etc.

This will not keep a single vimrc file, but for the sake of others who have the same question (as stated at the top of the page):
$ vim -u ~/.vim-min.vim
Note that this will suppress loading both the system vimrc file (if any) and your personal vimrc file.
:help -u
:help startup
(See Step 3 of the second reference.)

From the excellent answers above, I got this working:
~/.vimrc :
if exists('FLAG') " 'FLAG' passed from ~/.bashrc 'vimm' alias
set textwidth=150 " (launches vim in expanded terminal window)
set lines=58
else
set textwidth=79
set lines=40
endif
~/.bashrc :
# Needed to combine following two lines for ~/.vimrc use:
# alias vimm='konsole --geometry 1900x1040+0+0 -e "bash -c \"vim\""; exit'
# vim --cmd 'let FLAG=1'
str="'let FLAG=1'"
alias vimm='konsole --geometry 1900x1040+0+0 -e "bash -c \"vim --cmd $str\""; exit'
Now, 'vim' (normal use) launches Vim in a normal-sized terminal, whereas 'vimm' launches Vim in a larger terminal (alternate settings).
'Konsole' is the terminal that I use in Arch Linux.

Related

Override a builtin command with cabbrev

I'm trying to override 'w' in vim so it would call an external program and filter the buffer instead of writing to a file. There are very good examples across the internet about how to do that. I tried one from vim.wikia.com, but vim always complains with E488: Trailing characters. This is the command in my vimrc:
cabbrev w <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'W' : 'w')<CR>
I'm not very familiar with vim script. I tried removing <CR> from the end of the line with no luck.
UPDATE
Since I want to run vim as customized as possible I run it with the -u flag. I noticed that vim behaves differently when using that flag compared to running it without it
With the -u flag the expanded abbreviation is what needs to be evaluated as code.
Without the flag, the abbreviation is what it is intended to be (here I enter the cabbrev rule from vim's prompt)
Regarding the -u flag vim's man page says this:
-u {vimrc} Use the commands in the file {vimrc} for
initializations. All the other initializations
are skipped. Use this to edit a special kind of files. It can also be used to skip all
initializations by giving the name "NONE". See ":help initialization" within vim for more
details.
Apparently when this flag is used the initialization from
vim /etc/vimrc is not performed and there I found this option:
set nocompatible
vim's help about compatible option:
This option has the effect of making Vim either more Vi-compatible, or
make Vim behave in a more useful way. This is a special kind of
option, because when it's set or reset, other options are also changed
as a side effect. CAREFUL: Setting or resetting this option can have
a lot of unexpected effects: Mappings are interpreted in another way,
undo behaves differently, etc. If you set this option in your vimrc
file, you should probably put it at the very start.
...
When a vimrc or gvimrc file is found while Vim is starting up,
this option is switched off, and all options that have not been
modified will be set to the Vim defaults. Effectively, this means
that when a vimrc or gvimrc file exists, Vim will use the Vim
defaults, otherwise it will use the Vi defaults. (Note: This doesn't
happen for the system-wide vimrc or gvimrc file, nor for a file given
with the -u argument).
set nocompatible makes the cabbrev syntax from the question work

Make .vimrc changes being picked up by Vim only and for me only

I have a $HOME/.vimrc file which I have configured for many options like below but not limited to.
set foldmethod=indent
set foldnestmax=10
set mouse=a
set number
These all work fine if I open a file with vim however if I open with vi or view the file none of the config work, which is fine but I get errors for some.
line 104:
E538: No mouse support: mouse=a
line 205:
E518: Unknown option: foldmethod=indent
Press ENTER or type command to continue
After searching quite a while I was able to remove some of these.
if has('mouse')
set mouse=a
endif
silent! set foldmethod=indent
However this is just suppressing them. And the linenumbers are still being displayed with vi or view which look pretty bad and many people login to the box will be suddenly mystified by the yellow line numbers.
How to make vi not take up these config.
I powerbroker to the linux box. Can I make .vimrc setting just for me.
vi and vim are the same executable.
I'd suggest you change the name of your vimrc:
$ mv ~/.vimrc ~/.myvimrc
and start Vim with:
$ vim -Nu ~/.myvimrc
You could add an alias to ~/.bashrc (or whatever works in that system) to ease your workflow:
alias myvim='/usr/bin/vim -Nu ~/.myvimrc'
As vi does not support all options of vim, What I do is to set alias for vi to vim in my rc file
alias vim='vim -p'
alias vi='vim -p'
The -p is not really required. but I kinda like tabbing enabled by default on vim. So every time, if you hit vi or vim, it behaves the same.
You can make this permanent, if you like, by adding the lines to ~/.bashrc, assuming you are using bash. You need to source ~/.bashrc to take immediate effect without logging off.

Ctags path in vim is different from the shell

I was trying to generate the ctags from vim. I use Exuberant ctags.
So the problem is that when I do :!ctags or :call system('ctags') from vim it does not work because it uses my default ctags and not the exuberant ctags.
I had similar problem in my shell which I overcame by specifying the path of exuberant first
like export PATH=/usr/bin/local/:$PATH
So here is the really weird part
When I do echo $PATH in vim it shows the correct path. But When I do :call system('which ctags')
it shows me /usr/bin/ctags and not /usr/bin/local/ctags.
I can't understand what is going on??
Though I can overcome this problem by call system('/usr/bin/local/ctags') but I was just wondering if there is something better out there.
EDIT:
I use OSX 10.9.3
:set shell? -> shell=/bin/zsh
set shellcmdflag -> shellcmdflag=-c
and I set my path in zshrc file as export PATH="/usr/local/bin:usr/local:$PATH
When you do :!command or :call system('command') Vim starts a new subshell according to the values of 'shell' and a bunch of other options listed under :help 'shell'. The 'shellcmdflag' option is important because it usually tells your shell how to start (interactive or not, login or not) which usually has an impact on what *rc files are sourced and thus if your environment variables are seen or not.
Please, update your question with these informations:
your OS
:set shell?
:set shellcmdflag?
in what *rc file did you change your PATH?

How to set default vim colorscheme

The latest upgrade of Ubuntu made my vim colorscheme unusable. I know how to set it manually (:colo evening, for example), but I want to set the default for all vim sessions. I see reference in other places to .vimrc, but the right location and syntax have eluded me thus far.
Put a colorscheme directive in your .vimrc file, for example:
colorscheme morning
See here: http://vim.wikia.com/wiki/Change_the_color_scheme
Your .vimrc file goes in your $HOME directory. In *nix, cd ~; vim .vimrc. The commands in the .vimrc are the same as you type in ex-mode in vim, only without the leading colon, so colo evening would suffice. Comments in the .vimrc are indicated with a leading double-quote.
To see an example vimrc, open $VIMRUNTIME/vimrc_example.vim from within vim
:e $VIMRUNTIME/vimrc_example.vim
It's as simple as adding a line to your ~/.vimrc:
colorscheme color_scheme_name
You can try too to put this into your ~/.vimrc file:
colorscheme Solarized
What was asked for was to set:
the 'default', not some other color profile, and
'for all vim sessions', not simply for the current user.
The default colorscheme, "for all vim sessions", is not set simply by adding a line to your ~/.vimrc, as all of the other answers here say, nor is the default set without the word 'default' being there.
So all of the other answers here, so far, get both of these wrong. (lol, how did that happen?)
The correct answer is:
Add a line to your system vim setup file in /etc/vim/ that says
colorscheme default
or using the abbreviation
colo default
but not capitalized as
colo Default
(I suggest using the full, un-abbreviated term 'colorscheme', so that when you look at this years later you'll be able to more easily figure out what that darn thing does. I would also put a comment above it like "Use default colors for vim".)
To append that correctly, first look at your /etc/vim/vimrc file.
At the bottom of mine, I see these lines which include /etc/vim/vimrc.local:
" Source a global configuration file if available
if filereadable("/etc/vim/vimrc.local")
source /etc/vim/vimrc.local
endif
So you can append this line to either of these two files.
I think the best solution is to append your line to /etc/vim/vimrc.local like this:
colorscheme default
You can easily do that in bash with this line:
$ echo -e "\"Use default colors for vim:\ncolorscheme default" \
| sudo tee -a /etc/vim/vimrc.local
#
# NOTE: This doesn't work:
#
# $ sudo echo 'colorscheme default' >> /etc/vim/vimrc.local
#
# It's the same general idea, and simpler, but because sudo doesn't
# know how to handle pipes, it fails with a `Permission denied` error.
Also check that you have permission to globally read this file:
sudo chmod 644 /etc/vim/vimrc.local
With $ tail /etc/vim/vimrc.local you should now see these lines:
"Use default colors for vim:
colorscheme default
You can just use the one-liner
echo colorscheme koehler >> ~/.vimrc
and replace koehler with any other available colorscheme. Imho, all of them are better than default.
Once you’ve decided to change vim color scheme that you like, you’ll need to configure vim configuration file ~/.vimrc.
For e.g. to use the elflord color scheme just add these lines to your ~/.vimrc file:
colo elflord
For other names of color schemes you can look in /usr/share/vim/vimNN/colors
where NN - version of VIM.
Ubuntu 17.10 default doesn't have the ~/.vimrc file, we need create it and put the setting colorscheme color_scheme_name in it.
By the way, colorscheme desert is good scheme to choose.
Copy downloaded color schemes to ~/.vim/colors/Your_Color_Scheme.
Then write
colo Your_Color_Scheme
or
colorscheme Your_Color_Scheme
into your ~/.vimrc.
See this link for holokai
OS: Redhat enterprise edition
colo schema_name works fine if you are facing problems with colorscheme.

How to turn-off a plugin in Vim temporarily?

I have multiple plugins in Vim and some of them modify the default behavior of Vim. For example I use Vimacs plugin, which makes Vim behave like emacs in the insert mode alone. Sometime I want to turn off the Vimacs plugin without moving the vimacs.vim out of the plugins directory. Is there a way to do it?
You can do this if you use a plugin manager like Vundle or Pathogen, which will keep the plugin in its own directory underneath the ~/.vim/bundle/ directory.
In that case, just find out the runtimepath of the vimacs plugin with the following command:
set runtimepath?
Let's say it's ~/.vim/bundle/vimacs.
Then, put this command in your .vimrc:
set runtimepath-=~/.vim/bundle/vimacs
To load vimacs, just comment that line out and relaunch Vim (or source your .vimrc).
See which variable vimacs check on start. On the begin of the script file find something Like if exists('g:vimacs_is_loaded").... Then set this variable in your .vimrc or while start vim with vim --cmd "let g:vimacs_is_loaded = 1".
In case you are using pathogen, this post gives a better answer, in my opinion. Since I have frequent need to disable snippets when using latex, also added this in my ~/.config/ranger/rc.conf:
map bs shell vim --cmd "let g:pathogen_blacklist = [ 'ultisnips', 'vim-snipmate' ]" %f
This way, whenever I want to open a file with snippets disabled, it is easy.

Resources