how to copy a consecutive set of none empty characters in vim - 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.

Related

Yank text between certain words or marks

Is there some vim-magic to yank between two strings?
As example:
#%%
some example
or
some other
#%%
I would like to yank everthing in between (#%%)? Like the "yi(" command.
y works with all motions, which includes /.
/#%% and then wyn would accomplish what you want, where
/#%%: search for the literal symbols #%%. As long as your cursor was above the section you want to yank, this will highlight the first instance.
wyn: move one word forward, to not include #%% in your yank (this assumes #%% is space separated from everything you want to yank). yn just means "yank all text until the next instance of the search".
If you don’t care that :yank is linewise, you could do
:/#%%/+,/#%%/- yank
Since patterns count as addresses, we can write a range that selects the inner text, and then yank it.

Vim yank path under the cursor

What's the most efficient way to yank a path that's under a cursor? So if a line has the following contents:
/some/file /some/other/file /last/file
... and the cursor is over other, I would like to yank /some/other/file. Normally, if it were a word yiw would work, but since that path contains quotes, yiw would only yank other rather than the entire /some/other/file.
Is there a way to efficiently yank the entire filename?
A word consists of a sequence of letters, digits and underscores, or a
sequence of other non-blank characters, separated with white space (spaces,
tabs, ). This can be changed with the 'iskeyword' option. An empty line
is also considered to be a word.
A WORD consists of a sequence of non-blank characters, separated with white
space. An empty line is also considered to be a WORD.
yiW - just need to capitalize the W
While I would use yiW as in dave's answer, ByE (go to beginning of WORD, yank to end of WORD) is another possibility.
Byt<space>:
B to go to the start of the file location,
yt<space> to yank up to the next line space.

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 do I delete till non-whitespace in 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.

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