In a .vimrc, is `set nocompatible` completely useless? - vim

Several users in this epic question put the following in the .vimrc:
" Necesary for lots of cool vim things
set nocompatible
But is it really necessary? From the docs:
'compatible' 'cp'
boolean (default on, off when a |vimrc| or |gvimrc| file is found)
If set nocompatible is going in a .vimrc, that means that a .vimrc file exists, seemingly making it pointless.

If it is the system-wide vimrc, this option won't be off. So, if you're changing the system-wide vimrc and you want it, you need to set it.
From the documentation section *compatible-default* (emphasis mine):
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'.
Another difference is that explicitly setting 'nocompatible' overrules calling vim with the -C flag.
In any other scenario, yes, setting 'nocompatible' in your vimrc is a noop.
In the end I think it's just a matter of "better safe than sorry".

Many people share their .vimrc files on GitHub and I sometimes will test out settings without replacing my .vimrc file. vim allows me to do this with the -u flag.
vim -u test_vimrc
From vim ":help nocompatible"
(Note: This doesn't happen for the system-wide vimrc or gvimrc file,
nor for a file given with the |-u| argument).
This means that if you share your .vimrc with someone and they use -u flag to load your file, vim won't be configured the same as if the file were named .vimrc and located in your home directory.

I was using vim in Cygwin on a Windows VM and every time I was in Insert Mode, pressing arrow keys would result in vim printing "A", "B", "C" or "D" on the screen instead of scrolling. I found a forum that said putting vim in nocompatible mode would fix it. Thankfully, it did.
I put "set nocompatible" in my ~/.vimrc file and the problem remains gone. So perhaps it's not 100% useless.

Based on what Johnny pointed out above, I was simply astonished when I just found THIS out:
$ cat /usr/share/vim/vimrc.tiny
" Debian system-wide default configuration Vim
set runtimepath=~/.vim,/var/lib/vim/addons,/usr/share/vim/vimfiles,/usr/share/vim/vim74,/usr/share/vim/vimfiles/after,/var/lib/vim/addons/after,~/.vim/after
set compatible
ARGH!!!
No I did NOT expect that.
Debian (or Debian Unstable aka Ubuntu) indeed DOES put up a nightmare to their users by overriding the default setting by set compatible.
I hope that you will now know why when you're coming from FreeBSD, the first thing you would have to do is override the system-wide setting by putting a set nocompatible into your own ~/.vimrc. Because otherwise you'd just produce letters instead of being able to move the cursor the way you've been used to.
I think this is a horrid idea. In other words, this set compatible line ought to be removed from the system-wide vimrc.tiny in both Debian and Ubuntu, because it will annoy new users who are not (yet) as smart as knowing how to get the cursor keys working.
It's things like these that force them to nano and others because of such entirely pointless blockers!
I would really want to talk with the dude who once propagated this change to the system-wide resource file in Debian. And maybe also to the people who acknowledged his change to the fullest.
Johnny is right: on your private PC, you may remove said line from the system-wide .vimrc (if there), and touch an empty .vimrc on your $HOME. Thanks so much for pointing that out, way less clutter again. Note that you MUST have that ~/.vimrc (even if empty!) as otherwise you will not be able to use the cursors without explicitly putting in set nocompatible.

got the same problem on using -u vimrc option, even doing set nocompatible was misbehaving with some of my older setting, after some search i found one compactible version that works for me:
" Avoid side-effects when nocompatible has already been set.
if &compatible
set nocompatible
endif
this avoid re-setting of nocompatible flag when it is already set.
vim :help nocompatible shows conditions when it set/resets the options value, scroll to that table of options and values, for quick reference, conditions that effect this behaviour are:
Option is set to the value given in {set value} when 'compatible' is
set.
Option is set to the value given in {set value} when 'compatible' is set AND is set to its Vim default value when 'compatible' is
unset.
Option is NOT changed when setting 'compatible' but IS set to its Vim default when 'compatible' is unset.

Related

Vim weird behaviour with backspace with empty .vimrc

I'm having a weird issue with vim on Ubuntu. I've been using it for the last few weeks, trying to learn, on Windows and it behaves differently now that I'm using it on Linux.
I noticed that while in insert mode pressing backspace will delete text just like any other editor on Windows, but on Linux the text is "deleted" yet it stays there until I press ESC or write over it.
I was trying to fix this but I'm confused as to whether this is intended behaviour or not. It happens in gvim too.
The reason of this question is this, however:
I deleted my .vimrc file to see if any of my config was at fault and it fixed it. Backspace was now back to its regular self.
But then I tried creating an empty .vimrc file and that made it go back to the delayed delete. It's empty. Why the hell?
So I have no idea what's causing this. Hope my question makes sense my English ain't the best. Thanks.
Alright so looking at :h compatible I found this:
"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). Also see |compatible-default| and
|posix-compliance|."
So if I'm getting this right, running Vim with a .vimrc file should automatically set nocompatible and running it without one should set compatible... ? Whatever the case, I tried checking with :verbose set compatible? and it always says nocompatible is on so the -N flag shouldn't do anything... Yet it fixes the issue.
Without a vimrc Vim will load /usr/share/vim/vim80/defaults.vim (depending on your vim version). In this file the bs/backspace parameter is set to 2, or actually it is indent,eol,start which is the same as 2 (see :h bs)
Now if you create an empty .vimrc, defaults.vim will not be loaded, so your bs will possibly be 0.
This behaviour is described in :h defaults.vim
So to solve your problem, just put set bs=2 in your .vimrc
Alright I fixed it.
Running vim with the -N command makes it work properly. I'm not sure why but that's what's happening.

One of my files has an incorrect tabstop setting

One of my files, whenever I open it, has a different (incorrect) value for the 'tabstop' option. In my .vimrc, I have the line:
set tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab
and 'tab' is not mentioned in any subsequent lines. When I open a specific file I'm working on, the 'tabstop' option is set to 8, while all the other relevant options are correct; and all other files (so far) show their indentation correctly. I don't use modelines or smart tabs (yet). If I source my vimrc in the file, it corrects the indentation, so I'm assuming it isn't anything directly related to vimrc.
What is happening?
Basically there's two ways in which your .vimrc can be superseeded: plugins (including filetype-specific ones) might reset the option, or it might be changed by an autocommand. In any case, your first step is to check when the option was last modified (see :set-verbose):
:verbose set tabstop?
If that's not enough, look through the list of all the scripts and config files that Vim has read since it was started:
:scriptnames
(better do that in a newly started copy of Vim right after loading the offending file—this way, there'll be less output from :scriptnames.)
After these steps, you should have a list of scripts that might be the culprits. There's no easy way to narrow it down: you'll have to grep, to temporarily remove plugins from your ~/.vim and so on.
Finally you'll find a line that changes the setting. If it's in a plugin, look through its documentation to find a way to override the setting. If it's somewhere in the indent file or ftplugin, override its effect by placing a file into ~/.vim/after/indent/ or ~/.vim/after/ftplugin. An autocommand might also work.

set tags=tags in gvimrc not working, unless set it explicitly

I have a problem to set my tags file correctly. It use to work without problem after I reinstalled the system. error message like
E433: No tags file
E426: tag not found: Pids
accurs when I press ctrl+].
I have this line in my .gvimrc file
set tags=~/projectdirectory/tags
and tags-exuberant installed properly.
It works fine when I type :set tags=~/projectdirectory/tags in gvim
I also tried use set tags=~/projectdirectory/tags;/
All other .gvimrc settings function well. How this could be possible?
UPDATE:
I have solved the problem, it is because I have multiple tags setting in ~/.gvimrc, vim take the last one in current session.
You can check the actual effective value (after starting GVIM) via
:verbose set tags?
The option might have been overwritten by a later :set command, or a plugin.
Even if you only use GVIM, it's recommended to put the general settings into ~/.vimrc (which is also sourced in GVIM), and keep ~/.gvimrc reserved for GUI-specific settings. An important difference between the two is that the latter is only sourced at the very end, so it's unsuitable for configuring plugins.

Horizontal line in vim

What does a horizontal line in vim mean? While editing a remote file I see a horizontal line in the current line.
I don't see it while editing local files
Edit:
cursorline is not shown until I save the file(:w). When I type :w and enter password, cursorline appears. Why does it have such a behavior? When I edit file in remote machine cursorline is turned off and is not shown.
As others have answered, the effect is probably being caused by the cursorline option.
You can track down what is script made the most recent change to an option by running the command set optname? under the verbose command:
:verbose set cursorline?
You will probably just find that the Netrw plugin set it; Netrw handles local directory browsing and remote directory/file access like your scp:// example. Netrw adjusts cursorline (and cursorcolumn) for its own purposes (e.g. directory listings), but it tries to restore the value to the “user value”. Unfortunately, its idea of the “user value” is captured when part of the Netrw code loads and is not updated afterwards.
My guess is that, somehow (via some other plugin, or bit of configuration), cursorline is set when Netrw loads (and captures its value), but it is later reset by the time you start editing the first file. Then, when you later save the file (:w), Netrw restores the “captured” value. Unfortunately, there does not appear to be any good way to update this “captured” value of the cursorline option (there is no “external” access to the script variable it uses, and it does not “recapture” if you manually reload the file).
What you can do, however, is explicitly load the bit of Netrw that “captures” cursorline when your desired value is active. You could do that with the following two commands early in your ~/.vimrc (possibly at the very top, if necessary—it needs to be before the first time autoload/netrw.vim would ever be used):
set nocursorline
runtime autoload/netrw.vim " will 'capture' cursorline and cursorcolumn values
Netrw will still set/reset cursorline (and cursorcolumn), but as long as the value you normally want matches the value that is active right before Netrw is loaded, then you will not notice it.
I don't quite know the simplest solution to get get netrw to really capture the right value, but at very least (since for me, nocul is right,) adding:
let g:netrw_cursorline=0
To the end of my ~/.vimrc seems to have fixed the problem for me. Hope this helps someone!
It's the cursorLine. Its appearance is defined in your colorscheme. The one on the remote machine is probably different than yours or there's a mismatch between your client/server's $TERM.
Yup, ":set cursorline" or ":set nocursorline" to turn the line on or off.
The command "vim scp://...." copies the remote file onto your local machine (i.e. machine where the "vim" process runs), then opens the file in "vim" for you to edit, then, if you have modified the file, copies the file back onto the remote system. As such, syntax highlighting etc. is determined only by "vim" on your local machine.
Files with the same syntax type (":se syn" to show current syntax highlighting scheme) are highlighted the same way. Do the files, in which you see the difference, have the same syntax type?
Version 142 of netrw.vim has fixed this bug, at least for me (vim 7.3, old netrw.vim version was 140, running under cygwin).
You can get the latest version here: http://www.vim.org/scripts/script.php?script_id=1075

Quickest Way to Revert Spaces to TABs in VIM

I have always been one to replace TABs in VIM with x amount of spaces (usually 4).
Four lines I almost always use in my .vimrc config file are:
set tabstop=4
set shiftwidth=4
set expandtab
syntax on
Basically, there are moments when I NEED to use a single TAB (such as Makefiles), and I don't know how else to work around that other than leaving the file, editing my .vimrc, and then reloading the file of interest.
That said, what is the quickest way (from within VIM) to revert it back to using TABS, and then reverting back to my original settings? I'm looking for the least-hassle, least-keystrokes solution.
VIM will automatically enable the TAB for a makefile, assuming you name it "makefile," as opposed to "Makefile." Not sure why VIM still doesn't detect the type with a lower-uppercase difference, but such is life. (#Sedrik)
That aside, other alternative solutions are:
Filetype Binding (#ThorstenS #tungd):
autocmd FileType make setlocal noexpandtab
RealTime Switch (#ThorstenS):
Assuming the .vimrc configuration mentioned in the question, do:
:set noet (to switch from spaces to TAB)
and :set et (to switch back)
Just use the magical escape key available in insert mode.
On the *NIX's it is ^V by default when you are in insert mode. On Windows, you need to find out what the magical escape character is - ^V is taken for something else; I think it may be ^Q or ^S?
So! In your makefile:
this: this.c
<C-V><Tab>cc this.c
where the usual meanings apply:
means hit ctrl-V (you should see a ^ hiding away under the cursor)
- hit the tab key. Bingo.
Works for me.
Note: if you use vim settings or startup code which smashes tabs as you read a file, this obviously is a short-term fix. I prefer to learn how to use the retab command to ensure a file is tab-clean, because I don't like a file to be touched unless I consciously choose to do so.
Just type set noexpandtab . Perhaps you bind this to a function key.
Only this configuration helped me solve this problem.
filetype plugin indent on
filetype detect
autocmd FileType make set noexpandtab
Vim defaults to tabstop=8 and noexpandtab, so the defaults are well suited to working with Makefiles. If your .vimrc uses custom options for tabstop, expandtab, etc., one simple solution is to bypass your .vimrc while working with Makefiles.
From the manpage (emphasis mine):
-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.
Since I can never remember the necessary flag/value/formatting, I've created a Bash alias which remembers for me: alias vimnone='vim -u NONE'
You can create a custom configuration per file type.
mkdir -p ~/.vim/after/indent
echo 'set noexpandtab' >> ~/.vim/after/indent/make.vim
Basically, here we created a new indent configuration for makefiles that will be loaded after other scripts.
source

Resources