How do I change the default save location in Vim? - windows

Currently it's C:\Windows\System32 which is pretty impractical. I know that you can change the current directory with :cd [directory], but is there any way to change the default save location for new files without having to change directories all the time?

I am not at Windows so I cannot find the exact menu names and such, but if I recall correctly the easiest way to go about it is:
Find your gVim application
Right click it
Choose Properties,
Change the startup location in one of the tabs. The usual place to set it to would be your home directory.

You can change Vim's current working directory with the :cd command.
See
:help :cd
.
If you want Vim to always start in a particular directory, you can
put the :cd command in your ~/_vimrc file.
If you want Vim to use
the parent directory of the file you are editing as its current
working directory, you can put this setting in your ~/_vimrc file:
.
set autochdir
See
:help 'autochdir'

Related

Change location to shortcut location

I am launching a script with a powershell shortcut (C:\Windows....\powershell.exe -file 'D:\powershell\script.ps1').
Is there a way to make script change its current location to location of said shortcut?
Example: Script itself is in D:\powershell\ and the shortcut is in C:\Work\Project1. I need the script to cd to "C:\Work\Project1\".
Thanks
You can make a shortcut that starts in whatever directory it's located in. All you have to do is modify the "Starts In" property of the shortcut, and blank it out. That's right. Set it to nothing.
Then, when you want to invoke the shortcut, navigate to the folder (directory) where it's located before invoking it. I use this technique for a shortcut that launches powershell but doesn't launch a script. I haven't tested it with a shortcut that launches a script.
Walter Mitty's helpful answer provides an effective solution, provided that the shortcut file is opened from either the Desktop or File Explorer.
This answer provides background information.
You must configure the desired working directory as part of the shortcut file, because the script you invoke knows nothing about the shortcut that invoked it.
Therefore, to configure a specific working directory (e.g., C:\Work\Project1), specify it in the shortcut file's Properties dialog in the Start in: field.
In case you want to update a shortcut file's (*.lnk) working directory programmatically, use the technique from this answer with the .WorkingDirectory property.
Note: In both cases, only an absolute path can be configured as the working directory: File Explorer only allows you to enter an absolute path anyway, and while the programmatic method allows you to assign a relative path, it is instantly resolved to an absolute one, relative to the shortcut's location.
To make the shortcut's own directory the working directory, you can blank out the Start in: field / .WorkingDirectory property, but note the limitations:
Only works when such a shortcut is either opened from either the Desktop or from File Explorer.
By contrast, opening it from the taskbar or (pre-Windows 10 only) the Start Menu, the working directory is $env:windir\System32 (typically, C:\Windows\System32).
If the shortcut targets an application (rather than a document), as in this case, that application invoked by the shortcut - including cmd.exe and powershell.exe - starts in the configured / implied working directory.
Caveat re cmd.exe: If the working directory is specified as as a UNC path, cmd.exe won't be able to change to that directory; as a workaround, use a path with a mapped drive instead (but, obviously, that drive must be mapped at the time the shortcut is opened).
You can use the TargetPath property of the shortcut (maybe do a get-childitem $psscriptroot where name is like scriptname and extension is like .lnk) with the set-location cmdlet
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/set-location?view=powershell-6

Why does the TextMate title bar not show the file path when opened with the "mate" command?

When I open a file in TextMate using the mate command the title bar does not allow me to right click to see the path of the file while files opened within the TextMate app do show the full path. Is there something about the behavior of the mate command that prevents this functionality or is this a bug?
If you wish to have the path visible in the title bar the mate command must include the full path of the file. Using mate with an assumed current working directory (CWD) does not inherently provide mate the full file path.
If you have a local project .tm_properties file it's trivial to have TM2 display the path - add something like:
windowTitle = "$TM_DISPLAYNAME – $TM_DIRECTORY"
Works with $ mate [filename] in terminal and in normal finder use.
But I agree it's pretty lame that it doesn't seem to work for the global .tm_properties - though I'd love to be proved wrong!
Go to folder "/Applications/TextMate.app/Contents/Resources" or where your TextMate.app installation folder. Close textmate app before modifying the file.
Modify the file Default.tmProperties as below. I have provided the diff here:
diff Default.tmProperties
../textmateBin/TextMate.app/Contents/Resources/Default.tmProperties
10c10
< windowTitleProject = '${projectDirectory:+ — ${projectDirectory}}'
windowTitleProject = '${projectDirectory:+ — ${projectDirectory/^.*///}}'
Restart your textmate app and it should display your folder path :)
I think TextMate has updated the documentation on their website.
It even contains a similar snippet to solve the issue above.
For example, if we want to show the path as relative to the project
folder, we can use:
windowTitle = '${TM_DIRECTORY:+${TM_DIRECTORY/^${projectDirectory}(?:\/(.+))?/${1:+$1/}/}}$TM_DISPLAYNAME$windowTitleProject$windowTitleSCM'
Source: https://macromates.com/textmate/manual/projects

How can I show hidden files (starting with period) in NERDTree?

How can I make NERDTree show files that begin with an . (period)?
I have a file named .hgignore in a project directory. If I browse to that directory using NERDTree, the file is not displayed within the tree.
I am using gvim on Windows.
Press I (Shift+i) to toggle hidden files in the NERDTree explorer window.
To enable this behavior by default, add this line to your .vimrc file:
let NERDTreeShowHidden=1
For more detail, access the NERDTree help file :help NERD_tree.txt and search for "hidden".
If you are using Neovim on Linux, the configs are in the file :
home/[user]/.config/nvim/init.vim

"Register" an .exe so you can run it from any command line in Windows

How can you make a .exe file accessible from any location in the Windows command window? Is there some registry entry that has to be entered?
You need to make sure that the exe is in a folder that's on the PATH environment variable.
You can do this by either installing it into a folder that's already on the PATH or by adding your folder to the PATH.
You can have your installer do this - but you may need to restart the machine to make sure it gets picked up.
Windows 10, 8.1, 8
Open start menu,
Type Edit environment variables
Open the option Edit the system environment variables
Click Environment variables... button
There you see two boxes, in System Variables box find path variable
Click Edit
a window pops up, click New
Type the Directory path of your .exe or batch file ( Directory means exclude the file name from path)
Click Ok on all open windows and restart your system restart the command prompt.
You can add the following registry key:
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\myexe.exe
In this key, add the default string value containing the path to the exe file.
You have to put your .exe file's path into enviroment variable path. Go to "My computer -> properties -> advanced -> environment variables -> Path" and edit path by adding .exe's directory into path.
Another solution I personally prefer is using RapidEE for a smoother variable editing.
Rather than putting the executable into a directory on the path, you should create a batch file in a directory on the path that launches the program. This way you don't separate the executable from its supporting files, and you don't add other stuff in the same directory to the path unintentionally.
Such batch file can look like this:
#echo off
start "" "C:\Program Files (x86)\Software\software.exe" %*
Let's say my exe is C:\Program Files\AzCopy\azcopy.exe
Command/CMD/Batch
SET "PATH=C:\Program Files\AzCopy;%PATH%"
PowerShell
$env:path = $env:path + ";C:\Program Files\AzCopy"
I can now simply type and use azcopy from any location from any shell inc command prompt, powershell, git bash etc
It is very simple and it won't take more than 30 seconds.
For example the software called abc located in D:/Softwares/vlc/abc.exe
Add the folder path of abc.exe to system environment variables.
My Computer -> Click Properties -> Click Advanced system settings -> Click Environment Variables
Click on Ok.
now you can just open cmd prompt and you can launch the software from anywhere.
to use abc.exe just type abc in the command line.
it's amazing there's no simple solution for such a simple task on windows,
I created this little cmd script that you can use to define aliases on windows (instructions are at the file header itself):
https://gist.github.com/benjamine/5992592
this is pretty much the same approach used by tools like NPM or ruby gems to register global commands.
Simple Bash-like aliases in Windows
To get global bash-like aliases in Windows for applications not added to the path automatically without manually adding each one to the path, here's the cleanest solution I've come up with that does the least amount of changes to the system and has the most flexibility for later customization:
"Install" Your Aliases Path
mkdir c:\aliases
setx PATH "c:\aliases;%PATH%"
Add Your Alias
Open in New Shell Window
To start C:\path to\my program.exe, passing in all arguments, opening it in a new window, create c:\aliases\my program.bat file with the following contents(see NT Start Command for details on the start commmand):
#echo off
start "myprogram" /D "C:\path to\" /W "myprogram.exe" %*
Execute in Current Shell Window
To start C:\path to\my program.exe, passing in all arguments, but running it in the same window (more like how bash operates) create c:\aliases\my program.bat file with the following contents:
#echo off
pushd "C:\path to\"
"my program.exe" %*
popd
Execute in Current Shell Window 2
If you don't need the application to change the current working directory at all in order to operate, you can just add a symlink to the executable inside your aliases folder:
cd c:\aliases\
mklink "my program.exe" "c:\path to\my program.exe"
Add to the PATH, steps below (Windows 10):
Type in search bar "environment..." and choose Edit the system environment variables which opens up the System Properties window
Click the Environment Variables... button
In the Environment Variables tab, double click the Path variable in the System variables section
Add the path to the folder containing the .exe to the Path by double clicking on the empty line and paste the path.
Click ok and exit. Open a new cmd prompt and hit the command from any folder and it should work.
If you want to be able to run it inside cmd.exe or batch files you need to add the directory the .exe is in to the %path% variable (System or User)
If you want to be able to run it in the Run dialog (Win+R) or any application that calls ShellExecute, adding your exe to the app paths key is enough (This is less error prone during install/uninstall and also does not clutter up the path variable)
You may also permanently (after reboots) add to the Path variable this way:
Right click My Computer -> Click Properties -> Click Advanced system settings -> Click Environment Variables
Reference: Change System/User Variables
Put it in the c:\windows directory or add your directory to the "path" in the environment-settings (windows-break - tab advanced)
regards,
//t
In order to make it work
You need to modify the value of the environment variable with the name key Path, you can add as many paths as you want separating them with ;. The paths you give to it can't include the name of the executable file.
If you add a path to the variable Path all the excecutable files inside it can be called from cmd or porweshell by writing their name without .exe and these names are not case sensitive.
Here is how to create a system environment variable from a python script:
It is important to run it with administrator privileges in order to make it work. To better understand the code, just read the comments on it.
Tested on Windows 10
import winreg
# Create environment variable for call the program from shell, only works with compiled version
def environment_var(AppPath):
# Point to the registry key of the system environment variables
key = winreg.CreateKey(winreg.HKEY_LOCAL_MACHINE, r'System\CurrentControlSet\Control\Session Manager\Environment')
def add_var(path):
# Add the variable
winreg.SetValueEx(key, 'Path', 0, winreg.REG_SZ, path)
winreg.CloseKey(key)
try:
# Try to get the value of the Path variable
allPaths = winreg.QueryValueEx(key, 'Path')[0]
except Exception:
# Create the Path variable if it doesn't exist
add_var(path=AppPath)
return
# Get all the values of the existing paths
Path=allPaths.split(';')
# If the Path is empty, add the application path
if Path == ['']:
add_var(path=AppPath)
return
# Check if the application path is in the Path variable
if AppPath not in Path:
# Add the application path to the Path environment variable and add keep the others existing paths
add_var(path=AppPath+';'+allPaths)
# Only run this if the module is not imported by another
if __name__ == "__main__":
# Run the function
environment_var(AppPath=".")
You can find more information in the winreg documentation
You can also move your files to C:\Windows, but you need to use Administrator privileges and pay attention.
What did I mean with pay attention?
You need pay attention because you can also do some messes with Windows system files (Windows may not even work anymore) if you modify, delete, and do some changes incorrectly and accidentally in this folder...
Example: Don't add a file that have the same name of a Windows file
This worked for me:
put a .bat file with the commands you need (I use to run .py script into this) into a FOLDER,
go in the variable environment setting (type var in the search bar and it will show up)
in the global settings
choose path,
then modify,
then add the path to your .bat file (without the .bat file)
close everything: done.
Open the cmd, write the name of the .bat file and it will work
Example
Want to open chrome on a specific link
create a .bat file with this (save it as blog.bat for example)
start "" "https://pythonprogramming.altervista.org/"
go in enviromental variable settings from the search bar in the bottom left of the window desktop
go in enviromental variables (bottom button) then in path (bottom)
add the path, for example G:\myapp_launcher
click apply or ok
Now open cmd and write blog: chrome will open on that page
Do the same to open a file... create a .bat in the folder G:\myapp_launcher (or whatever you called the folder where you put the batch file), call it run.bat or myapp.bat or whatever (write inside of it start filemane.pdf or whatever file you want to open) and after you saved it, you can run that file from cmd with run or myapp or whatever you called your batch file.
Use a 1 line batch file in your install:
SETX PATH "C:\Windows"
run the bat file
Now place your .exe in c:\windows, and you're done.
you may type the 'exename' in command-line and it'll run it.
Another way could be through adding .LNK to your $PATHEX.
Then just create a shortcut to your executable (ie: yourshortcut.lnk) and put it into any of the directories listed within $PATH.
WARNING NOTE:
Know that any .lnk files located in any directories listed in your $PATH are now "PATH'ed" as well. For this reason, I would favor the batch file method mentionned earlier to this method.
I'm not a programmer or anything of the sort, but here's my simple solution:
Create a folder in which you'll be putting SHORTCUTS for all the programs you want to register;
Add that folder to the PATH;
Put all the shortcuts you want in the folder you created in the first step (context menu, New, Shortcut...) The SHORTCUT NAME will have be the be summoned when calling the program or function... NOT THE TARGET FILE NAME.
This will keep you from unintentionally putting files you don't want in the PATH.
Feel free to drop a comment if you think this answer needs to be improved. Cheers 🍻.
P.S. No system or File Explorer restart needed. 😀
Best way is to add the folder path for the .EXE file to Path values in the environment variable.
I'm not sure what versions of Windows this works with, but I put some useful .bat and .exe files into:
%LOCALAPPDATA%\Microsoft\WindowsApps
(equivalent to %USERPROFILE%\AppData\Local\Microsoft\WindowsApps)
which seems to be on my default PATH. I'd be interested to see if this were the general case.
DOSKEY is a Microsoft version of 'alias'. That function is already built into all versions of Windows (and most versions of DOS)
doskey fred=c:\myApps\myprog.exe
You'll want to load that every time you open a command prompt. Which you can do by any number of different methods. One way is to
Make a file containing all the doskey macros you want:
doskey fred=c:\whatever.exe
doskey alan=c:\whateverelse.exe
Change the file type / file name / file extension to .CMD or .BAT
ren myfile.txt myfile.CMD
Add the CMD/BAT file to your command processor autoruns key:
reg ADD \\HKCU\Software\Microsoft\Command Processor /v autorun /t REG_SZ /d myfile.CMD
For more information see
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/doskey
and
https://serverfault.com/a/1049766/142882
(serverfault.com/questions/95404/is-there-a-global-persistent-cmd-history)
Should anyone be looking for this after me
here's a really easy way to add your Path.
Send the path to a file like the image shows,
copy and paste it from the file and add the
specific path on the end with a preceding semicolon
to the new path. It may be needed to be adapted prior
to windows 7, but at least it is an easy starting point.
Command Prompt Image to Export PATH to text file
The best way to do this is just install the .EXE file into the windows/system32 folder. that way you can run it from any location. This is the same place where .exe's like ping can be found

Vim: Change start up directory?

When I start GVim and start writing my little program I'd like to save the file to the Desktop but it seems that Vim is starting the command line in:
C:\Windows\System32
How would I go about changing that to:
C:\Users\Casey
so then I could just:
:w Desktop\my_program.py
Assuming you're starting gvim from an icon/shortcut in Windows (from the kind of paths you supply), you can change the starting directory (completely independent from the fact that it IS gvim: it would be the same from any other app!) by editing the "starting directory" property of that Windows icon/shortcut.
Within vim, you can change the current directory with the :cd command:
:cd c:\users\casey
I found this way to be better:
Open gVim
:cd $vim
:e _gvimrc
Add the following line:
:cd c:\users\user\desktop\
I found that :Ex is slow on large directories like c:\windows\system32\ (where gVim usually starts).
Also, here is my full _gvimrc in case anyone is interested. I prefer Consolas for coding. The tabstop setting helps when coding especially in Python, and Ctrl+Tab/Ctrl+Shift+Tab is great for switching between buffers:
set guifont=Consolas:h12:cANSI
set tabstop=4 softtabstop=4 shiftwidth=4 expandtab
map <C-Tab> :bnext<cr>
map <C-S-Tab> :bprevious<cr>
:cd c:\users\user\desktop\
How about changing your starting position?
vim icon -> right click -> property -> shortcut -> Start in -> your path you want to change.
but it works perfectly.
I think :cd yourpath also works. but it will change when you don't want to change.
Use :cd c:\users\casey,
after that save into session (in gVim there is button up and down in red, click on it and save as mySessionProject.vim). Next time you need to go to that directory, open that session (you can also use :source mySessionProject.vim)
for command line:
:mksession! yourdir/yourVimConfName.vim
to load
:source yourDir/yourVimConfName.vim
I found the following to be very useful. I am on Windows 7 and vim 7.3.46, and am using gVim.
I edited startup settings, which wound up altering the _vimrc in c:\Users\me\.
I also tried using :version and editing the _vimrc files I found at $VIM, as well as the _vimrc I found at c:\windows\system32.
Editing those files to include :cd c:\Users\me did not result in my default startup directory changing after starting vim. I wanted my default directory to be c:\Users\me\, and editing c:\Users\me\_vimrc achieved that. That is I entered
:e $MYVIMRC
and added
cd c:\Users\cnorton.Arlington1\
to that file.
Just to to put this up incase anyone needs it:
vimrc accepts enironmental parameters. you can put cd $USERPROFILE in your vimrc
Use this mapping in your .vimrc file
:cd $USERPROFILE\Desktop<cr>
or the same shorter
cd ~\Desktop<cr>
A mapping that also displays afterwards the path instead of the command
nmap <leader>d :cd ~\Desktop<cr>:pwd<cr>
Inside init.vim, I use:
lcd $HOME/Projects

Resources