Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Mastering Vim

You're reading from  Mastering Vim

Product type Book
Published in Nov 2018
Publisher Packt
ISBN-13 9781789341096
Pages 330 pages
Edition 1st Edition
Languages
Author (1):
Ruslan Osipov Ruslan Osipov
Profile icon Ruslan Osipov

Table of Contents (12) Chapters

Preface Getting Started Advanced Editing and Navigation Follow the Leader - Plugin Management Understanding the Text Build, Test, and Execute Refactoring Code with Regex and Macros Making Vim Your Own Transcending the Mundane with Vimscript Neovim Where to Go from Here Other Books You May Enjoy

Advanced Editing and Navigation

Throughout this chapter, you will get a lot more comfortable using Vim in your day-to-day tasks. You will be working with a Python code base, which should provide you with a set of real-life scenarios for working with code. If you have a project of your own handy, you can choose to try out the lessons taught in this chapter using your own project files; however, you might find that not every scenario applies to your code base.

The following topics will be covered in this chapter:

  • A quick-and-dirty way of installing Vim plugins
  • Keeping your workspace organized when working with multiple or long files using buffers, windows, tabs, and folds
  • Navigating complex file trees without leaving Vim with Netrw, NERDTree, Vinegar, or CtrlP
  • Advanced navigation throughout a file, and covering more types of text objects: using grep and ack to look for things across...

Technical requirements

Installing plugins

This chapter will start by introducing Vim plugins. Plugin management is a rather broad subject (and it's covered in Chapter 3, Follow the Leader - Plugin Management, as well), but we're starting out with just a few plugins, so we won't have to worry ourselves with that topic yet.

First, let's go through the one-time set up:

  1. You'll need to create a directory to store plugins. Execute the following on the command line:
$ mkdir -p ~/.vim/pack/plugins/start
If you're using GVim under Windows, you'll have to create the vimfiles directory under your user folder (usually C:\Users\<username>), and then create pack\plugins\start folders inside of it.
  1. You'll want to tell Vim to load documentation for each plugin, as it doesn't do so automatically. For that, add the following lines to your ~/.vimrc file:
    packloadall...

Organizing workspace

So far, we've only worked with a single file in Vim. When working with code, you usually have to work with multiple files at once, switching back and forth, making edits across multiple files, and looking up certain bits somewhere else. Luckily, Vim provides an extensive way to deal with many files:

  • Buffers are the way Vim internally represents files; they allow you to switch between multiple files quickly
  • Windows organize the workspace by displaying multiple files next to each other
  • Tabs are a collection of windows
  • Folds allow you to hide and expand certain portions of files, making large files easier to navigate

Here's a screenshot illustrating the preceding points:

Let's understand the content in the screenshot:

  • Multiple files (labeled farm.py, animals/cat.py, and animal_farm.py) are open as windows
  • The bar at the top (listing 3 farm...

Navigating file trees

Since software projects contain a lot of files and directories, finding a way to traverse and display these using Vim comes in handy. This section will cover five different ways that you can navigate your files: using the built-in Netrw file manager or using the :e command with the wildemenu option enabled, as well as using the NERDTree, Vinegar, and CtrlP plugins. All of these provide different ways to interact with files and can be mixed and matched.

Netrw

Netrw is a built-in file manager in Vim (if we want to get technical, it's a plugin that ships with Vim). It allows you to browse directories and functions, similar to any other file manager you've worked with in your favorite OS.

Use :Ex...

Navigating text

We've covered some basic movements (by characters, words, and paragraphs), but Vim supports a lot more options for navigation.

Check the following if you want some movement within the current line:

  • As you already know, h and l move the cursor left and right, respectively
  • t (until) followed by a character allows you to search the line for that character and place the cursor before the character, while T allows you to search backward
  • f (find) followed by a character allows you to search the current line for that character and move the cursor to the character, while F allows you to search backward
  • _ takes you to the beginning of the line and $ takes you to the end of the line
A word consists of numbers, letters, and underscores. A WORD consists of any characters except for whitespace (like spaces, tabs, or newlines). This distinction helps with more precise...

Copying and pasting with registers

You can copy text by using the y (yank) command, followed by a movement or a text object. You can also hit y from a visual mode when you have selected some text.

In addition to all of the standard movement, you can use yy to yank the contents of the current line.

Let's yank the following piece of code by typing ye (yank until the end of the word):

This will copy animal_kind into our default register. Now, place the cursor where you want the text to appear (the text is inserted after the cursor):

To paste the code, hit p:

The delete and change operators also yanks content so that you can paste it later. Oh, and you can prefix the paste command with a number, in case you ever want to duplicate something multiple times.

Where do the registers...

Summary

You now know how to navigate core concepts Vim operates by: using buffers to represent files, utilizing split windows, and using tabs to organize multiple windows. You've also learned how to use folds to make navigating large files more manageable.

You now should be more confident getting through a large code base by navigating files with plugins such as Netrw, NERDTree, Vinegar, and CtrlP. Oh, and this chapter taught you a quick (even though it's a slightly manual) way to install said plugins.

This chapter covered new movement operations, text objects, ways to quickly dart into insert mode, and how to make even fancier jumps throughout the file using the EasyMotion plugin. We've also dipped into search functionality, searching both within a single file and across the whole code base. You get a bonus point for trying out the ack plugin.

Finally, this chapter...

lock icon The rest of the chapter is locked
You have been reading a chapter from
Mastering Vim
Published in: Nov 2018 Publisher: Packt ISBN-13: 9781789341096
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}