Ignore trailing spaces when yanking a word in neovim - vim

So my issue when yanking a word (yw) is that it copies the trailing spaces as well:
Example:
Doing a yw on this:
‖Hello World
Results in copying Hello
But the same applies to:
‖LongVariableName = 'SomeValue';
Results in copying LongVariableName
Is there a way to make as such I copy without copying any space? (which for me makes sense when copying words)

You can use yiw to yank the 'inner word' text object (yaw would include an additional space). See :help text-objects.
A nice feature of text objects is that the cursor can be anywhere on the object (in this case the word) for it to work.
There are many text objects in vim:
is means inner sentence
ip means inner paragraph
i) means inner parentheses
And many more!
Also the comment below by #DoktorOSwaldo points out that you can also use ye to yank from the current position until the end of the word without the space, e is the end of the current word, w is the start of the next word.

Related

Surround Several Words With Quotes At Once In vim

I was curious if there is a way to surround several words at once with quotes using vim. I am using tpope surround and repeat but I was wondering if there is a command like
3ysw"
so from
one two three
to
"one" "two" "three"
You can visually select the range with v3e, and then run a substitution command on it: :s/\v(\w+)/"\1"/g (the range '<,'> should automatically be inserted).
Personally though, I'd rather surround one word with ysw", and then do w.w. (repeat as often as needed).
Alternatively, record a macro that does both steps (surrounding and moving on to the next word), then call it n times:
qqysw"3wq
After this is in your q register, you can then call 2#q to perform the surroundings on the remaining words.
When you want to enquote three words, beginning with the one your cursor is currently placed within, you can do:
bv3ec'<Ctrl+r>"'
b places the cursor at the beginning of the current word, v enters visual mode, 3e jumps at the end of the current 3-word sequence, c cuts the selection and enters insert mode, where you insert the left enclosing quote ' and press <Ctrl+r>" in order to paste current contents of the clipboard buffer, before you insert the other enclosing quote '.
Omit the leading b if you start off with the cursor at the first character of the first word.
Another substitution option
s,\w\+,"&",g
s ............. substitute current line (add %s for the whole file)
\w\+ .......... one word or more
"&" ........... & represents the whole match on the search part
g ............. every occurrence on the line
OBS: When using substitution we can use a different delimiter in order to make easy to type. (Also useful when searching for things like "/my/pattern/")

How can I highlight multiple lines in gVim without using the mouse?

Vim noob here. I am trying to select multiple lines of code to copy and paste in other areas. Is there a way to do this without using the mouse?
A few other ways that don't use visual mode at all:
using marks
leave a mark somewhere with ma
move somewhere else
yank from here to there with y'a
using search motions
localize some unique token at the end of the part you want to yank
yank from here to there with y/foo<cr> (forward search) or y?bar<cr> (backward search)
using text-objects
determine what text-object would map to what you want to yank:
inner/outer word, iw/aw
inner/outer pair, i'"([{</a'"([{<
inner/outer html tag, it/at
sentence, s
paragraph, p
"block", ]
…
yank that text-object with, say, yip
using other motions
yank to end of function: y]}
yank to end of file: yG
all of the above solutions with visual mode
V'ay
V/foo<cr>y
V?bar<cr>y
Vipy, etc.
V]}y
VGy
:h motion.txt will hopefully blow your mind, like it did to mine.
You can place your cursor in the first line you want to copy and then type nyy where n is the number of lines you want to copy. For example, type 2yy to copy the two lines under the cursor.
Then, you can paste them using p.
You can also select multiple lines by placing your cursor somewhere and keeping Shift pressed. Move your cursor to the end of the desired selection and stop pressing Shift. Then copy using just y (and not yy) and paste with p.
Yep, in normal mode type V[direction] and you will highlight multiple lines. If you don't want whole lines, use v instead of V. To copy it, hit y and move to the area which you want to paste in and hit p. To delete it, instead of y use x.
Alternatively, you can simply use [number of lines]yy to yank some number of lines or [number of lines]dd to cut some number of lines. In this case pasting is the same.

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