Vim's `ci(` Motion


Vim’s ci( motion is a powerful tool for quickly changing the content of parentheses. It allows you to delete the text within the parentheses and enter insert mode, ready to type the new content. This command works not only for round parentheses () but also for curly braces {}, square brackets [], and angle brackets <>. It’s a significant time saver compared to manually deleting and navigating.

Here’s a breakdown of how ci( works:

How to Use It

  1. Navigate to the parentheses: Move your cursor anywhere on the opening or closing parenthesis.
  2. Type ci(: The text inside the parentheses will be deleted, and you’ll enter insert mode.
  3. Type the new content: Enter the text you want to replace the original content with.
  4. Press Esc: This will exit insert mode and save your changes.

Examples

Let’s say you have the following line of JavaScript code:

let message = greet("world");

You want to change “world” to “user”. Follow these steps:

  1. Move your cursor to either parenthesis of ("world").
  2. Type ci(. The line will change to: let message = greet(""); with the cursor positioned inside the empty parentheses.
  3. Type user.
  4. Press Esc. The final line will be: let message = greet("user");

Example in Go:

Imagine you have this Go code:

fmt.Println(reflect.TypeOf("hello"))

You want to change the type from “hello” (string) to reflect.TypeOf(10) (int).

  1. Move your cursor to either parenthesis of ("hello").
  2. Type ci(. The line will change to fmt.Println(reflect.TypeOf()).
  3. Type 10.
  4. Press Esc. The final line will be: fmt.Println(reflect.TypeOf(10))

Beyond Parentheses

As mentioned earlier, this works with other delimiters as well. For instance, ci{ will change the content inside curly braces, ci[, inside square brackets, and ci<, inside angle brackets. This flexibility makes ci( incredibly useful for a wide range of editing tasks in various programming languages and file formats.