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.
Related
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.
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".
Often when developing I am confronted with a nested object that I'd like to delete from code in the middle of a line like this:
htmlDoc.WriteLine("<b><h3>" + this.cbAllSyncs.SelectedItem.ToString() + "</h3></b>");
The part that I'd like to delete is:
this.cbAllSyncs.SelectedItem.ToString()
I know I can count the number of words and periods and enter 7dw to delete from my current cursor position of "this". However, what I'd love to do is not have to count at all and delete to the space with one command. Is this possible?
Try dtspace. In general dtx deletes from current position till just before x. Just tx moves the cursor to just before character x in current line.
To delete up to and including the space, use dfspace.
You can use dW or dE as #glenn suggested if you don't want to delete the space itself.
A WORD (uppercase W) consists of a sequence of non-blank characters, separated with white
space.
Give a look to the word motions.
one possible solution is to use the delete with a search.
so type in d/<space> and vim will delete until it hits a space.
If you want to delete from anywhere inside the WORD, and in a single command, just use
daW
(you can of course use daw if you want to limit to a word)
I use it quite a lot because it spare a move to the begining of the word (in your case)
delete for the current position to a specific character example "
dt"
delete the word from the position cursor is on till the end of the word
dw
delete the entire word the cursor is on irrespective of the position the cursor is on the word, this also puts you in insert mode to enable you to insert immediately.
diw
dtspace is the answer to your question, but df+ looks like it will solve your problem better.