Suppose I'm in a certain line position in vim and I want to delete up to a certain character, say a semicolon. I would do df; except it would also delete the semicolon. Is there a command that will do the same thing but will not include the character I'm searching for?
Yes, dt;. From the Vim docs:
t{char}
Till before [count]'th occurrence of {char} to the right. The cursor is placed on the character left of {char} |inclusive|. {char} can be entered like with the |f| command.
To add to what Michal said, you can also use T and F to do the same thing backwards.
Also ; will repeat the last t,T,f or F motion, and ' will repeat it in the opposite direction.
Related
How to delete till the first character of the line ? In the line below, for example, with the cursor near the end as indicated, delete backwards until the first $.
I have this line:
[space][space][space]$entity->setPositionBrand(count($qb->getResult())[my_cursor_here] + 1);
After deletion, I want this:
[space][space][space] + 1);
There is a standard vim motion that goes exactly to the first non-whitespace character on the line. It's ^
So you only need to type d^.
Obviously not as succinct as Vladimir's answer (which is a better solution), but for the record, here's how you could achieve the same with visual mode.
v0wx
v Enter visual mode.
0 Move to the beginning of the line.
w Move to first word.
x delete characters in visual selection.
Any amount of whitespace counts as a word.
If you want a general solution, you would probably need some sort of regex-based keybinding. But, you can accomplish what you want with these key combinations:
Put your cursor on the first $ sign.
d 12 e
The second key sequence deletes characters from the position of your cursor up to the end of the twelfth word, which will result in exactly the line you want.
I am in vim editing a python file, how can you delete the sequence throw=it,? After searching online I see the command daw, but that doesn't work with this word group.
one two three throw=it, now
another way is daW. with a capital W, it will delete any sequence of non-space characters, regardless of where inside the sequence you are.
This is different from dE, because dE only deletes from the cursor until the next end of the sequence of non-space characters, whereas daW will also delete the whole sequence between whitespaces.
assuming cursor is at the start of throw=it, dE should do the trick
E Forward to the end of WORD [count] |inclusive|.
Does not stop in an empty line.
Another way is Bdt<space>. Note your cursor can be on any character on throw=it,
B: puts the cursor at the very front of the block after the preceding space
dt<space>: delete till space
Use caW to delete your sequence and enter insert mode.
Use daW to delete your sequence and stay in normal mode.
I'm looking for a command to delete from the cursor to the first non-whitespace character on the same line. I've googled for a while and tried several possibilities. No joy. Does someone out there know how to do this?
The sequence dw will delete all characters from the cursor until the next word. This means that if you execute the command while standing in the middle of a word, it will delete the remainder of that word and subsequent whitespaces. May or may not be what you're looking for.
You may want to try dW. This will move by "WORD" (see help for WORD and word), which looks more appropriate for your question.
The dw won't meet your needs in, for example:
array[1] = 5
Hitting dw while positioned in the a will leave you with:
[1] = 5
But using dW will result in:
= 5
Many of the answers here don't really address the question directly. The asker wants to know how to delete up to the first non-whitespace character. Some of the answers will technically work, but let's take a look at how to do this explicitly.
The following examples demonstrate how to do this in normal mode with variations that account for the starting position of the cursor. The u̲nderlined c̲haracters indicate the cursor position:
dw:
foo_ bar
→
foob̲ar
The delete word command, described in other answers, works just fine to delete up to the next non-whitespace character when our cursor is positioned before the target.
db:
foo b̲ar
→
b̲ar
Naturally, we'd want to try the inverse of dw to delete backwards to the first non-whitespace character before the cursor. However, as shown above, the delete back-word command deletes more than we expect—it erases the previous word as well. For this case, we should use:
dT<?>:
foo b̲ar
→
foob̲ar
...where <?> is the first non-whitespace character before the cursor. The delete back-unTil command erases everything up to but not including the character <?>, which, in this case, is the character o at the end of "foo".
dt<?>:
foo_ bar
→
foob̲ar
Similar to the previous command, we can use delete until (with a lowercase "t") to delete characters forward until the next <?> character (the b in "bar", for this example). This achieves the same result as dw for the purpose of this question.
diw:
foo _ bar
→
foob̲ar
If our cursor is positioned in the middle of the whitespace, we can use the delete inner word command to remove the whitespace from both sides.
d/<?>↵:
foo_ \n bar
→
foob̲ar
If the whitespace we want to remove includes line-breaks, we can use the command shown above to delete until matched pattern of <?>, where the pattern in this case is just the first non-whitespace character. As shown, press Enter to complete this command.
When the first non-whitespace character occurs at the beginning of the line after the break, Vim will remove the whitespace, but leave the target on the next line. We need to add J to the above command to Join the lines (an uppercase "j").
d/<?>↵J:
foo_ \nbar
→
foob̲ar
To delete a word regardless on which letter the cursor is on, use daw (mnemonic "delete a word") works with other commands as well, e.g. caw "change a word". f and t are other excellent command that can be used together with d. E.g. to delete from cursor to and including first occurrence of e.g. the letter "t", use dft. To leave the "t" intact, use dtt instead.
dw should work.
d W will delete word include the last space. If the word you want to delete is at the end of a line, you can prefer use d e. because if you use d W, it can shift your next line up.
I always use d i W.
In vim, in normal mode, if the cursor is in a word, not the last letter, de deletes the suffix of the word, from the position of the cursor. If the cursor is on the last letter, x does it too, while de would jump to the end of the next word.
What command would you use that would work in both cases, last letter or not?
The purpose is to include the command in a macro.
Try vwged instead of de, and define a mapping like the following, if you
like it.
:nnoremap <leader>de vwged
It seems to do exactly what you want.
You could also try d/\> which translates to delete upto next end word boundary.
If your word separator is space, dt<space> will work. t will match all characters until the specified character.
:s/\w\+//
This will substitute at the beginning of the line.To make it substitute at the position of the cursor you have to add some lines to your vimrc. Follow the instructions here
http://vim.wikia.com/wiki/Repeating_a_substitute_from_current_cursor_position
Observe a line in a Vim instance:
Now I hit $:
Why does my cursor not go all the way to the end? Once I try inserting, the text gets inserted before the last character! Even if I try to move right again while still in normal mode I get the bell. Oddly, when in edit mode I can move to the actual end of line with the right arrow key!
Does anyone know why Vim does this? On 7.3 by the way. Thanks for the help.
Pressing $ while in command mode causes the cursor to move to the end of the line, effectively highlighting the last character. Hit i here to insert before the last character, or a to append to the line. It is slightly ambiguous here, because you're using a pipe character as a cursor rather than a rectangular block cursor. Have a look at ":help termcap-cursor-shape" if you want to change that.
If the goal is to append to the end of the line, A will jump to the end of the line and enter insert mode with a single keypress.
Use a to append a character after the current.
Or, to go to the end of the line and append in 1 step, use capital A. I.e. shiftA.
Similarly shift-I to insert at the beginning of the line without first having to press ^.
The cursor can't be between two characters, it is always on a character.
If you press $ then x, you will correctly delete the last printable character of the current line.
What you are observing is the fact that using i, you are always inserting your text before the selected character. If you want to insert after the selected character, you have to use a or better A as it has already been mentioned.
In other words:
i means "insert before character under cursor".
a means "insert after character under cursor".
mnemonic for a : a for "append".