0

I would like to create two commands for vim:

1 -

example code:

var a = "My string to be replaced";

So imagine that the cursor is on top of any letter inside the ". I would use the command ds" and it would do the equivalent of

T" dt" i

so it would delete everything inside the string and enter in insert mode automatically. If it is a function it would use the character used to call the command inside it, so dt" would delete everything inside " and dt'would delete everything inside ''.

2 -

example code:

class SomeClassNameHere {

so now imagine the cursor is somewhere on top of 'Name' and I would like to use a command that would put the cursor on the first uppercase character on the left of the current position and after that delete everything till the first uppercase character on the right (non including the character itself) and enter insert mode. This command is similar to the first one but it will look for an uppercase letter which probably is means the commands will need to use some regular expression like [A-Z]. After the command the code would look like that:

class SomeClassHere {

I would like those be some kind of map that I can add to my .vimrc file.

2
  • FYI, you can currently do di' and di" to delete everything inside single and double quotes respectively. It will not enter insert mode automatically though. Commented Jan 2, 2016 at 6:02
  • 2
    Seems like by ds" you actually mean ci". Commented Jan 2, 2016 at 6:36

2 Answers 2

2

The first operation already exists as a text-object, it's i" and you are supposed to use it like this [operator][text-object]:

ci"

There's also i', i(, i{, etc. See :help text-objects.

The second operation could be described like this:

?[A-Z]<CR>
c/[A-Z]<CR>

and mapped like this:

nnoremap <key> ?[A-Z]<CR>c/[A-Z]<CR>

It would be even better to turn that in a proper text-object:

xnoremap iu ?[A-Z]<CR>o/[A-Z]<CR>h
onoremap iu :normal viu<CR>

that you could use like this:

ciu
diu
yiu
viu
Sign up to request clarification or add additional context in comments.

Comments

0

Here is an improved version of romainl's answer:

xnoremap iu ?\u<cr>o/.\ze\([^a-z_]\<bar>$\)<cr>:<c-u>noh<cr>gv
onoremap iu :normal viu<cr>

It adds the ability to select or delete the last uppercase word, which was not possible, and hides the highlighting when done.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.