Changing case at the end of a word in Vim - vim

I know gUw will turn properties to PROPERTIES and stay at the first character of the word.
But if I use gUb, it will be PROPERTIEs and cursor at the first charactoer.
If I use gUge, it will be PROPERTIES but cursor at last word.
What should I do if I want to turn the word to upper case, and stay at the end of the word(so that i can keep on typing without a w)

All operator+motion edits will leave the cursor on the first character of the motion. From :help motion.txt:
After applying the operator the cursor is mostly left at the start of the text that was operated upon. For example, "yfe" doesn't move the cursor, but "yFe" moves the cursor leftwards to the "e" where the yank started.
So, assuming your cursor is on the last character of the current word:
properties
^
there is no built-in way to ensure that the cursor will stay there after an operator+motion edit.
One can leave a mark and jump back to it after the edit:
m'gUiw``
but that's more work than just pressing w. It could be turned into a mapping, though, if that's a common need:
nnoremap <key> m'gUiw``
but that doesn't sound very scalable.
Another approach would be to record your edit:
qq
m'gUiw``
q
and play it back as needed:
#q
But your last sentence is puzzling:
What should I do if I want to turn the word to upper case, and stay at the end of the word(so that i can keep on typing without a w)
If you are at the end of:
properties
^
and your edit leaves the cursor at the beginning of:
PROPERTIES
^
w will move the cursor to the beginning of the next word:
PROPERTIES foo
^
and not to the end of the current word.
We are missing some context, here.

Related

In Vi/Vim, how do I copy a substring inside a line

I have a dictionary in my config file.
candidates = {'morpheus':(3,1), 'trinity':(3,1), 'neo':(3,1), 'switch':(3,1)}
I can highlight with my mouse one k/v pair (e.g. 'neo':(3,1)) to copy and paste if I needed add more k/v pairs to the dictionary but is there a way using vi keyboard commands to yank from the current cursor to the next comma or space to grab the 'neo':(3,1) k/v pair?
I know there's yw for yank word but in this case, vi stops at the punctuation marks and doesn't grab what I want. I think I can also yank characters to the left or right of the cursor but I don't want to count characters if I can help it.
Is there a way to tell vi to yank from the current cursor position to the next space or the next ) character?
yw, which is actually "yank to next word" rather than "yank word", is not to be taken as a single command. It really is two commands: an operator, y, followed by a motion, w. This is pretty important because understanding that operator+motion model allows you to freely compose very expressive editing commands.
In this case, you can move the cursor to the closing ) with f) or 2t,, which gives you the two following commands:
yf)
y2t,
See :help f, :help t, and the user manual's introduction of the operator+motion model: :help 04.1.
Yank to the next space (excluding space) yt (Note the space at the end, the character yanking to)
Yank to the second space (excluding space) y2t
Yank to the next space (including space) yf
Yank to end of line y$
Yank everything inside of current '...' yi'

How to move behind autopaired char in insert mode in vim

I am using auto-pair in Vim (https://github.com/jiangmiao/auto-pairs). I am typing code:
= link_to 'Link name', some_path(#var|)
And the cursor in edit mode is at | position. Then I want to continue typing more params.
What is the fastest way to move cursor behind the auto-paired )?
I know I can press Ctr-O Shift-A but 4 key strokes to move cursor one column to the left is over-kill.
You could use the right/left arrow keys, but the way the plugin provides a better way of doing it. If your cursor is right next to the closing auto-paired character, you just need to type that character. For instance, if your cursor is in a position like this:
def some_func(args|)
You can just press ) to go to the right of the auto paired character. Same goes for any other closing characters that auto-pairs is compatible with.

Undo cursor movement in Vim

Let's say I have my cursor lying in the code below:
internal static SingleSelectList<Country, int> CreateCountrySingleSelectList(List<Country> countries, List<Airport> airPorts)
And the cursor is in the head of the line. Now I want to move my cursor to the second < of this line of code, which is in the WORD List<Country>, and just doing the key sequence of f<; will bring me there.
But what if I did something wrong by pressing another ;, that will bring me to the next < in the line, which is in the word List<Airport>.
In this situation, how can I get back to the second < using the minimum key strokes?
Is there a fastest way to undo cursor movements, instead of F< or pressing the key hfor a very long time?
Try CtrlO to go back in cursor history.
http://www.rosipov.com/blog/open-previously-edited-file-in-vim/
The reversible action of ; is , so the answer to your question would be to use , one time.
You can look up what the command does by :h ;
; Repeat latest f, t, F or T [count] times.
, Repeat latest f, t, F or T in opposite direction
[count] times
Contrary to Ruslan Osipov's wrong answer, the f motion does not store the previous position in the jump list (only motions that usually go to non-adjacent lines do).
But nothing prevents you from explicitly setting a jump yourself with m'. Then, you can return to that position via ``, or <C-O>.

How does "daw" to delete a word in Vim work if "dw" only deletes part of the word?

I'm a Vim user and I want to delete a keyword. I always use "dw" to delete a specific keyword, but it sometimes doesn't work well. For example, I want to delete "valule123" in sample program.
ex) public void function(int valule123)
When I put my cursor is on "2", and then I input "dw", only part of keyword is deleted and the result is "valule1". Why?
I tried another command, "daw". In this case, the result is just as expected! But what does "a" mean? I think "a" means "add".
The command/action/verb d in Vim acts on an object. For dw the object is "all text the cursor moves over with a w command". For daw you're actually using a Vim concept called a "text object". There are many of these, including aw (a word), as (a sentence), i} (inner {...} block, e.g. code within a block in C code), it (inner tag, useful for XML-like languages), and more.
See :help text-objects for the full list.
These can not only be used by the d command, but any command/action/verb that takes an object. For example, =aB will reindent an entire code block, cas will delete a sentence and drop you into insert mode to type a new one, and yit will yank/copy everything inside the current XML tag.
dw: deletes word from the cursor to the end of the word.
daw: deletes the word under the cursor.
There is also the option of writing caw, this one does the same as daw, but also puts you into insert mode.
If you type :help daw inside Vim console, you will see it means "delete a word". So, 'a' means a here. More from the doc:
For example, compare "dw" and "daw": "dw" deletes from the cursor
position to the start of the next word, "daw" deletes the word under
the cursor and the space after or before it.
If you don't want to delete the space after/before it, you can use diw (delete inner word).
For these cases, you can always use: diw that way it won't matter where your cursor is over the word it will always remove the entire word.
d: delete
i: internal/inner
w: word
Another useful use is ciw (change internal word) to delete the word and go into insert mode.
Cheers!
In case that I know the word to delete, my flow of deleting would be:
Find :/deleting-word and cursor at the end of the deleting word.
on -- INSERT --, combo [ctrl + w] to delete that word, or keep going for multiple words.
lbce may work well if you want to change a word in a english sentence despite where the cursor position in the word you want to delete.
When navigating around dw will delete a word. Of course w will navigate from the first character of a word to the next word, whilst b the previous word etc.
Given that you use the navigation keys j,k,h,l (down, up, left, right ), if you're in a word on a particular charachter, you can type d for delete then l and the character to the right will be deleted, or h and the character to the left will. It's more intuitve when your fingers are actually on the keys 😄

How do I get fine-grained undo in Vim

I find Vim's undo to be a bit too coarse. E.g. if I type something like this:
a // to go into edit mode
to be or not to ve
<esc> // to exit insert mode
Oops! I made a typo. I want to start undoing so I press u, but then it clears the whole line. Is there a way to undo word-by-word or character-by-character?
You can break undos via :help i_ctrl-g_u. You can map this if you want for every character, but that might a little bit complicated. Mapping this to space button is a way.
:inoremap <Space> <Space><C-g>u
After that every word can be undo via u
So as you see from the others what you are asking for doesn't exist in Vi (AFAIK).
Undo undoes what your last action was. If your last action was to enter insert mode and then add a line and then exit insert mode. That will be undone, however if from the default mode you hit the "x" key then you will delete 1 character or if in visual mode with text selected the text will be deleted. If you hit undo then you will restore that one character or the text that was selected.
...You should think of this as an action, and actions can be atomically undone or restored
As mentioned previously if you wish to delete the previous word then you should be able to hit Ctrl + w and delete the previous word while remaining in insert mode.
If you exit insert mode you can navigate (motion) back a word with "b" forward a word with "w" to the end of a word with "e", and can cut (which leaves you in insert mode) with "c" or delete with "d". Both actions cut and delete can accept a motion following them so you can delete the current word / up to the next word with "dw" or cut the previous word with "cb"
This concept becomes more useful when you remember to use the "." command (in normal mode). This command is to repeat the last action. I have used this many times to find and replace a small group of words in a file (It is especially useful if you are paranoid about changing too much). The scenario would be the following:
File:
This is my post
really it is a test
this is the middle
This is the end
if I wanted to replace "is" with "was" I could write:
%s/\<is\>/was/g
however if I wanted to change the first line and the third line "is" to "was" (and I didn't know their line numbers, and I wanted to see if there were any other places I wanted to change is to was I could type
"/is"
hit "n" until I reach the place I want substituted, and then hit "cw" and type "was"
I can now hit "n" until I reach another place I want substituted and hit ".", and that will replace "is" with "was" (Note: my search string didn't limit to the word "is", just the two characters "is" so "This" & "this" will match in this case)
No, it is not possible and is actually not necessary either. Vim has a million ways of dealing with that. Try cb for example. Or bC. Or brb. Or Tspace to jump back instead of b. Or ciw.
You can, of course use most of these solutions in insert mode (by pressing CTRLo first), or bind one to your favorite key combination (:help map and :help imap).
On Linux, using control-w while in input mode deletes the last 'word'.

Resources