💾 Archived View for pwshnotes.flounder.online › gemlog › 2021-08-30-installing-vim.gmi captured on 2021-12-03 at 14:04:38. Gemini links have been rewritten to link to archived content

View Raw

More Information

-=-=-=-=-=-=-

Installing Vim

Background

PowerShell is a command-line program. Sometimes it would be useful to have a text editor present at the command-line. Then you could read and edit text files without leaving the shell. And you would not depend on graphical tools like Notepad. Some systems do not have a desktop environment at all. In those cases, command-line utilities become indispensable.

Vim is a popular text editor on Linux. It can run inside a shell. And there is a port available for Windows.

Today, I will install Vim on Windows 10. I will get the command-line version of Vim running in PowerShell. And I will create a new file, edit it, and save.

The most important thing to understand about Vim is that it is difficult for new users to learn. That is because Vim is not like other programs you're used to. It does not have graphical elements, menus, or keyboard shortcuts.

Instead, the main loop a user proceeds through while learning Vim is:

As long as you can manage the Vim modes and use the built-in help, you can learn anything you want about Vim.

You can also find help from people online. Those links will appear in my help file. See link below.

Vim uses modes to modify the function of keys. For example, in one mode, pressing 'i' begins the process of inserting text. In another mode, 'i' adds a literal i to the body of the document. And in another mode, 'i' types an i at the Vim command line.

Another way to think about modes is that it is like holding the Ctrl or Alt keys down in other programs. Instead of typing literal characters, each key now has a special function. And that is the mode Vim begins in: Normal mode.

There are four basic Vim modes:

Here's a quote from the mode-switching topic to help you get out of any mode you're in:

If for any reason you do not know which mode you are in, you can always get back to Normal mode by typing <Esc> twice.

To reach the Vim help file, open Vim and press F1. Or, :help (typing "help" in command-line mode) will open help. This is a good place to start learning about Vim. If you want to view the topic I quoted, ":help mode-switching" will take you directly there. Use :q to leave help and return to the editor. You can also view the help files online if that is easier for you. See link below.

Previously, I installed PowerShell 7 and Windows Terminal. See links below. This will affect the Vim build (or version) I choose today. I'd like mouse support while using Vim in Windows Terminal. For that to work, I need an unsigned, nightly build of Vim. The problem this causes is Windows will block unsigned programs from installing. My instructions will specify a nightly build and disable SmartScreen for that install. This means you will have to trust that this software is safe for your computer. If you want a signed version, you can visit the Vim download page. See link below. There, search for the latest "stable" version of Vim for Windows. The drawback with this route is builds of Vim before 8.2.2913 will not support the mouse in Windows Terminal. The latest signed version is build 2825.

Installation

gvim_8.2.3386_x86.exe

Now gvim_8.2.3386_x86.exe is saved to my hard disc. Note "gvim" is the correct name. Vim is included in this setup file.

You will need to run the setup as administrator.

Note: You might be notified by SmartScreen that this software is not signed:

Windows protected your PC
Microsoft Defender SmartScreen prevented an unrecognized app from starting. Running this app might put your PC at risk.
More info (link)
Don't run (button)

If you click the 'More info' link, there will be a button at the bottom to 'Run anyway'. Since I trust the source of this software, I will click Run anyway and continue with the installation.

Now the Vim setup program is running.

If you left the 'readme' box checked on the last slide, the graphical version of Vim, gVim, will launch. Please close gVim when you're done with the readme.

Sample Run

I want to run Vim in PowerShell. There are no shortcuts to Vim in PowerShell. So I will have to specify the full path to Vim before I can run it. Also, I previously installed PowerShell 7 and Windows Terminal. I will refer to those applications in these steps.

The full path to my vim.exe is:

"C:\Program Files (x86)\Vim\vim82\vim.exe"

Here the "vim82" folder corresponds to Vim version 8.2 In the future, that folder name will change to match the then current version of Vim. Example: vim90 for Vim 9.0

To run a program in PowerShell, we need the call operator:

&

So, if we want to run Vim, we can combine the full path with the call operator:

& "C:\Program Files (x86)\Vim\vim82\vim.exe"

The first thing that would be helpful to learn is how to exit Vim. This causes a lot of confusion.

First, we'll have to enter Command-line mode.

" Use :q to quit Vim. 
:q

You can use the command history in PowerShell to launch Vim again.

& "C:\Program Files (x86)\Vim\vim82\vim.exe"

Let's create a new file.

By default, Vim launches showing a new file. This is called an empty buffer.

The default message shown when launching:

VIM - Vi IMproved
version 8.2.3386
by Bram Moolenaar et al.
Vim is open source and freely distributable
Sponsor Vim development!
type :help sponsor<Enter> for information
type :q<Enter> to exit
type :help<Enter> or <F1> for on-line help
type :help version8<Enter> for version info

... is just overlaid on the window and that text won't appear in any file you start editing. In fact, once you issue your first command, this prompt disappears.

To start editing this empty buffer, we can enter Insert mode.

While in insert mode, any alphanumeric keys you press will be entered literally into the file (or buffer).

Feel free to practice typing while in Insert mode. You will notice keys like Backspace, Delete, arrow keys, End, etc have their normal function.

If you type until the end of the line, your text will wrap to the next line. Note this is a visual break at the character level. So long words may be split at an awkward boundary.

When you are done typing, enter Normal mode.

From here, we can issue a command to save our work.

Vim recognizes tilde ~ as a substitute for the current user's profile folder. Let's take advantage of that to save a file to the desktop. Remember to return to Normal mode first.

The prepared command should look like the following:

:w ~\Desktop\test 01.txt

To save this file again in the future, you can enter :w without specifying a path.

" Use :w to save an existing file. 
:w

Now we're back at the shell.

Now it would be useful to learn how to edit an existing file. This can be done by passing the file name to Vim while launching.

The path to our file is:

"~\Desktop\test 01.txt"

Because our file path includes a space and we'll be using PowerShell, we need to quote the path as shown.

If we combine the call operator, path to Vim, and path to the file then we can edit existing files in Vim.

& "C:\Program Files (x86)\Vim\vim82\vim.exe" "~\Desktop\test 01.txt"

Notice there are two sets of quotes. And each pair is separated by a space. Example:

& "exe" "file"
 ^     ^ space
 ^ space

Vim should open your existing file and begin in Normal mode.

Now you can create a new file, edit any file, save, and quit Vim. So you have all the basic skills you need to edit text files at the command-line.

References

Get Help from Others (Scroll down to the Vim section.)

Vim Online Help Files

Installing PowerShell

Installing Windows Terminal

Vim Main Download Page

Vim Nightly Build Download Page

Created: Monday, August 30, 2021

Updated: Monday, August 30, 2021