Avoid dual maintaining vimrc and vsvimrc files - vim

I just discovered keithn/vsvimguide for using VsVim with Resharper and added some useful key-mappings that use Resharper's functionality to a new _vsvimrc file I created.
Unfortunately, all my settings in my _vimrc which I use for gVim/vim elsewhere are no longer loaded. Is there a good way to not have to dual maintain my settings in two files? Perhaps a conditional I can use to check if I'm running inside Visual Studio? I'm assuming you can't load two settings files in VsVim.

glad you found it useful, I keep meaning to update it as I've changed around a bunch of how I use vsvim and resharper.
But to your question, you can load files with source which is a Vim standard way of loading files (you can :source <file> ) but in your vs vim file just put
source <path to vimrc>
you can source any other files you like into your .vsvimrc you can do the same in your actual vim config as well, so you could break out certain things into a "common" file if you wanted as well

Related

How are MacVim's settings related to .swp files unique from other versions of Vim and how can I override them?

I have been using Vim casually for around 3 years (mostly for git commit messages) and recently started using it exclusively for text editing and coding while I work my way through a series of intermediate tutorials and resources. Up until a few weeks ago, I primarily used either Vim, NeoVim or iVim (on iOS).
Recently I installed MacVim and started using it exclusively when I'm working in iTerm on a Mac. I have noticed some differences in the way .swp files are created and managed. In the other versions of Vim that I have used, .swp files are only created when I have a file open in more than one instance of Vim at the same time. It seems that MacVim creates .swp files for every open file (I'm guessing for backup/restore purposes). MacVim also seems to put .swp files into the working directory. I don't recall other versions of Vim doing this but it recently led me to add *.swp to my global Git ignore settings.
Before drafting this question I did a quick search for: vim macvim swp files and found one result that gave me a few ideas on how to work around one of the issues that I've noticed:
Vim Swap Files Not Deleting
I also found this post that gives the impression that the following settings are involved:
backup / nobackup
writebackup / nowritebackup
swapfile /noswapfile
But this doesn't really answer my question, which is "What is different?". I am editing my .vimrc regularly and would like to add in the appropriate settings to get the same behavior in MacVim by default (while also understanding what I am adding). What is unique about the way MacVim is setup related to swap files? Is there a specific combination of the three settings mentioned above or are more settings involved? How can I set up the same default behavior I have noticed in Vim, NeoVim, and iVim?
I have read the MacVim FAQ and Troubleshooting Guide but didn't find any relevant information.
The default location for a swap file is determined by :help 'directory'. Given the default value, Vim will try the directory of the file for which the swap file is created first, appending the .swp extension. If it can't create the swap file there, it will try the next location in 'directory':
The file:
_posts/2018-07-31-npm-201.markdown
The swap-file:
_posts/2018-07-31-npm-201.markdown.swp
If a swap file already exists for a file you are trying to edit and you decide to edit it anyway, another swap file is created at the same location, with a different extension: .swo, .swn, etc.
What I described above is the normal, expected, behaviour. And that's how MacVim works.
In the other versions of Vim that I have used, .swp files are only created when I have a file open in more than one instance of Vim at the same time.
Those "versions of Vim" are either:
broken,
weirdly configured,
not Vim.
It seems that MacVim creates .swp files for every open file (I'm guessing for backup/restore purposes).
You guessed right, and yes, that's the expected behaviour in every Vim.
MacVim also seems to put .swp files into the working directory.
If the file is in the working directory it's normal. If it's not, the working directory may be part of 'directory'. If it's not, you have found a bug.
I don't recall other versions of Vim doing this but it recently led me to add *.swp to my global Git ignore settings.
It's very common to have a Vim section in there.

Location-dependent vim settings

I work on multiple software projects that have different style conventions. Is it possible for vim to automatically change its settings (e.g. indentation) depending on the directory a particular file lives in?
No
Vim doesn't have any settings appropriate for what's described.
But there's a plugin for that
However, you may find a plugin that does what you want for example vim-independence:
The plugin is automatic, if the file .vimrc exists in your git project root - it will load it.
(full disclosure: I wrote that script)
you can create some file with settings in each project. .vim_setting for example. and append source .vim_settings to your .vimrc. if you run vim from project directory, settings will be picked up

Which plugins make assumptions about current directory

I'm thinking about setting the following in vimrc
set autochdir
In vim.wikia.com it states that
...Unfortunately, when this option is set some plugins may not work
correctly if they make assumptions about the current directory....
Am I better just changing the working directory manually when required? Which plugins will not work properly when using autochdir?
I've been using :set autochdir since I've started with Vim in 2002, I use a lot of plugins, but I haven't experienced many problems. (And those problems were rather easy to fix; I think I've sent some such patches to plugin authors.)
In the end, it greatly depends on the individual plugins. Don't be afraid and just try it; it's trivial to switch back, anyway. Look out for problems with plugins that create new buffers or access / navigate to other buffers, especially when those are distributed over different directories. Problems ususally manifest themselves into the wrong buffer (or an empty one in the wrong directory) being opened.

Opening the header file to a C/C++ source file with vim from multiple directories and multiple extension

First off, I was about to write a long list of if/else statements in vim and realized that 1) there was a better way to do what I was trying to do and 2) SO would be ripe with help on the subject. So! I have a variety of files spread about like
foo/src/file01.C
foo/src/file02.cc
foo/src/file03.c
foo/include/file01.hh
foo/include/file02.h
foo/include/file03.h
If you notice that the C/H, cc/hh, c/h extension may or may not match then you are keen and I'd like you to please help. I've look at things like the following vim scripts from the Vim wiki for "Easily switch between source and header file" and although I only dumped a few hours into a.vim without success, it doesn't seem that the others would work via the docs on that page. So can anyone help out on how to make this work?
A good lead I had was a quick How to Easily Switch between Header and Source topic, but still couldn't make it work.
I guess what I really want is how to avoid the multiple if statements and use real matching to do what I want. I want to look into another directory and if look for a header file of the same name with any familiar extension if it was a source C/C++ file, or look for a source file of any regular extension if it was a header file. Thanks for your help!
UPDATE: I specifically want to open the file in a new tab. I live on vim tabs!
I recommend using the FSwitch plugin. https://github.com/derekwyatt/vim-fswitch
This does exactly what you need out of the box. It is better than a.vim in more than one way, being a rewrite of the idea behind a.vim.
The link you posted presents it as a solution, too.
I have just installed it to my vim configuration and it does its job well.
Enjoy.
Just to make sure I was using the most current version, I downloaded the latest a.vim script (2.18) and copied it into my ~/.vim/plugin directory.
You can define certain variables in your ~/.vimrc file to get a.vim to recognize alternate file extensions.
To get the files in your example to match their alternates I added the following to my ~/.vimrc:
let g:alternateExtensions_C = "H,hh"
let g:alternateExtensions_hh = "C"
These are global variables that allow you to override what's already defined. You'll have to define
both relationships (they don't work both ways).
You can see what the current mappings are by typing:
:echo g:alternateExtensionsDict
If you need to define other mappings, follow the same pattern. What comes after the underscore is the file extension you're editing. What's in the double quotes is a comma-separated list of alternate extensions.
let g:alternateExtensions_<ext> = "<list,of,alt,ext>"
If you have a different directory structure, you can define what paths to search by overriding the g:alternateSearchPath variable. ../src and ../include are already included by default.
:echo g:alternateSearchPath
To open the alternate file in a new tab:
:AT
By the way, the a.vim script is pretty well documented. You might want to open it up and take a look. I found a setting or two that I didn't know about and I've been using it for years ;o)
I hope that helps.
IMO your best option is to adopt existing scripts to use :tabf instead of :e or whatever the scripts use right now to open the counterpart file. You can also try to make the change configurable and submit it to the script author. (I'm pretty sure many would find the enhancement useful.)
That reminded me of a trick I used very long time ago. Instead of guessing where the corresponding source/header files are, I have used at the top of file special comment containing relative path to the counterpart file. Switching was as simple as finding the special comment, extracting file name and opening it. Problem was similar to yours in that file extensions were not predictable. My rational solution was to stop guessing and denote counterparts explicitly in the source code. (This days I would have probably tried to put the relationship table into an external file with a standard name and look it up in VIM using the upward search.)
Two helpful things
:he 'path'
:he tabfind
So you would do
:set path=../,/usr/include/,/home/buildagent/SDKROOT/mysdk/inc
:tabfind error_codes.h
to open error_codes.h from somewhere exotic without having to specify it. Note how vim globbing is very very flexible, so you might not need mucht
:argadd ./**/*.[h,H] | tab sall
will open all header files under the current directory, regardless of how many levels deep. Be careful running this command on a large tree or with symlinks outside the tree

A few vim questions

So I was hoping that some old school Vim'ers could help me out. These are all separate questions and normally I would put them up each on their own but I'm not sure if that qualifies as question whoring here.
Plus I think if you know enough to be asking any of these questions they will all be coming up in the near future:
I have a library I'm writing and a series of applications that use that library. There doesn't seem to be an easy way(from what I can tell) to build a ctags file for the library and build one for each of my applications and make sure one references the other when I'm in vim.
Using gf to open files from command mode is awesome, but a lot of my include files
don't contain the full path. They refer to an include directory I set in the IDE. How can I set this directory as another point for Vim to start looking for files?
Is there a way to compile a file inside Vim and send the output to a buffer? I'm currently using MSVS 2k3 but I'll be porting over to Linux in a few weeks so if this is possible on either system I'd appreciate it.
Re 3)
If you put a makefile in your root dir, you can simply write
:make
This will run make and (iirc) put any errors into a seperate buffer, and make vim goto the first compile error. From there you can navigate all erroring lines using :next-error
Also, see this page
http://wiki.beyondunreal.com/Legacy:Vim
and
http://linux.byexamples.com/archives/287/perform-grep-and-make-in-vim/
for details on how to show the result in a seperate console.
1- tags files are independent, and can be used together. See :h 'tags'
I can't tell what is the easy way to build tags files. I have one that consists in using two plugins of mine:
one (draft) plugin that knows how to update C++ tags files (it should be easy to adapt it to other filetypes),
and another (local_vimrc) that helps me define directories-local .vimrc. Thus for any files within a given directory hierarchy, I can adapt the &tags options to use the relevant tag files, and the current tag file that will be rebuilt automatically (or when a keybinding is triggered). (Plugins like project should do the trick as well)
2- :h 'path'
3- :h :make
HTH.
2)
:cd {path}
For help:
:he cd
A few others like :lcd might be better suited. Just scroll down that help page.
This is rather off topic, but might still be useful: if you're using Visual Studio a lot and like Vim, you might want to look at ViEmu. It's the best Vim-emulation for any IDE I've yet seen, and the cost is really low. :) And no, I'm not getting a commission. :P
It's not obvious, but if you open a directory instead of a file, it's nicely browseable.
e.g.
:e . (colon-e-dot)
:e .. (colon-e-dot-dot)
will let you browse from your current directory or its parent.
(understanding that you were probably hoping for a capability to have vim accept e.g.
:e abc.txt
and have it look in several directories, which I don't know how to do.)

Resources