VIM: Deleting from current position until a space - vim

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.

Related

Is there a Vim motion command that put cursor at beginning of current word?

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 complete the whole list with Vim?

I have a list of products to place on a rails seed and I would like to instead of put brackets one by one on the list with a command place the brackets on the whole list?
for example:
1. Dakine
2. Dale of Norway
3. Dan Post
1. ["Dakine"],
2. ["Dale of Norway"],
3. ["Dan Post"],
I searched on the help but did not find any about. Thanks.
You can record a macro in Vim and repeat that.
If you are on number 1, you can do following:
qqf a["Esc$a"],Esc0jq
Explanation:
qq: Start recording macro in register q
f: Go to first space character
a: : Insert after (the space character from above)
\[": Insert those characters
Esc: Back to normal mode
$: Go to end of line
a: Insert after (end of line)
"],: Insert the characters
Esc: Back to normal mode
0: Jump to start of line
j: Go down one line
If you have 100 such lines, you can do 100#q to achieve your result.
With vim substitute command:
:%s/.*/["&"]/
If you don't want to operate on all lines, then select the ones you want to transform or note the related line numbers, and then type :s/..... without the %. You'll see actually :'<,'>s this range represent the visually selected lines, and vim adds it automatically in visual mode.
On Atom you can enable the find to use Regex in the search(there is a button next to the search field)
Then you can search for something like (^.*$) to get every line separated by groups and in the Replace field you use ["$1"],. The $1 represents the value matched by the Regex.
Then just do a Replace All and remove the last comma in your list if needed.

VIM Delete From Cursor to Another Location in File

I'm trying to learn VI/VIM. I would like to know how to deleted the text from my cursor to some other spot in the file. I know how to delete a line (dd) and multiple lines (5dd) and to the end of a line (d$), but not, for example, from the cursor to the middle of the next line or the middle of the next two or three lines.
Thanks for any tips.
Cheers!
You can use any motion after a d. For example, to delete two words, you can do d2w. Or to delete 10 characters to the left, you can do d10h, or to delete next two lines, do d2j. For something more complicated like 'delete up to middle of next line', I usually just do v to go into selection mode, select what I need with hjkl, and hit d to delete it. If you do block selection mode Ctrl+v you can select a block that needs deletion and hit d. Hope that helps.
What do you mean "the middle"?
You delete with d{motion}, and that includes things like:
d5w - delete the next 5 words
d/test - delete up to the word test
see
:help d
:help motion
and the motion.txt linked in the help (also online http://vimdoc.sourceforge.net/htmldoc/motion.html )
I used to have vi macros to delete between marks. e.g. use ma to make mark a, and then put your cursor where you want it and:
mb'a"ad'b will make mark b and delete from mark a to b into buffer 'a'
mb'a"ay'b will copy (not delete)
"ap will get the text back.
(from memory this is whole line based, not "position on a line")

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.

VIM to delete a range of lines into a register

I am trying to delete a range of lines into a register a. Is this the easiest way to achieve this?
:5,10d a
The definition of "easiest" depends on what do you have, and what do you want to do
if you have a start line number and end number, e.g.
:2349,5344d a
is the easiest way.
You don't have to consider the questions like
"where is my cursor?"
"how many lines would be removed?"
...
If you are about to remove a small amount of lines, particularly they are on same screen. (You could use relative-linenumber.) for example: "a5dd but you have to move your cursor to the first line you want to delete. And this could be done by option 1 too: 5:d a<CR> (vim will automatically translate it into .,.+5d a<CR>)
If you just know the 1st line of deletion, and find the last line you want to delete by reading your text, (of course, small amount of lines) you could press V, and press j by reading, when it reaches the deletion ending border, press "ad
If the "range" in your question is the "range" concept in vim, The first option would be better. since it could be 234,540, it could be 1;/foo, /foo/,/bar/... :h range see detail
so back to the first sentence in my answer, There is no absolutely easiest way. It all depends on what do you have, and what do you want to do.
The other way to achieve this would be to highlight the range of lines in visual line mode. (Shift-V)
Then type "ad while in visual line mode. This will put the deleted lines into the a register.
" followed by a register puts the next delete, yank or put into that register.
Below is the documentation for " (quote)
*quote*
"{a-zA-Z0-9.%#:-"} Use register {a-zA-Z0-9.%#:-"} for next delete, yank
or put (use uppercase character to append with
delete and yank) ({.%#:} only work with put).
Another example of deleting multiple lines and putting it in a register. To delete 6 lines and put them in a register you can got to the line and type "a6dd. This puts the 6 deleted lines into register a.

Resources