Mastering Vim
eBook - ePub

Mastering Vim

Build a software development environment with Vim and Neovim

Ruslan Osipov

Share book
  1. 330 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Mastering Vim

Build a software development environment with Vim and Neovim

Ruslan Osipov

Book details
Book preview
Table of contents
Citations

About This Book

Mastering Vim, reviewed by Bram Moolenaar, the creator of Vim, covers usage of Vim and Neovim, showcases relevant plugins, and teaches Vimscript

Key Features

  • Expert Vim and Vimscript techniques to work with Python and other development environment
  • Accomplish end-to-end software development tasks with Neovim and Vim plugins
  • Understand best practices for various facets of projects like version control, building, and testing

Book Description

Vim is a ubiquitous text editor that can be used for all programming languages. It has an extensive plugin system and integrates with many tools. Vim offers an extensible and customizable development environment for programmers, making it one of the most popular text editors in the world.

Mastering Vim begins with explaining how the Vim editor will help you build applications efficiently. With the fundamentals of Vim, you will be taken through the Vim philosophy. As you make your way through the chapters, you will learn about advanced movement, text operations, and how Vim can be used as a Python (or any other language for that matter) IDE. The book will then cover essential tasks, such as refactoring, debugging, building, testing, and working with a version control system, as well as plugin configuration and management. In the concluding chapters, you will be introduced to additional mindset guidelines, learn to personalize your Vim experience, and go above and beyond with Vimscript.

By the end of this book, you will be sufficiently confident to make Vim (or its fork, Neovim) your first choice when writing applications in Python and other programming languages.

What you will learn

  • Get the most recent Vim, GVim, and Neovim versions installed
  • Become efficient at navigating and editing text
  • Uncover niche Vim plugins and pick the best ones
  • Discover multiple ways of organizing plugins
  • Explore and tailor Vim UI to fit your needs
  • Organize and maintain Vim configuration across environments
  • Write scripts to complement your workflow using Vimscript

Who this book is for

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.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on ā€œCancel Subscriptionā€ - itā€™s as simple as that. After you cancel, your membership will stay active for the remainder of the time youā€™ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlegoā€™s features. The only differences are the price and subscription period: With the annual plan youā€™ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, weā€™ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Mastering Vim an online PDF/ePUB?
Yes, you can access Mastering Vim by Ruslan Osipov in PDF and/or ePUB format, as well as other popular books in Computer Science & Computer Science General. We have over one million books available in our catalogue for you to explore.

Information

Year
2018
ISBN
9781789344530
Edition
1

Transcending the Mundane with Vimscript

This chapter will cover Vimscript in all its glory. We will go into quite a bit of detail, but since we only have so many pages the coverage will be somewhat spotty. Hopefully, this chapter will get you interested in Vimscript enough to start your own research, and maybe you can use it as a reference as you build your early plugins and scripts. In this chapter, we will look at the following:
  • The basic syntax, from declaring variables to using lambda expressions
  • Style guides, and how to keep sane when developing in Vimscript
  • A sample plugin from start to finishā€”from the first line to publishing it online

Technical requirements

This chapter walks you through learning Vimscript by using numerous examples. All the examples are available on GitHub: https://github.com/PacktPublishing/Mastering-Vim/tree/master/Chapter08. You can create the scripts on your own as we're working through this chapter, or download the files from the repository to play around with.

Why Vimscript?

You've already encountered Vimscript when you worked on your .vimrc. What you may not have known is that Vimscript is actually a Turing-complete scripting languageā€”there's no limit to what you can do. So far, you've used it to set variables and perform a few comparison operations, but it can do so much more!
You will learn how Vimscript not only helps you understand Vim configuration better, but lets you solve text editing problems you encounter by writing functions and plugins.
It's pretty awesome.

How to execute Vimscript

Vimscript is made up of commands you run in command-line mode, and really is just a sequence of Vim commands in a file. You can always execute Vimscript by running each command in command mode (the one you prefix with :), or by executing the file with commands using a :source command. Historically, Vim scripts have a .vim extension.
As you're following along with this section, you may want to create *.vim files to experiment in. You can execute the files by running this:
:source <filename>
A much shorter version of that is this:
:so %
Here, :so is a short version of :source, and % refers to the currently open file.
For example, I just created a variables.vim file to play around with Vim's variables. I could execute its contents with :so %:
Alternatively, I could run each command in command mode. For example, if I wanted to print the contents of a variable, g:animal, I would run the following:
:echo g:animal
I will do just that, as in, print cat into our status line:
Normally, I run longer scripts with :so %, and perform debugging operations through command-line mode (:).
Additionally, if you're entering commands in command-line mode, you'll stay in command-line mode if you enter a function or a flow control operator (such as if, while, or for):
In this example, I did not have to type : on every line. Additionally, each line gets executed as you hit Enter (as you can see by this is probably unix being printed on the screen).

Learning the syntax

Let's take a lightning-fast deep dive into Vimscript's syntax.
This section assumes you're comfortable with at least one programming language, conditional statements and loops in particular. If that's not the case, you will most certainly be better off finding a more extensive tutorial. Vimscript deserves its own book, and Steve Losh wrote just that: Learn Vimscript the Hard Way is easily the best Vimscript tutorial available (and it's free on the web!).

Setting variables

You've already discovered some basics of Vimscript syntax. To set internal Vim options, you use the set keyword:
set background=dark
To assign a value to a non-internal variable, use the let keyword:
let animal_name = 'Miss Cattington'
Vimscript doesn't have explicit booleans, so 1 is treated as true and 0 as false:
let is_cat = 1
Since we're assigning variables, let's talk about scopes. Vim handles variable and function scopes with prefixes, like so:
let g:animal_name = 'Miss Cattington'
let w:is_cat=1
Each letter has a unique meaning, in particular the following:
  • g: global scope (default if scope is not specified)
  • v: global defined by Vim
  • l: local scope (default within a function if scope is not specified)
  • b: current buffer
  • w: current window
  • t: current tab
  • s: local to a :source'd Vim script
  • a: function argument
In this example, g:animal_name is a global variable (it could also be written as let animal_name='Miss Cattington', but explicit scope declarations are always better), and w:is_cat is a window scope variable.
As you might remember, you can also set registers with let. For example, if you wanted to set register a to hold cats are weird, you could do this:
let @a = 'cats are weird'
You can also access Vim options (the ones you can change with set) by prefixing the variable with &...

Table of contents