How to select between brackets (or quotes or ...) in Vim?

VimQuotesBracketsParenthesesYank

Vim Problem Overview


I'm sure there used to be a plugin for this kinda stuff, but now that I need it, I can't seem to find it (naturally), so I'll just ask nice and simple.

What is the easiest way to select between brackets, or quotes, or generally a list of matching characters?

   write ( *, '(a)' ) 'Computed solution coefficients:'

For example, here I'd like to select (a), or Computed solution coefficients:.

I'm not interested in multiline, just cases which occur on one line.

Vim Solutions


Solution 1 - Vim

To select between the single quotes I usually do a vi' ("select inner single quotes").

Inside a parenthesis block, I use vib ("select inner block")

Inside a curly braces block you can use viB ("capital B")

To make the selections "inclusive" (select also the quotes, parenthesis or braces) you can use a instead of i.

You can read more about the Text object selections on the manual, or :help text-objects within vim.

Solution 2 - Vim

Use whatever navigation key you want to get inside the parentheses, then you can use either yi( or yi) to copy everything within the matching parens. This also works with square brackets (e.g. yi]) and curly braces. In addition to y, you can also delete or change text (e.g. ci), di]).

I tried this with double and single-quotes and it appears to work there as well. For your data, I do:

write (*, '(a)') 'Computed solution coefficients:'

Move cursor to the C, then type yi'. Move the cursor to a blank line, hit p, and get

Computed solution coefficients:

As CMS noted, this works for visual mode selection as well - just use vi), vi}, vi', etc.

Solution 3 - Vim

This method of selection is built-in and well covered in the Vim help. It covers XML tags and more.

See :help text-objects.

Solution 4 - Vim

For selecting within single quotes use vi'.

For selecting within parenthesis use vi(.

Solution 5 - Vim

Use arrows or hjkl to get to one of the bracketing expressions, then v to select visual (i.e. selecting) mode, then % to jump to the other bracket.

Solution 6 - Vim

Write a Vim function in .vimrc using the searchpair built-in function:

searchpair({start}, {middle}, {end} [, {flags} [, {skip}
			[, {stopline} [, {timeout}]]]])
	Search for the match of a nested start-end pair.  This can be
	used to find the "endif" that matches an "if", while other
	if/endif pairs in between are ignored.
    [...]

(http://vimdoc.sourceforge.net/htmldoc/eval.html)

Solution 7 - Vim

I would add a detail to the most voted answer:

If you're using gvim and want to copy to the clipboard, use

"+<command>

To copy all the content between brackets (or parens or curly brackets)

For example: "+yi} will copy to the clipboard all the content between the curly brackets your cursor is.

Solution 8 - Vim

I've made a plugin vim-textobj-quotes: https://github.com/beloglazov/vim-textobj-quotes

It provides text objects for the closest pairs of quotes of any type. Using only iq or aq it allows you to operate on the content of single ('), double ("), or back (`) quotes that currently surround the cursor, are in front of the cursor, or behind (in that order of preference). In other words, it jumps forward or backwards when needed to reach the quotes.

It's easier to understand by looking at examples (the cursor is shown with |):

  1. Before: foo '1, |2, 3' bar; after pressing diq: foo '|' bar
  2. Before: foo| '1, 2, 3' bar; after pressing diq: foo '|' bar
  3. Before: foo '1, 2, 3' |bar; after pressing diq: foo '|' bar
  4. Before: foo '1, |2, 3' bar; after pressing daq: foo | bar
  5. Before: foo| '1, 2, 3' bar; after pressing daq: foo | bar
  6. Before: foo '1, 2, 3' |bar; after pressing daq: foo | bar

The examples above are given for single quotes, the plugin works exactly the same way for double (") and back (`) quotes.

You can also use any other operators: ciq, diq, yiq, viq, etc.

Please have a look at the github page linked above for more details.

Solution 9 - Vim

I wanted to add to the already good answers. I came here looking for a way to change the text inside of html brackets, so I want to provide an answer for anyone else that is also looking for that.

You might think ci< would work, but actually that only works if you are inside one of the tags themselves:

<would work inside here> But not here </would work inside here>

What I wanted was to change the text between the html tags themselves:

<div>change me</div>

What I wanted was "change inner tag": cit

Thank you to the other answer that mentioned the documentation (:help text-objects) which is how I found what I was looking for.

Solution 10 - Vim

A simple keymap in vim would solve this issue. map viq F”lvf”hh This above command maps viq to the keys to search between quotes. Replace " with any character and create your keymaps. Stick this in vimrc during startup and you should be able to use it everytime.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRookView Question on Stackoverflow
Solution 1 - VimChristian C. SalvadóView Answer on Stackoverflow
Solution 2 - VimTim WhitcombView Answer on Stackoverflow
Solution 3 - VimmichaelView Answer on Stackoverflow
Solution 4 - VimCanopusView Answer on Stackoverflow
Solution 5 - VimStoborView Answer on Stackoverflow
Solution 6 - VimAdrian PanasiukView Answer on Stackoverflow
Solution 7 - VimyuriplocView Answer on Stackoverflow
Solution 8 - VimAnton BeloglazovView Answer on Stackoverflow
Solution 9 - VimchillpenguinView Answer on Stackoverflow
Solution 10 - VimKrishnaView Answer on Stackoverflow