Where is '$' under the "normal mode" (after esc-pressing mode) in "Vim" text editor? - vim

I have hard time realizing, as a beginner, why in "Vim" (Windows 10), when I want to delete a word with a d$ and when I simultaneously press shift+4 on non-numeric key above qwerty... why "Vim" does not recognize that as a $?

I strongly believe that you confusion stems from the fact that d$ is not actually meant to delete a word. Moreover, dw (which sound pretty much like "delete word") is not meant to delete a word either.
d is a command that will perform its action (deletion) on the next movement right after the command. Note that $ moves to the end of the line, and w moves to the beginning of the next word. Therefore:
d$ deletes from the cursor position until the end of the line.
dw deletes from the cursor position until the beginning of the next word.
For a better example you should try d3j. 3j moves three lines below, therefore d3j deletes the current lines and the next three lines as well.
To delete the word under the cursor (no matter where the cursor is in the word) there are several ways. Some ways to do it:
bdw first move to the beginning of the word then delete 'till the next word.
vawd visually select the word a word (including the space after the word) and delete it.
viwd visually select an inner word and delete it (this leaves the spaces around the word in peace).
daw delete a word (without going into visual).
diw delete inner word (without visual).
(Try the visual ones first to see how they work, then you can use the non-visual ones.)
Also, we have a vi.SE section of the website.

I've finnaly understand it... a cursor must be BEFORE a related "tail" to be eliminated.
PS: In fact problem was with "e212 error: can't write to file"... instead of changing proposed "sudo permissions", while I'm on Win 10 instead, I've just reinstalled a fresh Vim.
So, problem were not with "$" (as it was where always have been) but when one is unsure where problem lies, often makes a wrong diagnosis.

Related

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 😄

VIM Select Entire Line

How do you select a single line in VIM, when your cursor as at some random point along that line?
I know you can do (v, $) to get to the end of the line, or (v, ^) to get to the start, but when you do (v,$,^) it logically doesn't select the whole line, it selects from cursor, until end, then switches it to cursor until beginning... So this approach fails of course.
Capital V selects the current line in one key stroke; two, if you include the "shift" in shift+v.
V would be direct answer. However, I rarely need to do this because "selecting the current line" is generally part of a larger task. Example of such tasks includes copying the line and deleting the line. There's generally a better way to accomplish the task as a whole. The following are some of the tasks I can think of:
copy the line: yy
delete the line: dd
indent the line: >> or <<
select the current paragraph: vap or vip
delete from the current line to the end of the file 0dG
highlight the current line to see where my cursor is: use :set cursorline in .vimrc file
One case in which I do use V is to select multiple lines that are not a paragraph or some other text object. In this case, there's a tip that might be useful for you: once in the selection mode, you can use o to jump the cursor between the start and the end of the selection.
While this might be more keystrokes.
If you are already in visual mode you can use o to go to the other end of the visual selection.
So you can type
v0o$
To select the whole line. Take a look at :h visual-change
However from the comments it seems you just want to copy the whole line.
Which would just be yy
Just change your order of operations. You almost have it.
^,v,$
Or as suggested by #Kent: because ^ goes to the first non-empty char, if the line has leading spaces:
0,v,$
I know this thread is super old, but I just had the same question. This thread came up first, but I found a different answer than any found here. Use 'V' to select whole lines. That easy. One character to select the whole current line.

How to delete, including the current character?

Let's say I've typed "abcdefg", with the cursor at the end. I want to delete back to the c, so that I only have "abc" left.
Is there a command like d that includes the current character? I know I could do dTcx, but the x feels like a work-around and I suppose there's a better solution.
No. Backward motions always start on the left of the current character for c, y and d which is somehow logical but also unnerving.
The only "clean" solutions I could think of either imply moving to the char after c first and then do a forward delete:
Tcde
or using visual mode:
vTcd
v3hd
But, given your sample and assuming you are entering normal mode just for that correction, the whole thing sounds extremely wasteful to me.
What about staying in insert mode and simply doing ←←←←?
try this:
TcD
this will leave abc for your example... well if the abcdefg is the last word of the line.
if it is not the last word in that line, you may do:
ldTc
or golfing, do it within 3 key-stroke:
3Xx or l4X
See this answer to a similar question : there is a setting to be allowed to go beyond the end of the line
From the doc :
Virtual editing means that the cursor can be positioned where there is
no actual character. This can be halfway into a tab or beyond the end
of the line. Useful for selecting a rectangle in Visual mode and
editing a table.
"onemore" is not the same, it will only allow moving the cursor just
after the last character of the line. This makes some commands more
consistent. Previously the cursor was always past the end of the line
if the line was empty. But it is far from Vi compatible. It may also
break some plugins or Vim scripts. For example because |l| can move
the cursor after the last character. Use with care!
Using the $ command will move to the last character in the line, not
past it. This may actually move the cursor to the left!
The g$ command will move to the end of the screen line.
It doesn't make sense to combine "all" with "onemore", but you will
not get a warning for it.
In short, you could try :set virtualedit=onemore, and see if your environment is stable or not with it.
Use d?c
That will start d mode, search back to 'c' and then delete up to your cursor position.
Edit: nope, that does not include current position...
I may be misunderstanding your request, but does 3hd$ do it?
I would use vFdd in this example. I think it's nicer than the other solutions since the command explicitly shows what to delete. It includes the current character and the specified character when deleting.
v: enter visual mode (mark text)
F: find/goto character backwards
d: the character "d" that will be included for removal.
d: delete command
Since it is visual mode, the cursor can also be moved before executing the actual removal d. This makes the command powerful even for deleting up to a non unique character by first marking a special character close to the character and then adjusting the position.

vi to delete from the beginning of the line till the cursor

How can we use vim to delete characters from the beginning of the line till the cursor.
Say, we have a string "hello world" while the cursor is on "w". How can we delete from "h" till "w".
Try d0. 0 denotes the beginning of the line.
I believe that the following should work (d^):
d^
This assumes that you only want to delete to the h even if there is white space in front of it. It will leave the white space.
TLDR:
The easiest way is to do the following
use the navigate mode
set the cursor at whatever line you want
press dgg - this will remove everything from cursor to the beginning
press dG - this will remove all the lines from cursor till the end of the doc.
But why?
gg - goes to the begin of the document
G - navigates at its very end
d - delete mode
I was also looking for some combinations with the notation of d and line number, and when you are editing huge files like GB in size I found it bit annoying.
Like to remove first 3 lines you need to do this :0,d3, but when the docu is over 1mln lines its tough for my eyes to read this crap...
The other suggestions didn't work for me, but I used visual mode to achieve this.
I moved the cursor to the position I wanted to delete through, and hit v to enter visual mode. Then I hit ^ to select back to the beginning of the current line, and then d to delete all of the highlighted text.
If you're in insert mode and you want to delete to the start of the line (and stay in insert mode), you can use CTRL+u
This matches the bash meaning of CTRL+u as shown here.
I don't know if this is undocumented or just a quirk of my setup (neovim on Ubuntu) but doing :help CTRL-U shows the help for the keystroke out of insert mode. Maybe someone can help me find the help which points to the usage I'm describing here.
not sure what happened but when I enter the d0, the line got deleted from my
cursor location to start of the line. – YouAreAwesome Dec 14 '17 at 6:43
Confirming that this worked. It seems to be the simplest method. (You can't upvote a comment, so I'm adding it as an answer in it's own right.)
Example: In .ssh/authorized_keys, I had:
no-port-forwarding,no-agent-forwarding,no-X11-forwarding,command="echo 'Please login as the user "centos" rather than the user "root".';echo;sleep 10" ssh-rsa AAAAB3NzaC1yc2...
This is the way Amazon AWS keeps you from logging in as root.
I put the cursor on the s in ssh-rsa and hit 'd0' and got a perfect delete to beginning of line.
Well, if your cursor is on "w", you'll be deleting backwards...
You can use vim's "till" t, but moving backwards it requires an uppercase T.
The same works for "find" f moving backwards it's F.
So, in your case, you can delete back "till" the quote symbol:
dT"
Or, if to prefer targeting/finding the "h":
dFh
Try jumping around first without the delete to get a feel for it, then you can just layer in the action as the prefix.
Happy Vimming! :)

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