How to change the default working directory in vim permanently (on windows 10)? - vim

Although this question is similar, answer given over there changes the working directory only temporarily i.e. whenever I close and reopen gVim and write a new file given just the filename, it saves it in the home directory.
Here is another similar question, but I didn't get what the answer is trying to say. I found a _vimrc file at C:\Program Files (x86)\Vim. But I don't know what to do with it.
I want to achieve this:
Whenever I open vim and write a new file, given only the filename, it should save it in C:\Users\Sourav Kannantha B\Documents\Vim Projects instead of C:\Users\Sourav Kannantha B
I just downloaded gVim today from their website. So I am new to this.

Related

How does File Explorer open files?

It's maybe stupid question, but what happens behind the scenes when i double click the word app, or .exe app?
For 'Non-Developer' it just opens the file in right environment after doubleclicking. But I would like to know, how is it done, how can the file manager know what to open? (.docx in word, .txt in texteditor,etc...)
+ I would like to know how can I do that in Node.js, is it the best way to use child_process and if statements for every suffixes?
There is a file association to tell Windows how to treat a certain file extension. You can type assoc in a Windows Command Prompt to see them. As an example:
C:\test>assoc .txt
.txt=txtfile
So Windows knows now, that the file with the extension .txt is a txtfile.
ftype defines, how that filetype is to be handled:
C:\test>ftype txtfile
txtfile=%SystemRoot%\system32\NOTEPAD.EXE %1
So whenever you doubleclick on a file, Windows checks if it is associated with any filetype. Then it looks up, how to handle that filetype and executes that command (in the example above, it opens Notepad with the filename as a parameter).
Both assoc and ftype are able to change the settings (for example to open .txt files with another editor). But if you try that, do yourself a favor and note the original settings, so you are able to revert your changes when needed.
The changes can also be done directly in the registry (not recommended, when you are not experienced in handling the registry)
For Node.js, you can use child_process to shell out to the start command which does similar things as the File Explorer.
E.g. start some/path/to/file.docx will open that file in the default program associated with the format.

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.

_vimrc getting renamed to _vimrc.2014

I noticed that for whatever reason, my _vimrc wasn't being loaded this morning. I keep my entire vim directory saved to my Google Drive to the location I specify in my _vimrc. I didn't worry about it since I'd recently backed it up, but now when I dump it into my vim folder, whenever I start up gVim, it looks like it renames it to _vimrc.2014.
I can't find anything about this behavior, is it normal? It doesn't really affect me too much since it still gets source, but I just want to know why it's doing that.
Vim certainly is not doing this. I'm not certain it's causing YOUR problem, but Google Drive has problems replacing files with a new file of the same name. Under the hood, that is exactly what Vim is doing when it writes a file with default settings. See https://groups.google.com/d/topic/vim_use/jkw_nnHz9cE/discussion : you can use either the 'backupskip' or 'writebackup' options to force Vim to write the file directly instead of replacing it with a new file when editing inside your Google Drive folder. I'm using this line in my .vimrc to accomplish the task:
let &backupskip.=','.expand('$HOME/Google\ Drive/').'*'

strange behavior saving a file with vim on Windows in a folder that requires elevated permission

I'm editing the apache2.conf configuration file under the 'C:\Program Files...' folder. This folder requires elevated permission ("Start program as administrator") for writing to anywhere on this folder.
Opening the file by mistake with vim without elevation - didn't show any warning. Moreover, I was able to save the file (':wq') just fine without any warning. Opening the file again with vim - showed me the modified file. However - no changes in the behavior of Apache (of course).
Trying to open the file with Notepad - show me the file was not modified.
Then, trying to open the file with vim from an elevated shell - I got the original file (just like notepad), but this time, I was able to edit the file, and save it.
I know have two versions of the file. The real file (shown by notepad, elevated shell, just 'type' and seen by apache httpd), and the file I edited and saved from an unelevated shell - stored somewhere (where???).
This is very uncomfortable, as sometime I open this file by mistake from a regular shell (even just for read).
What is going on? How do I exit this state?
I was hit by a Windows feature called "Virtual Store". Underneath the unaware application (that is, vim in this case), the OS running in compatibility mode will save the files in a folder named "Virtual Store", at this location: C:\Users\<login name>\AppData\Local\VirtualStore\.... The application will not be aware the file went there. Subsequent reads will read from that Virtual Store location.
It seems vim isn't UAC aware and it is running in compatibility mode - which could be fixed, but as for version 7.4 it hasn't.

How to set the working directory of WinGHCi

I just installed WinGHCi. When I try to load an .hs file with Ctrl+L, it opens the file browser in C:\Program Files (x86)\Haskell Platform\2012.2.0.0\winghci which is where I installed this. I don't want it to go there by default, I keep my code files in a different directory on a different drive and I don't want to have to navigate to D:\MyPath\Haskell every time I load a file.
I tried to set the "Start in" field of the shortcut I use to launch WinGHCi but it changed nothing.
How can I make WinGHCi look in my own directory by default?
It opens in the last directory you opened a .hs or .lhs from.
Try opening something from the folder you want, exit, restart.
Did it restart where you were?
Try working like that for a while, and if you like it (I do) then you're fine.
If you really do want to go to the same place every time, perhaps there's a way.
I can't find any setting to control this, but the information is stored in the registy.
Mine is at
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Haskell\WinGHCi 1.0.6]
"WorkingDir"="D:\\Files\\Andrew\\prog\\haskell"
You can save your version of that as resetghci.reg:
To make your own, open regedit and first try the same location. If not, search for WinGHCi.
You can export that subtree but you get the whole lot including recent expressions etc, which I don't think you should reset. Save that as ghcioriginal.reg just in case. Edit away all the lines except WorkingDir and save as resetghci.reg
When you double-click it it will ask you if you're sure you want to add that info to the registry. Yes you are.
Untested:
What follows works on older versions, but Microsoft seem to be going off .pif files and the start command, and I'm not able to test this in Windows 7.
Make a text file called ghci.bat in the same folder as the .reg you made. In it put
#echo off
regedit resetghci.reg
start WinGHCi
You might need to specify the full path to WinGHCi.
The start command is the windows equivalent of making a background process from a shell prompt, so this should terminate immediately.
Make a shortcut to ghci.bat (it will be called ghci.pif) and set it to open minimised and put a nice lambda icon on it (nick it from the winghci executable).
Pop that on your desktop, start menu or shortcut bar, and when you use it, you'll be popped back to your standard location.

Resources