29,99 €
Discover what makes Vim one of the most popular text editors in the world, renowned for its blend of efficiency, customization, extensibility, and portability. With Mastering Vim, you’ll come to appreciate its extensive plugin system and seamless integration with various tools, which enable Vim to provide developers with an extensible and customizable development environment for programmers.
This comprehensive guide to Vim will help you become more efficient at editing text: be it prose or code. Starting from the basics, advanced movement, and text operations, you’ll learn how Vim can be used as a full-fledged IDE. You’ll then progress to refactoring, debugging, building, testing, version control, plugins, and Vimscript through practical guidance. This second edition comes packed with fully revamped examples that are both engaging and easy to understand, complemented by drawings and diagrams that help to visualize the concepts covered in the book. Updated to cover Vim 9, this edition includes updated installation and troubleshooting instructions, along with examples in Vim9script.
By the end of this Vim book, you’ll be well-versed in Vim and have the skills you need to build a complete application using this popular text editor.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 293
Veröffentlichungsjahr: 2024
Mastering Vim
Efficient and effortless editing with Vim and Vimscript
Ruslan Osipov
Copyright © 2024 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Associate Group Product Manager: Kunal Sawant
Publishing Product Manager: Samriddhi Murarka
Senior Content Development Editor: Rosal Colaco
Book Project Manager: Deeksha Thakkar
Technical Editor: Jubit Pincy
Copy Editor: Safis Editing
Indexer: Tejal Soni
Production Designer: Gokul Raj S.T
DevRel Marketing Coordinator: Shrinidhi Manoharan
Business Development Executive: Debadrita Chatterjee
First published: November 2018
Second edition: July 2024
Production reference:1190724
Published by Packt Publishing Ltd.
Grosvenor House
11 St Paul’s Square
Birmingham
B3 1RB, UK
ISBN 978-1-83508-187-7
www.packtpub.com
Dedicated to my mother, my grandmother, and my wife, the three most important women in my life.
– Ruslan Osipov
Ruslan Osipov is a software engineering manager at Google, and the author of the bestselling “Mastering Vim” (first edition, 2018). Passionate about developer productivity and workflow optimization, he continues to refine his Vim expertise and share his knowledge with the community in this expanded second edition.
I’d like to say thank you to Samriddhi, Deeksha, Rosal, and everyone else from Packt who worked on this book. A huge thank you to Bram for reviewing the first edition of this book (and may he rest in peace), and Christian. A word goes out to the Vim Japan conference organizers—Tatsuhiro Ujihisa (Uji), Taro Muraoka (KaoriYa), Thinca, Aomoriringo, Mopp, Yasuhiro Matsumoto (Mattn), t9md, and Guyon (and anyone else I missed). Thank you for your hospitality!
Special thank you goes to Masafumi Okura, who diligently identified a number of inaccuracies in the book when translating it to Japanese.
Bram Moolenaar (first edition, 2018) was the creator and maintainer of Vim. With the help of volunteers, he worked on it for 32 years. Bram passed away in 2023.
After studying electronics and inventing parts of digital copying machines, Bram decided that creating open source software was more useful and fun, so he worked on that exclusively for several years. He last found himself employed at Google, one of the few companies that fully embrace open source software. In between, he did voluntary work on a project in Uganda and is still helping poor children there through the I Care Children Foundation (ICCF).
“I would like to thank all the Vim developers for helping me make Vim into what it is today. Without them, only a fraction of the features would have been implemented and the quality would not have been nearly as high. I would also like to thank all the plugin writers for building on top of Vim and making complex features available to users (so that I don’t have to!). And finally, I would like to thank Ruslan for writing a book that not only aids users with Vim’s built-in features, but also with getting to know and use plugins.”
Bram Moolenaar, 2018
Christian Brabandt (second edition, 2024), has been working in the IT industry for almost 25 years, mostly as a consultant in various roles, and enjoys working with people and helping them implement a complex technology stack. He is currently employed with Ataccama as Senior Platform Consultant where he often has to work with Vim in various incarnations and on different platforms.
He has been involved within the Vim community in various roles ever since graduating from university and slowly taking a bigger role in Vim development. Since 2023 he has been one of the main maintainers of the Vim project and helped lead the project after the passing of Bram Moolenaar, the long-standing Vim maintainer.
I’d like to thank my family and friends for helping me through some personal dark times, allowing me to take away their precious time, spending it on Vim development, and investing it into the Vim community. Thank you all for your support and for allowing me to be who I am!
I’d also like to thank everybody in the Vim community for spreading the word and contributing to Vim in different ways and keeping the project healthy!
Mastering Vim will introduce you to the wonderful world of Vim through examples of working with Python code and tools in a project-based fashion. This book will prompt you to make Vim your primary IDE since you will learn to use it for any programming language.
Mastering Vim is written for beginner, intermediate, and expert developers. The book will teach you to effectively embed Vim in your daily workflow. No prior experience with Python or Vim is required.
Chapter 1, Getting Started, introduces the reader to basic concepts and the world of Vim.
Chapter 2, Advanced Editing and Navigation, covers movement and more complex editing operations and introduces many plugins.
Chapter 3, Follow the Leader – Plugin Management, talks about modes, mappings, and managing your plugins.
Chapter 4, Understanding the Text, helps you interact with, and navigate, code bases in a semantically meaningful way.
Chapter 5, Build, Test, and Execute, explores options for running code in, or alongside, your editor.
Chapter 6, Refactoring Code with Regex and Macros, takes a deeper look at refactoring operations.
Chapter 7, Making Vim Your Own, discusses options available for further customizing your Vim experience.
Chapter 8, Transcending the Mundane with Vimscript, dives into the powerful scripting language Vim provides.
Chapter 9, Where to Go from Here, provides some farewell food for thought, talks about Vim’s younger sibling, and points at a few places on the internet you might be interested in.
The code bundle for the book is hosted on GitHub at https://github.com/PacktPublishing/Mastering-Vim-Second-Edition. In case there’s an update to the code, it will be updated on the existing GitHub repository.
We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
There are a number of text conventions used throughout this book.
Code in text: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: “Functions in Vimscript 9 are defined using the def keyword.”
Keypresses are indicated as follows: jk. This means keypress j, followed by keypress k. More complex keypress chords are written out explicitly (e.g. Ctrl + j, k).
A block of code is set as follows:
" Manage plugins with vim-plug. call plug#begin() call plug#end()When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
" Manage plugins with vim-plug. call plug#begin() Plug 'scrooloose/nerdtree' Plug 'tpope/vim-vinegar' Plug 'ctrlpvim/ctrlp.vim' Plug 'mileszs/ack.vim' Plug 'easymotion/vim-easymotion' call plug#end()Any command-line input is written as follows:
$ cd ~/.vim $ git initBold: Indicates a new term, an important word, or words that you see on screen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: “Select System info from the Administration panel.”
Warning
Warnings or important notes appear like this.
Tip
Tips and tricks appear like this.
Feedback from our readers is always welcome.
General feedback: If you have questions about any aspect of this book, email us at [email protected] and mention the book title in the subject of your message.
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata and fill in the form.
Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Once you’ve read Mastering Vim, we’d love to hear your thoughts! Please click here to go straight to the Amazon review page for this book and share your feedback.
Your review is important to us and the tech community and will help us make sure we’re delivering excellent quality content.
Thanks for purchasing this book!
Do you like to read on the go but are unable to carry your print books everywhere?
Is your eBook purchase not compatible with the device of your choice?
Don’t worry, now with every Packt book you get a DRM-free PDF version of that book at no cost.
Read anywhere, any place, on any device. Search, copy, and paste code from your favorite technical books directly into your application.
The perks don’t stop there, you can get exclusive access to discounts, newsletters, and great free content in your inbox daily
Follow these simple steps to get the benefits:
Scan the QR code or visit the link belowhttps://packt.link/free-ebook/978-1-83508-187-7
Submit your proof of purchaseThat’s it! We’ll send your free PDF and other benefits to your email directlyWelcome to Mastering Vim, a book that will teach you how to get good with Vim, its plugins, and its ideological successors!
This chapter will establish a foundation for working with Vim. Every tool is built with a particular usage philosophy in mind, and Vim is no exception. Vim introduces a different way of working with text compared to what most people are used to these days. This chapter focuses on highlighting these differences and establishing a set of healthy editing habits. It will let you approach Vim with a Vim-friendly frame of mind and will ensure you’re using the right tools for the job. To make examples concrete, we will be using Vim to create a small Python application throughout this chapter.
The following topics will be covered in this chapter:
The difference between major Vim versionsModal versus modeless interfaces, and why is Vim different from other editorsInstalling and updating VimThe gVim – the graphical user interface for VimConfiguring Vim for working with Python and editing your configurationCommon file operations – opening, modifying, saving, and closing filesMoving around – navigating with arrow keys and cursor movement keys, by words, paragraphs, and so onMaking simple edits to files and combining editing commands with movement commandsPersistent undo historyNavigating the built-in Vim manualThroughout this chapter, we will be writing a basic Python application. You don’t have to download any code to follow along with this chapter as we’ll be creating files from scratch. However, if you ever get lost and need more guidance, you can view the resulting code on GitHub:
https://github.com/PacktPublishing/Mastering-Vim-Second-Edition/tree/main/Chapter01
We will be using Vim to primarily write Python code throughout this book, and it is assumed that the reader is somewhat familiar with the language. Examples assume you’re using Python 3 syntax.
Working with Python 2 code
If you must live in the past, you can convert Python 3 examples to Python 2 code by changing the print() command syntax. Change all of print('Woof!') to print 'Woof!' to make the code run in Python 2.
We will also be creating and modifying Vim configurations, which are stored in a .vimrc file. The resulting .vimrc file is available from the previously mentioned GitHub link.
Let’s go back to the beginning of time: the middle of the twentieth century. Before personal computers and terminals, there were teleprinters. A teleprinter is a mechanical typewriter that can send and receive messages over a telecommunications channel. Here’s a photo of the Teletype ASR-33 (1963) teleprinter – a fancy typewriter that was used as an interface and an input device for a computing machine:
Figure 1.1 – Teletype Corporation ASR-33 teleprinter (image by Arnold Reinhold, Wikipedia (CC BY-SA 3.0))
The only way to engage with the text through teleprinters was line by line, so line editors such as ed (developed by Ken Thompson) or its successor – Bill Joy’s ex – were used. Line editors were, just like Vim is today, modal editors (more about that in a bit) but, due to aforementioned input/output limitations, were limited to working on one line at a time. Yes, really.
As technology progressed, teleprinters were replaced by terminals with screens, which enabled much more robust text editing. Vim’s direct predecessor, vi, started its life all the way back in 1976. Unlike its predecessors, vi (developed by Bill Joy, the author of ex) included many quality-of-life features and even allowed you to edit multiple lines of text at once – what a luxury!
Did you know?
On many modern systems, the vi command is a symlink to a feature-limited version of vim referred to as vim-tiny (rather than the original vi implementation)!
Vi inspired many clones, including STEVIE (ST Editor for VI Enthusiasts): and STEVIE source code is what was eventually used as a basis for Vim. The first version of Vim was released in 1991 by Bram Moolenaar. Now, you’re all caught up!
While it’s not very likely that you’ll stumble upon a device with Vim that hasn’t been updated since the late 1990s, it helps to get a basic idea of what changed between different Vim versions.
Here, you can see the (oversimplified) highlights of each major version of Vim up until the moment of writing the second edition of this book. Vim has been in some form of continuous development since 1991!
Major version
Years
Highlights
1.0
1991
Bram Moolenaar releases Vi Imitation for the Amiga computer.
2.0
1993
Vi Improved is released under its modern name!
3.0
1994
Multiple windows.
4.0
1996
Graphical interface support.
5.0 – 5.8
1998 – 2001
Syntax highlighting, scripting, and select mode are added.
6.0 – 6.4
2001 – 2005
Plugin support and folding support are added.
7.0 – 7.4
2006 – 2013
Notable new features include spell checking, code completion, tabs, branching history, and persistent undo.
8.0 – 8.2
2016 – 2019
Optimizations, asynchronous I/O support, built-in terminal, and pop-up windows are available.
9.0
2022
Introduction of the new scripting language (Vim9script).
Just like with vi, there are many Vim clones, and some are quite successful. This book covers one of the more successful alternative implementations of Vim in Chapter 9.
Ask for :help
If you’re interested in a more detailed breakdown of the differences between the versions, you can open :help (you’ll learn how to use that by the end of the chapter) and look for “Versions”. Try :help version9 to see what’s new in Vim 9!
If you’ve ever edited text before, you are most likely to be familiar with modeless interfaces. It’s the default option chosen by modern mainstream text editors, and that’s how many of us learned to work with text. If you’re old enough to remember a time before smartphones, many landlines and early mobile phones were also modeless.
The term modeless refers to the fact that each interface element has only one function. Each button press results in a letter showing up on screen, or some other action being performed. Each key (or a combination of keys) always does the same thing: the application always operates in a single mode.
However, this is not the only way.
Welcome to the modal interface, where each trigger performs a different action based on context. The most common example of a modal interface that we encounter today is a smartphone. Each time we work in different applications or open different menus, a tap on the screen performs a different function.
Figure 1.2 – A smartphone uses a modal interface, while the traditional phone is (mostly) modeless
It’s similar when it comes to text editors. Vim is a modal editor, meaning that a single button press might result in different actions, depending on context. Are you in insert mode (a mode for entering text)? Then, hitting o would put the letter o on the screen. However, as soon as you switch to a different mode, the letter o will change its function to add a new line below the cursor.
Working with Vim is like having a conversation with your editor. You tell Vim to delete the next three words by pressing d3w (delete 3words), and you ask Vim to change the text inside quotes by pressing ci” (change inside “ [quotes]).
You may hear very frequently that Vim is faster than other editors, but it’s not necessarily the point of Vim. Vim lets you stay in the flow when working with text. You don’t have to break the pace to reach for your mouse, you don’t have to hit a single key exactly 17 times to get to a particular spot on the page. You don’t have to drag your mouse millimeter by millimeter to ensure you capture the right set of words to copy and paste.
When working with a modeless editor, workflow is filled with interruptions. Working with modal editors has a certain sense of flow to it: you ask the editor to perform actions in a consistent language. With Vim, editing becomes a much more deliberate exercise.
Vim is available on every platform, and comes installed on Linux and macOS (however, you may want to upgrade Vim to a more recent version). You have different options for setting up Vim depending on your operating system and preference, and here’s a handy crude drawing showing some of the more common options:
Figure 1.3 – Options for installing Vim across different OSs
Find your system in the following sections, and skim through the instructions to set it up.
Why so many screenshots?
You’ll see that I’ve included a large number of details and screenshots in the following installation instructions. While Vim is easily available on most platforms, getting the latest version of Vim installed is not as straightforward as one would expect. If you realize that you’re using the wrong version of Vim – you can always go back to the instructions in this chapter for help.
Linux machines come with Vim installed, which is great news! However, it might be rather out of date, and a new version of Vim often includes new functionality and optimization changes (you can read more about changes between versions in the A brief history lesson section of this chapter). Pull up your Command Prompt and run the following code to build an up-to-date Vim from the latest patch (at the time of cloning the source repository):
$ git clone https://github.com/vim/vim.git $ cd vim/src $ ./configure --prefix=/usr/local --with-features=huge $ make $ sudo make installKeep on reading to learn more about Compilation options in the next section.
Missing dependencies
If you’re running into issues as you’re installing Vim, you might be missing some dependencies. If you’re using a Debian-based distribution, the following command should add common missing dependencies:
$ sudo apt-get install make build-essential \
libncurses5-dev libncursesw5-dev --fix-missing
This will make sure that you’re on the latest major and minor patches of Vim. If you don’t care about being on the cutting edge, you can also update Vim using a package manager of your choice. Different Linux distributions use different package managers; the following list includes some common ones:
Distribution/System
Command to install the latest version of Vim
Debian-based (Debian, Ubuntu, Mint)
$ sudo apt-get update
$ sudo apt-get install vim-gtk
CentOS (and Fedora prior to Fedora 22)
$ sudo yum check-update
$ sudo yum install vim-enhanced
Fedora 22+
$ sudo dnf check-update
$ sudo dnf install vim-enhanced
Arch
$ sudo pacman -Syu
$ sudo pacman -S gvim
FreeBSD
$ sudo pkg update
$ sudo pkg install vim
Pay attention to names
You can see in the preceding table that Vim uses different package names for different repositories. Packages such as vim-gtk on Debian-based distributions or vim-enhanced on CentOS come with more features enabled (such as GUI support for instance).
Do keep in mind that package manager repositories tend to lag behind from anywhere between a few months to a few years.
That’s it; you’re now ready to dive into the world of Vim! You can start the editor by typing the following command:
$ vimvim versus vi
Vi is Vim’s predecessor (Vim stands for Vi Improved) and is available to be invoked via the vi command. On some distributions, the vi command links to a feature-stripped version of Vim (aka vim-tiny), while on some it’s merely a symlink to a feature-complete Vim.
Compiling from source is often the best way to receive the latest available version of Vim. If you’re not afraid to get your hands dirty, you might want to know about common compilation options.
This section will cover various options for the configure command. To compile Vim, you’ll have to run the following commands, with <options> replaced with the desired compilation options:
$ git clone https://github.com/vim/vim.git $ cd vim/src $ ./configure <options> $ make $ sudo make installYou can control installation location with --prefix:
--prefix=/usr/local as a reasonable default for a system-wide installation--prefix=$HOME/.local will make the newly installed Vim only available to your userFeature sets allow you to enable different sets of features to be available in Vim, with options being tiny, small, normal, big, and huge. The most interesting options include the following:
--with-features=huge includes all possible features, including experimental ones--with-features=normal includes a reasonable feature set, which is mostly covered in this book--with-features=tiny only offers bare-bones essentials, without syntax highlighting or plugin supportLanguage support is controlled via the --enable-<language>interp option. Note that this refers to the ability of Vim’s internals to interact with the selected programming language (e.g., in plugins or your own scripts), and not your ability to edit said files. Some options include the following:
--enable-luainterp=yes enables Lua support--enable-perlinterp=yes enables Perl support--enable-python3interp=yes enables Python 3 support--enable-rubyinterp=yes enables Ruby supportFinally, to integrate the clipboard with the X11 Window System (that is, your system-wide clipboard on Linux), you might want to compile Vim with the --with-x option. Note that you might need the X development library (e.g., you can use sudo apt install libx11-dev libxtst-dev if you’re using the aptpackage manager).
macOS comes prepackaged with Vim, but the version can be outdated. There are a few ways to install a fresh version of Vim, and this book will cover two. First, you can install Vim using Homebrew, a package manager for macOS. You’ll have to install Homebrew first, though. Second, you can download a .dmg image of MacVim. This experience would be more familiar because Mac users are used to the visual interface.
Since this book covers interactions with the command line, I recommend taking the Homebrew route. However, you’re welcome to go forward with installing the image if interacting with the command line does not interest you.
Homebrew is a third-party package manager for macOS, which makes it easy to install and keep packages up to date. Instructions on how to install Homebrew are available on https://brew.sh, and, as of the moment of writing this book, consist of a single line executed in the following command line:
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"Once you run that prompt, hit Enter to continue (as many times as you need to).
If you don’t have Xcode
If you don’t have Xcode installed (which is often a prerequisite for any kind of development-related activity on Mac), you’ll get an Xcode installation popup. We won’t be using Xcode directly, and you can install it with default settings.
This should take a while to run, but you’ll have Homebrew installed by the end: a fantastic tool you can use to install a lot more than Vim! You’ll see the Installation successful! message in bold font once the installation is complete.
Let’s install a new version of Vim now using the following command:
$ brew install vimHomebrew will install all the necessary dependencies too, so you won’t have to worry about a thing.
If you already have Homebrew installed, and you have installed Vim in the past, the preceding command will produce an error. You may want to make sure you have the latest version of Vim, though, so, run the following command:
$ brew upgrade vimYou should now be ready to enjoy Vim; let’s try opening it with the following command:
$ vimWelcome to Vim:
Figure 1.4 – Vim on macOS (installed via Homebrew)
Let’s move on to the next section about downloading a .dmgimage.
Navigate to https://github.com/macvim-dev/macvim/releases/latest and download MacVim.dmg.
Open MacVim.dmg, and then drag the Vim icon into the Applications directory, as can be seen in the following screenshot:
Figure 1.5 – The MacVim installation screen – drag and drop MacVim into the Applications folder
Depending on the security settings of your Mac, you might be greeted by an error when navigating to the Applications folder and trying to open the MacVim app, as demonstrated in the following screenshot:
Figure 1.6 – The default “unidentified developer” prompt
Open your Applications folder, find MacVim, right-click the icon, and select Open. The following prompt will pop up:
Figure 1.7 – The “unidentified developer” prompt, which you can get by right-clicking and selecting Open
Now, hit Open, and MacVim can be opened as usual from now on. Give it a shot:
Figure 1.8 – MacVim on macOS
Windows provides two primary routes for using Vim: setting up Cygwin and providing a more Unix-like command-line experience, or installing gVim – a graphical version of Vim (which supports working with cmd.exe on Windows). I recommend installing both and picking your favorite: gVim feels slightly more at home on Windows (and it is easier to install), while Cygwin might feel more at home if you’re used to the Unix shell.
Cygwin is a Unix-like environment and a command-line interface for Windows. It aims to bring a powerful Unix shell and supporting tools to a Windows machine.
Windows Subsystem for Linux (WSL)
WSL is a feature in Windows 10+ that allows you to run a Linux environment directly in Windows. While I personally haven’t had experience with WSL, it’s widely praised as a fast, user-friendly, and reliable way to access the Linux command line and tools on Windows. It could be a better alternative to Cygwin as it continues to be developed. You can read more about WSL at https://learn.microsoft.com/windows/wsl.
To begin the installation process, navigate to https://cygwin.com/install.html and download either setup-x86_64.exe or setup-x86.exe, depending on the version of Windows you’re using (64-bit or 32-bit respectively).
How many bits are in your system?
If you’re not sure whether your system is 32-bit or 64-bit, you can open Control Panel | System and Security | System, and look at System type. For example, my Windows machine shows System type: 64-bit Operating System, x64-based processor.
Open the executable file, and you will be greeted by the following Cygwin installation window:
Figure 1.9 – The Cygwin Setup screen on Windows
Hit Next > a few times, proceeding with the default settings:
Download source: Install from InternetRoot directory: C:\cygwin64 (or a recommended default)Install for: all usersLocal package directory:C:\Downloads (or a recommended default)Internet connection:Use System Proxy SettingsDownload site:http://cygwin.mirror.constant.com (or any available option)After this, you will be greeted with the Select Packages screen. Here, we want to select the vim, gvim, and vim-doc packages. The easiest way to do this is to type vim in a search box, expand the All |Editors category, and click on the arrow-looking icons next to the desired packages, as demonstrated in the following screenshot:
Figure 1.10 – Cygwin package selection screen – note that gvim, vim, and vim-doc are marked to be installed, as seen in the New column
The preceding screenshot shows version 8.2.4372-2. This is the latest version available at the moment of writing this chapter, July 2023. At this time, the latest version of Vim is 9.0, which introduces Vim9script (you can learn more about version differences in the A brief history lesson section).
Use Cygwin to compile Vim
If you’d like to have the latest version of Vim, I recommend using Cygwin to compile Vim from its official Git repository. After installing Cygwin, you should visit the Setting up on Linux section we covered earlier in this chapter for the instructions. If you’d like to do that, you’ll want to install git and make utilities in Cygwin.
You might need additional utilities
You may want to install curl from under the Net category, and git from under the Devel category, as we’ll be using both in Chapter 3. It might also be helpful to install dos2unix from under the Utils category, which is a utility used for converting Windows-style line endings to Linux-style line endings (something you might run into once in a while).
Hit Next > two more times to proceed, which will begin the installation. The installation will take some time, and now would be a great moment to prematurely congratulate yourself with some coffee!
You might get a few post-install script errors, which you can safely dismiss (unless you see any errors related to Vim – then, Google is your friend: search for an error text and try to find a solution).
Hit Next > a few more times, proceeding with the defaults:
Create icon on DesktopAdd icon to Start MenuCongratulations – you now have Cygwin installed with Vim!
Installing Cygwin packages
If you ever need to install additional packages in Cygwin, just rerun the installer while selecting the packages you want.
Open Cygwin – the program will be called Cygwin64 Terminal or Cygwin Terminal, depending on the version of your system, as can be seen in the following screenshot:
Figure 1.11 – The Cygwin64 Terminal application icon
Open it! You will see the following prompt, which will be familiar to Linux users:
Figure 1.12 – The Cygwin command prompt for the user called ruslan and the RUSLAN-DESKTOP machine
Cygwin supports all of the Unix-style commands we will be using in this book. This book will also say whether any commands need to be changed to work with Cygwin.
Type vim and hit Enter to start Vim, as demonstrated in the following screenshot:
Figure 1.13 – Vim is installed through Cygwin (note Modified by <[email protected]>)
Cygwin is a way to get a Linux-like shell experience on Windows, meaning you’ll have to follow Linux-specific instructions throughout this book if you decide to use Cygwin.
You’ll also want to be careful with Windows-style line endings versus Linux-style line endings, as Windows and Linux treat line endings differently. If you run into an odd issue with Vim complaining about ^M characters it is unable to recognize, run the dos2unix utility on the offending file to resolve the issue.
You can read more about the graphical version of Vim in the Vanilla Vim versus gVim section later in this chapter.
As it always is with Windows, the process is slightly more visual. Navigate to github.com/vim/vim-win32-installer in your browser and download an executable installer. At the moment of writing this chapter, July 2023, the latest available version of Gvim is 9.0.
You can use winget instead
Alternatively, if you’re familiar with the winget tool, you can run winget install -e --id vim.vim (substitute with vim.vim.nightly if you’d like to live on the bleeding edge).
Open the executable and follow the prompts on the screen, as demonstrated by the following screenshot:
Figure 1.14 – gVim 9.0 setup welcome screen
Let’s go ahead and hit Next, then I Agree until we arrive at the Installation Options screen. We’re happy with most of the default options gVim has to offer, except that you might want to enable