How do I delete till non-whitespace in vim? - vim

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.

Related

Vim: How to delete between the cursor and the first character of the line?

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.

Delete word group in Vim

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.

In Vim, I'd like to go back a word. The opposite of `w`

When you're using vim, you can move forward word by word with w. How do I go backwards?
Use b to go back a word.
You may also want to check out W and B to advance/go back a WORD (which
consists of a sequence of non-blank characters separated with white space, according to :h WORD).
It helps for me to think of it as:
b to go to beginning of current or previous word
w to go the beginning of next word
e to go to the end of current or next word
ge to go the end of the previous word
Try :h word-motions for more details and how to combine them with operations.
use "b" to move back - just tested in vi - works fine.
Alternatively, if you use w, b, W, and B to navigate lines by hopping over words, consider the following alternatives which can be faster if used correctly.
f<char> # jump to next occurrence of <char> to right (inclusive)
or
F<char> # jump back to next occurrence of <char> to left (inclusive)
If your words are separated by spaces
If your words are separated by <space> you can hop over words by spaces:
f<space>;;;; where ; repeats the previous command, so you hop forward by spaces
F<space>;; to hop backwards by space
If your words are separated by punctuation and not spaces
just replace <char> with punctuation, for example .
The punctuation method is not efficient for scrolling through, but if you know where you want to jump, it can usually get there in a jump or two.

how to copy a consecutive set of none empty characters in vim

${BASE}/test/ other_consecutive_none_empty_characters
If the cursor is under the $ sign ,how can I copy "${BASE}/test/" as a whole and paste it
somewhere else in vim ? I don't want to use the human-eye-letters-counting solution if that is the only solution I'd rather like to hit the keyboard charcter by charcter .
yE would be the most correct solution. y means "yank" (i.e. copy to buffer), and E - "up to the end of the WORD", which is, up to the next whitespace character.
Personally, I prefer typing yiW - yank inner WORD. It works the same way, but the cursor may reside anywhere inside the WORD, not just on the dollar sign.
y Yank W WORD:
yW
From :help W:
A WORD consists of a sequence of non-blank characters, separated with white space. An empty line is also considered to be a WORD.
yt , i.e., y, t, followed by space. t can be thought of as "till". You can select with vt, delete with xt, etc.

how to move a word to the beginning of the sentence in vim

say I have this line
= function (x, y, word);
and I want to convert it to
word = function (x,y);
Thus far, I have been manually selecting the word, then 'x', and then paste it at the beginning. And then I would remove unnecessary comma.
Is there a more efficient way to accomplish the same thing?
Don't create weired functions or macros, as many advanced users may suggest you, but learn simple commands, which can help you when you would need to make similar, but slightly different substitution.
My solution would be: place cursor on the comma, and type: xxdw^Pa <C-[>
Description:
xx - delete comma and space
dw - delete word
^ - place cursor on the beginning of text in line
P - place deleted text before cursor
a - add space after word
<C-[> - escape to return to normal mode, you can also press <ESC> if you like, or don't press at all
And how to place cursor in comma? Learn about f,, F,, t,, T,, w, b and e to move faster around your text.
:%s/\(.*\),\([^)]*\)/\2\1/
EDIT:removed /g
EDIT2: the %s is only if you want to do this for the entire file. if you just want to do this for the current line then replace % with . (a dot)
I'd suggest recording a macro: (starting at the beginning of the line)
qq2f,2xdw0Pa <esc>0jq, then running that macro wherever you need it: #q.
Try this: :dw to cut the current word, move to beginning of line, then :p to paste the buffer there.
Or you could use a regular expression.
:s/\(^.*\), \(\a\+\)\();\)/\2\1\3/
(Match up to the last comma) -> \1
(match last argument) -> \2
(Match closing brace and semicolon) -> \3
The reorder the matched terms as you need.
Place cursor over word and type:
"0diw delete word and store it in register 0
dF, delete backwards up to and including ,
^ place cursor at first character in line
"0P paste word
I would suggest to map this to a key.

Resources