38

I just installed gVim, and tried using the usual "vim myfile.java" technique that usually works for linux to open up a file and edit it. But unfortunately, this doesn't seem to work. I've also tried "gvim myfile.java", but that doesn't work either.

Does anyone know how to open up vim (and use it like you do in linux) using Windows Powershell, or some other technique?

Mr Prolog
  • 461
  • 1
  • 4
  • 4
  • 1
    It needs to be added in your PATH environment variable, and if I recall correctly, this requires a restart to become globally available. – Jay Apr 06 '12 at 21:15
  • http://stackoverflow.com/questions/5402615/vim-as-the-default-editor – Romhein Apr 06 '12 at 21:16
  • Thanks, Jay. How do you do the PATH environment variable technique? – Mr Prolog Apr 06 '12 at 21:17
  • 1
    @MrProlog - Go to Control panel/System/Advanced/Environment Variables/ and then add it to the end in PATH variable. This is for XP, but I'm sure it's rather similar on '7. – Rook Apr 06 '12 at 21:27
  • @Jay - Picking a nit: It is not necessary to do a full restart to update environment variables. New processes automatically get the current values from their parent process, and the Windows shell (start menu etc.) immediately sees the new values. So at most you need to start a new command prompt, or start a new instance of gvim (from the shell). – David Pope Apr 12 '12 at 02:24
  • @DavidPope I think this is probably true in principle, but not always so in practice. Anecdotally, you will find myriad questions online to the effect "How can I update PATH without rebooting?!?!" I think in the Windows shell, you may have to kill explorer.exe and re-launch it. – Jay Apr 12 '12 at 02:39
  • 1
    @Jay - Happily, you don't. You can verify it yourself, give it a try. To see all your environment variables in a command prompt just type 'set' with no arguments. So you can keep the Windows "environment variables" dialog up, add a new variable like "ZZZ" with value "foo", launch a new command prompt, and immediately see it show up. Sometimes with background services like web worker processes you have to cycle the web server, but that's just an 'iisreset', after which the new instances come up with the new environment. – David Pope Apr 12 '12 at 03:24
  • 1
    Adding it to the path doesn't require a reboot, but you do have to restart you command window. – Orin Nov 04 '16 at 13:44

5 Answers5

39

When you install gVim:
Please make sure [✓] Create .bat files for command line use is checked.
It'll create several .bat files in C:\Windows\:

C:\>cd %windir%
C:\WINDOWS>dir /b *.bat
evim.bat
gview.bat
gvim.bat
gvimdiff.bat
view.bat
vim.bat
vimdiff.bat
vimtutor.bat

Notice that: C:\WINDOWS is already in the PATH environment variable.
When you type vim in command line, C:\WINDOWS\vim.bat will be launched.
If you leave the checkbox mentioned above unchecked, you need to modify PATH manually.

kev
  • 137,128
  • 36
  • 241
  • 259
  • This is helpful suggestion, but I had already installed it. Default Vim 8.2 install for Windows has the [Create .bat files..] option unchecked. On a current (Mar. 2021) Windows-10 Home Edition box, easiest trick to get "vim" to work, is modify PATH. Click or invoke Control Panel/System/Advanced../Environment Variables/(bottom panel, click on PATH)/(click Edit button)/(click New button)/ and then enter "C:\Program Files (x86)\Vim\vim82" to add the vim path at end of Windows-10 PATH. Next open of a Cmd shell should be able to run vim. – gemesyscanada Mar 27 '21 at 20:43
3

Just to supplement, I'm on a fairly highly controlled Windows workstation right now, and don't have access to much. Downloading the "executable installer" that I usually use did not create the bat files nor, for some reason, vim.exe, though gvim.exe was installed in the vim74 dir for me and worked fine. So though I also needed to set the PATH, that there was no bat file in C:\WiNDOWS nor any command line executable in my VIm runtime folder to call meant that callingvim from the command line (or Powershell) didn't work.

I'm guessing some portion of the install that's creating the command-line related stuff, apparently including vim.exe, isn't recovering gracefully when you don't have admin permissions.

Either way, the "right" thing to do appears to be to set your PATH to your vim executable folder as usual (note that this might be a little more difficult than usual if you don't have admin privs), then download the "Win32 console executable" from the usual download page that matches the version of gvim that you've already installed, dig vim.exe out of that zip you just downloaded, and place that into the same folder as gvim.exe.

Looking on another box where the executable installer did work "fully", there's some jive in the vim.bat file that wasn't installed for me about "collect the arguments in VIMARGS for Win95" and if .%OS%==.Windows_NT goto ntaction, etc etc, but not having any of that doesn't seem to be a problem on Win7, at least. ;^)

Community
  • 1
  • 1
ruffin
  • 13,513
  • 8
  • 72
  • 118
3

Windows 10 has linux subsystem for windows. So you can install bash in windows and from bash you can use vim.I found it more convenient.

Rajat Bhatt
  • 1,237
  • 11
  • 16
  • 1
    This does not answer the question that was asked. The bash for windows is still in beta. It is possible to run these type of commands from the normal windows cmd environment, which is what the question was asking for help with. – deanshanahan Nov 16 '17 at 09:23
  • 1
    @DeeCee I think there is already an answer to run vim from windows command prompt.The question also asks some other techniques so I just gave an alternative way. – Rajat Bhatt Nov 17 '17 at 06:25
  • this works sometimes, but for me it's impractical because you have to use the WLS's code distro. that's problematic because WLS makes it extremely difficult to install packages like cx_Oracle or ROracle (database connectivity), maybe impossible in my noobish experience – 3pitt Jun 20 '18 at 13:21
0

Install gVim on your window and enable ".bat" when you install gvim and click next, done. You can use vim on window.

0

Because of restrictions on my workstation, I had to install my instance of vim locally to the following folder: %LocalAppData%\Vim

But just changing an environmental variable like others suggest is insufficient because the batch files aren't in the installation directory (such as vimdiff). So I figured out where these come from, and made my own batch files to add them to an environmental variable. To help you make these batch files, I've provided the list of commands below for you to run in the vim installation directory (if saving the commands to an install.bat file first, replace all % characters with %% to escape it):

echo @echo off > evim.bat
echo gvim.exe -y %* >> evim.bat

echo @echo off > view.bat
echo vim.exe -R %* >> view.bat

echo @echo off > gview.bat
echo gvim.exe -R %* >> gview.bat

echo @echo off > vimdiff.bat
echo vim.exe -d %* >> vimdiff.bat

echo @echo off > gvimdiff.bat
echo vim.exe -d %* >> gvimdiff.bat

Then you have to go to Start > Search 'Edit environment variables for your account' > Find 'Path' > Edit > Add a new subentry for %LocalAppData%\Vim\vim82. Last you may need to close and open the command prompt, or kill explorer.exe and restart it.

Pluto
  • 2,525
  • 22
  • 31