1

Been using Vim for a few weeks now and I find myself constantly using ci" or ci( to change whats enclosed in "" or (). I do also find myself wanting to append inside of the brackets e.g.:

If I have:

if (name == "Frank") {

I want to be anywhere on that line and append && lastName == "Castle" to my condition inside the (). To end up with:

if (name == "Frank" & lastName == "Castle") {

OR I want to append Castle to what's inside the "". To end up with:

if (name == "Frank Castle") {

Obviously ai" and ai( doesn't work, since it puts you in append/insert mode and you then just type i"...

Anyone know how to do that, or if it's possible?

Cheers,

2 Answers 2

3

There's not a command that follows the ci... structure to do that. You can add a remap to do it (see below), or use core vim commands f and t (and their counterparts F and T) motions to get the cursor to where you want, and then switch to insert mode (with i or a) to effect the change (see even further below).

Adding a remap to do it

Here's a mapping to do it:

nnoremap <LEADER>ci" ci"<C-r>"

The way it works: The ctrl + r + " pastes from the unnamed register once in insert mode - and since the unnamed register will contain whatever was between the double quotes it just pastes it back in and leaves you in insert mode to append your string:

Showing how the map works, step by step:

if (name == "Frank") {

ci" (this leaves you in insert mode)

if (name == "") {

ctrl+r + " (this pastes the content back, and leaves you in insert mode)

if (name == "Frank") {

Typing your addition && lastName == "Castle":

if (name == "Frank && lastName == "Castle") {

Using core vim (i.e. without a remap)

Relevant stuff from the help:

f{char}    To [count]'th occurrence of {char} to the right.  The
           cursor is placed on {char}... 

F{char}    To the [count]'th occurrence of {char} to the left.
           The cursor is placed on {char}...

t{char}    Till before [count]'th occurrence of {char} to the
           right.  The cursor is placed on the character left of
           {char}...
            
T{char}    Till after [count]'th occurrence of {char} to the
           left.  The cursor is placed on the character right of
           {char}...

;          Repeat latest f, t, F or T [count] times.

,          Repeat latest f, t, F or T in opposite direction
           [count] times.

In the following, the ^ shows where the cursor is.

First case

if (name == "Frank") {
     ^

Pressing f):

if (name == "Frank") {
                   ^

Typing i && lastName == "Castle":

if (name == "Frank" && lastName == "Castle") {

Second case

if (name == "Frank") {
  ^                    

Pressing t) or f"; or 2f":

if (name == "Frank") {
                  ^                    

Typing i Castle:

if (name == "Frank Castle") {
                         ^                    

Going backwards with F

if (name == "Frank") {
                     ^                    

Pressing F":

if (name == "Frank") {
                  ^

Going backwards with T

if (name == "Frank") {
                     ^                    

Pressing T"; or 2T":

if (name == "Frank") {
             ^
3
  • 1
    Thanks for answering. Yeah I’ve been doing exactly that. Just thought that there may have been something since the whole ci” works super well from anywhere on the line. Maybe it’s time I do some form of key map or something to handle this hehe.
    – Albert
    Dec 17, 2021 at 10:24
  • @Albert I figured out a mapping for it (updated answer)
    – mattb
    Dec 17, 2021 at 10:53
  • 1
    Hi @mattb, this is great! I have tried it and will be using this going forward :) Thanks for your time!
    – Albert
    Dec 17, 2021 at 18:09
2

To help someone else looking for this, below is my complete mapping.

Note: ^R is entered by pressing Ctrl + v, then Ctrl + r which turns it into ^R.

nnoremap <LEADER>ci" ci"^R"<space>
nnoremap <LEADER>ci' ci'^R"<space>
nnoremap <LEADER>ci( ci(^R"<space>
nnoremap <LEADER>ci) ci)^R"<space>
nnoremap <LEADER>ci{ ci{^R"<space>
nnoremap <LEADER>ci} ci}^R"<space>
nnoremap <LEADER>ci[ ci[^R"<space>
nnoremap <LEADER>ci] ci]^R"<space>
nnoremap <LEADER>ci< ci<^R"<space>
nnoremap <LEADER>ci> ci>^R"<space>
nnoremap <LEADER>ci` ci`^R"<space>

Cheers,

1
  • 2
    You should be able to use <C-r>
    – D. Ben Knoble
    Dec 17, 2021 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.