9

I use an example of javascript code:

1        var a = 1;
2        var b = |{};

The cursor is at the position of |( on the character "{" when the cursor shape is block), I find out that I want a should be assigned an object, so I want to modify it like below:

1        var a = {};

So I want to know when cursor is on "{", how to quickly delete all the contents before "{" until the blank after the "=" sign at line 1?

In other normal editors, I'll just press backspace key to delete the code. But I don't how to achieve this in vim.

  • you can also press i and use the backspace key. No body will shoot you for that. I think. – elyashiv Mar 13 '15 at 9:46
  • 1
    but backspace won't delete character of above line – Aaron Shen Mar 13 '15 at 9:56
  • 3
    @AaronShen That depends on your 'backspace' setting. If it includes eol then backspace will delete line breaks. You'll also need start to be included in the setting. (See :help backspace). Don't use this method though! There are several much faster methods detailed in the answers below. – Rich Mar 13 '15 at 10:17
20

Not sure if I totally understand the question, but if you wanted to delete until you only have var a = {};, you could, from the cursor position, use d?1Enter

d takes a motion, and you can give it a search pattern with / for a forward search or ? for a backward search.

Here's a small gif demonstrating this:

enter image description here

  • 1
    It's probably worth mentioning that you need to hit Enter after typing d?1. – Rich Mar 13 '15 at 10:08
  • @Rich indeed thanks, I have updated the answer to include that. – akshay Mar 13 '15 at 10:10
  • 1
    What font is that shell using? – Chris Burt-Brown Mar 13 '15 at 12:24
  • @ChrisBurt-Brown: Terminus – akshay Mar 13 '15 at 15:24
10

If you are using vim (and not classic vi), then in your example you could do vkd (or vkx).

  • v will put it into 'visual' (select) mode
  • k will go up one line to put the cursor on the '1', selecting the portion you want deleted
  • d or x will delete the selection
5

Delete Up

For this specific example, dgk is one of the quickest ways to do so.

The d command, as Akshay explained, can be followed by a motion, deleting all the characters between where the cursor begins, and where the motion takes it to.

In your code, you want to to delete everything between the cursor position and the same column one row further up, so moving the cursor upwards with the gk motion (which when used with an operator is non-linewise) will achieve your goal.

EDIT: BobH points out in the comments that this method only works when 'wrap' is set. As such, unless you always have wrap set, you're probably better off using dvk or vkd, as suggested by the other answers on this page.

Delete Words Backwards

You could also use: d5b or d4B, which will delete 5 words or 4 WORDs backwards, respectively. (1; is one WORD, but two regular words).

  • why dk will delete both current line and above line completely – Aaron Shen Mar 13 '15 at 10:14
  • @AaronShen Nope. Try it. – Rich Mar 13 '15 at 10:18
  • 1
    @Rich if that works for you then you have a setting that enables that delete movement. I've just tested it with vim -u NONE -U NONE and it deletes both lines. – Vitor Mar 13 '15 at 11:40
  • @Vitor You're right. Trying to figure out which setting does it, now. – Rich Mar 13 '15 at 12:50
  • 1
    BTW it turns out using gk as characterwise apparently requires :set wrap (somewhat cryptically). I couldn't figure out why this seemingly was working for everyone else but not for me, when the help for gk likewise seemed to imply it should work. The only clue was that the main meaning of gk had to do with wrapped lines. Once I tried setting wrap on (kind of on a whim), voila, it worked. – BobH Mar 14 '15 at 2:08
1

I can't yet comment, so I need to make a new answer. The keys d k given by Rich don't work for me even when using the nocompatible option. A reason might be that the motion k is considered linewise by vim, in which case both lines are deleted. From :help d:

An exception for the d{motion} command: If the motion is not linewise, the start and end of the motion are not in the same line, and there are only blanks before the start and after the end of the motion, the delete becomes linewise. This means that the delete also removes the line of blanks that you might expect to remain. Use the |o_v| operator to force the motion to be characterwise

It also shows the remedy: Use dvk to force the motion to be character-wise. This does the trick for me.

I didn't know o_v before, so here's :help o_v:

v

When used after an operator, before the motion command: Force the operator to work characterwise, also when the motion is linewise. If the motion was linewise, it will become |exclusive|. If the motion already was characterwise, toggle inclusive/exclusive. This can be used to make an exclusive motion inclusive and an inclusive motion exclusive

  • The 'nocompatible' thing was a complete red herring. I messed up when I was testing because I am an idiot. – Rich Mar 13 '15 at 13:19
  • We can also use dgk, where gk is also exclusive and not linewise. – MS.Kim Jun 16 '16 at 2:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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