Practical Vim< /a> vit Practical Vim< /a> Note that the styling for a Visual selection is the... development I thought I knew Vim, but Practical" name="description"/>
Tải bản đầy đủ - 0 (trang)
Tip 54. Jump Between Matching Parentheses

Tip 54. Jump Between Matching Parentheses

Tải bản đầy đủ - 0trang

Chapter 8. Navigate Inside Files with Motions

• 128

To see how we might use % in practice, let’s use this short extract of Ruby:


cities = %w{London Berlin New\ York}

Suppose that we want to switch from the %w{London Berlin New\ York} syntax to

a regular list definition: ["London", "Berlin", "New York"]. We’ll have to switch opening

and closing curly braces to square brackets. You might think that this would

be a perfect occasion to use the % motion. You’d be right, but there’s a gotcha!

Let’s say that we start off by positioning our cursor on the opening curly brace

and then we press r[ to change it to an opening square bracket. Now we’ve

got this strange-looking construct: [London Berlin New\ York}. The % command only

works on well-formed matching parentheses, so we can’t use it to jump to

the closing } character.

The trick here is to use the % command before making any changes. When

we use the % command, Vim automatically sets a mark for the location from

which we jumped. We can snap back to it by pressing `` . Here’s a partial

solution for our example refactoring:


Buffer Contents


cities = %w{London Berlin New\ York}


cities = {London Berlin New\ York}


cities = {London Berlin New\ York}


cities = {London Berlin New\ York]


cities = {London Berlin New\ York]


cities = [London Berlin New\ York]

Note that in this case, the command would work just as well as the ``

motion (see Tip 55, on page 131). The surround.vim plugin provides commands

that would make this task even easier. Find out more in Surround.vim, on

page 129.

Jump Between Matching Keywords

Vim ships with a plugin called matchit, which enhances the functionality of

the % command. When this plugin is enabled, the % command can jump

between matching pairs of keywords. For example, in an HTML file, the %

command would jump between opening and closing tags. In a Ruby file, it

would jump between class/end, def/end, and if/end pairs.

Even though matchit ships with the Vim distribution, it’s not enabled by default.

This minimal vimrc would make Vim autoload the matchit plugin on startup:

report erratum • discuss

Jump Between Matching Parentheses

• 129

set nocompatible

filetype plugin on

runtime macros/matchit.vim

The enhancements provided by this plugin are very useful, so I’d recommend

enabling it. Consult :h matchit-install for more details.


One of my favorite plugins is surround.vim by Tim Pope,a which makes wrapping a

selection with a pair of delimiters easy. For example, we could put the words New

York in quote marks:


Buffer Contents


cities = ["London", "Berlin", New York]


cities = ["London", "Berlin", New York]


cities = ["London", "Berlin", "New York"]

The S" command is provided by surround.vim, and it can be read as “Surround the

selection with a pair of double quote marks.” We could just as easily use S) or S} if

we wanted to wrap the selection with opening and closing parentheses or braces.

We can also use surround.vim to change existing delimiters. For example, we could

change {London} to [London] with the cs}] command, which can be read as “Change

surrounding {} braces to [] brackets.” Or we could go the other way with the cs]}

command. It’s a powerful plugin—check it out.


report erratum • discuss


Navigate Between Files with Jumps

As we learned in the previous chapter, motions allow us to move around

within a file. Jumps are similar, except that they can also move us between

different files. Vim provides a couple of commands that turn keywords in the

document into a wormhole, allowing us to jump quickly from one part of our

codebase to another. That might seem disorienting at first, but Vim always

traces our path by leaving a trail that we can easily follow to get back to where

we came from.

Tip 55

Traverse the Jump List

Vim records our location before and after making a jump and provides a couple

of commands for retracing our steps.

In web browsers, we’re used to using the back button to return to pages that

we visited earlier. Vim provides a similar feature by way of the jump list: the

command is like the back button, while the complementary command is like the forward button. These commands allow us to traverse Vim’s

jump list, but what exactly is a jump?

Let’s start by making this distinction: motions move around within a file,

whereas jumps can move between files (although we’ll soon see that some

motions are also classified as jumps). We can inspect the contents of the jump

list by running this command:


jump line





col file/text



report erratum • discuss

Chapter 9. Navigate Between Files with Jumps





• 132




Press Enter or type command to continue

Any command that changes the active file for the current window can be

described as a jump. In the jump list, Vim records the cursor location before

and after running such a command. For example, if we run the :edit command

to open a new file (see Tip 41, on page 93), then we can use the and

commands to jump back and forth between the two files.

Moving directly to a line number with [count]G counts as a jump, but moving

up or down one line at a time does not. The sentence-wise and paragraphwise motions are jumps, but the character-wise and word-wise motions are

not. As a rule of thumb, we could say that long-range motions may be classified as a jump, but short-range motions are just motions.

This table summarizes a selection of jumps:




Jump to line number

//pattern /?pattern / n / N

Jump to next/previous occurrence of pattern


Jump to matching parenthesis


Jump to start of previous/next sentence


Jump to start of previous/next paragraph


Jump to top/middle/bottom of screen


Jump to file name under the cursor

Jump to definition of keyword under the cursor

’{mark} / `{mark}

Jump to a mark

The and commands themselves are never treated as a motion.

This means that we can’t use them to extend the reach of a Visual mode

selection, nor can we use them in Operator-Pending mode. I tend to think of

the jump list as a breadcrumb trail that makes it easy to retrace my steps

through the files that I’ve visited during the course of an editing session.

Vim can maintain multiple jump lists at the same time. In fact, each separate

window has its own jump list. If we’re using split windows or multiple tab

pages, then the and commands will always be scoped to the jump

list of the active window.

report erratum • discuss

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tip 54. Jump Between Matching Parentheses

Tải bản đầy đủ ngay(0 tr)