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

前端 未结 9 2014
时光说笑
时光说笑 2020-12-12 08:55

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 th

相关标签:
9条回答
  • 2020-12-12 09:37

    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.

    0 讨论(0)
  • 2020-12-12 09:43

    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.

    0 讨论(0)
  • 2020-12-12 09:44

    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.

    0 讨论(0)
提交回复
热议问题