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
Related
Let's suppose I have the cursor located as depicted on next image
Pressing b in normal mode I can go to the start of the word.
Great!
If I move cursor to 1 like
and press b we have
Question:
Is there a motion command to move cursor to start of the word so that if the word is one character long remains at the initial position? In my example, the cursor should stay at 1.
I'm looking this motion command to implement a Macro that in some of the steps move cursor to the start of a word, with words sometimes having just one character.
Simplest solution I have found for this is just wb which works wherever your cursor is on the word, and for single character and multi character words.
Source is reddit user 'happysri' from this thread: https://www.reddit.com/r/vim/comments/1xzfjy/go_to_start_of_current_word_if_not_already_there/
I don't believe there is such a motion (please, anyone, correct me if I'm wrong)
But you can achieve that with a search:
:call search('\<', 'bc')
\< matches the beginning of a word
The b stands for backwards
The c is to accept matches under the cursor
I've found this answer Move to end of the current word in Vim that is somehow similar to my problem...
Applying the idea shown there, I should always move one character forward l and then move at the beginning of the word with b. That works and it is consistent for words of different sizes.
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.
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".
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.