Difference Between Substitute and Change in Vim - vim

In Vim, what is the difference between s and c? If I substitute n letters or words, is there any difference to changing them? And are there any instances where substituting and changing are different?

When in doubt, check the :help:
:help s
*s*
["x]s Delete [count] characters [into register x] and start
insert (s stands for Substitute). Synonym for "cl"
(not |linewise|).
:help c
*c*
["x]c{motion} Delete {motion} text [into register x] and start
insert. When 'cpoptions' includes the 'E' flag and
there is no text to delete (e.g., with "cTx" when the
cursor is just after an 'x'), an error occurs and
insert mode does not start (this is Vi compatible).
When 'cpoptions' does not include the 'E' flag, the
"c" command always starts insert mode, even if there
is no text to delete.
TL;DR: c takes normal movements (like cw to change the word you're on) to determine how much text to delete before entering insert. s takes a number (like 10s) to determine how much text to delete before entering insert. That being said, you could do <num>s with c, by using c<num>l (and the help mentions that s is, in fact, a synonym for cl).

Related

Move cursor to end of line in vim (to delete last word)

I usually type the command db to remove one word backwards in Vim. Doing this at the end of a line without whitespace leaves the last character of the word since the cursor starts removing from the second last character.
I can press b followed by de instead, but I find this confusing sometimes when doing it many times in a row, leading to unneccesary mistakes. I was hoping there was a way to go to the end of the line and remove the last word backwards.
Is there a way to do this?
You should train yourself to stop using db and learn to love text objects instead. This problem happens even if you're not at the end of a line.
foo bar baz
^
and then db will give
foo r bax
^
Instead of using db or de or even dw, train yourself to use diw and daw. This might take some getting used to, but down the road it will become natural and be way more convenient to use. And once the text objects become natural to you, you can then start using other commands ('c' or 'y', etc.) or even other text objects. (I can't even count how many times I've used dap or ci()
For a short explanation of what these do, think of diw as (d)elete (i)nside this (w)ord and daw as (d)elete (a)round this (w)ord or (d)elete (a) (w)ord. For a more concrete example:
foo bar baz
^
typing diw gives
foo bar
^
(note the leading space)
But
foo bar baz
^
typing daw gives
foo bar
^
And as long as your cursor is over the same word, these will still do the same thing. So if we moved the cursor back to the 'a' or the 'b', you'll get the exact same results. This blog goes into more depth on text objects.
You can either:
use a mapping like this:
:nnoremap <silent> db :exe 'norm!' (col('.')==col('$')-1 ? 'daw' : 'db')<cr>
alternatively, you can do :set ve=onemore which allows you to go one
char past the end of line, then once your cursor is past the end (see command g$), simply use db.
EDIT (explanations)
:exe executes the string formed by its arguments as a command.
So if the cursor is at the end of line (col('.')==col('$')-1),
it will execute:
:norm! daw
otherwise it will execute:
:norm! db
:norm lets you run normal commands in the command line. For example if you're already in
normal mode, typing :norm db + return will do the same as just typing db.
It's useful inside commands. The ! prevents mappings to be used in the :norm commands.
See :h :norm.
<silent> makes a mapping silent: without it you would see
:exe 'norm!' (col('.')==col('$')-1 ? 'daw' : 'db') in the bottom of the
screen.
See :h <silent>.
<cr> is the key code for the return key. You can put this kind of codes inside mappings.
In this case, <cr> will validate the command just entered in the mapping.
See :h keycodes for details.
What about
$diw
$ - takes you to last character of the line
d - as you know is for delete
i - implies from inside. Read more on other powerful text objects :help text-objects
w - word
If you are not habituated using text-objects, start using. You will love it

How to select the similar text in gvim and modify them?

How I can select all the text start with foo_list starting from line 4 (see. below code) and rename them with list_values or any other preferred name ? Please note, I don't want to change in the first line.
Thanks in advance !
foo_list = [5, 2, 3, 1, 4]
def reverse_list_1():
foo_list=[0,10,20,40]
for i in reversed(foo_list):
print i,
foo_list=[0,10,20,40]
print foo_list[::-1]
for i in reversed(foo_list):
print i,
length = len(foo_list)
for i in range(length):
print foo_list[length-i-1],
Preferable solution: key map in the .vimrc or .gvimrc file, don't want to use any plugin.
That's a job for :substitute. You can specify the range with explicit line numbers (here: 4 to end of buffer $, or maybe next empty line /^$/):
:4,$substitute/\<foo_list\>/list_values/g
You can also first move to the first line and use the .,$ range.
Since that's still a lot of typing, you can pull in the current word (assuming you first position the cursor on the foo_list occurrence in line 4) into the command line via <C-R><C-W>.
Or, for a plugin solution, my ChangeGlobally plugin provides a mapping that avoids the use of :s.
:%s/foo_list/list_values/gc
This command says to replace the word foo_list with list_values in the whole document, asking for confirmation each time. Then for the first occurrence of foo_list on line 1, press n (to indicate NO) , and press y (to indicate YES) for all further occurrences to replace them. This solution works when you have to replace a few words. You can read the command as follows:
In the whole document (%), substitute (s) the word foo_list with list_values and do this globally (g), asking for confirmation (c) each time. For more options in the substitute command type :help :s in vim.
Solution 2 :
When there are thousands of words to replace, you surely don't want to type a y/n confirmation each time (which is enabled by the c flag in the end in the above command).
Take your cursor to line 4 and run
:.,$s/foo_list/list_values/g
Read the above command as from here (.) to the end of file (,$)
replace (s) the word foo_list with list_values
globally (g).
For small changes like this I like to use the gn motion. The gn motion visually selects the current search pattern. This makes for a powerful search/replace method when combining the gn motion with the change, c, operator and the repeat command, ..
Basic steps:
Make foo_list your search pattern. e.g. /foo_list or via *
Use c and gn to change the first foo_list. e.g. cgnbar_list<esc>
Now repeat that change on the next search result via .
Use n to advance to the next search results. (Hit n twice to skip an occurrence)
Keep using n and . until done
There is a nice Vimcasts episode on this topic: Operating on search matches using gn
For more information see:
:h gn
:substitute is the 'correct' way, but if you're only making a few changes, and you are not very experienced with ex commands, sometimes it takes longer to think through the command than to bounce through the list of changes you want to make using motions.
If you start with your cursor on the first instance of foo_list, hit '*' to jump to the next occurrence in the file. Hit 'ce' to delete to the end of the word and enter insert mode. Type in your new variable name and return to normal mode. Now you can jump through the rest of the file using 'n' to jump to the next occurrence (or 'N' to go back), and '.' to repeat your last edit action.

Replace inside a replace in Vim

How do you replace inside a replace?
I know you can get the current word under the cursor into the replace, but I want to perform a replace on it before having it as the output.
For example:
Having a document that has passed through the clbuttic filter and replacing words that you know have been affected for the worse, such as deinstitutionalization (Hint: Consbreastution of the United States of America). The file also has some words that are properly replaced.
Changing o to 0 in Wooloomooloo (and only for that word) throughout a document
A real world example (from my vim history) is being in a Latex document and running
%s/\\begin{table}\[h\]/\\begin{table}\[H\]/c
where I am duplicating the string just to get a minor change.
Replacing o to 0 is straightforward:
:s/o/0/g
Perhaps you want to know how to apply it to a (visual) selection?
:s/\%Vo/0/g
Edit Likely what you want (see comments)
:%s/\<\zs[Wolm]\+\>\ze/\=substitute(submatch(0), 'o', '0', 'g')/g
What this does:
[Wolm]\+ matches sequences consisting of just W,o,l,m
\<[Wolm]\+\> matches (independent) words of the same (so, Wooloomooloo, or mooW, Wlomo would all match, but not amool etc).
\zs marks the begin of the match for replace, \ze the end
\=substitute(submatch(0), 'o', '0', 'g') replaces the match (between \zs and \ze) but subsituting 0 for o
You can start from this pattern
replacing [Wolm]\+ with your actual target
adding optional context outside \zs...\ze so you can reduce matches
adding \c in the front of the pattern to enable case-insensitive matching
Try typing Ctrl-f while you're writing your :sub command. This will open the command-line window and allow you to use vim's normal mode to edit your command. If you press Enter in this mode, the current line will be executed. If you press Ctrl-c, you'll drop back to the command-line mode with the current line as the current text.
The command-line window is also what happens when you accidentally type q:. (I know I did that a lot before understanding what it's for. It also has history of your previous commands. Also try the same during a / search or type q/.
See also :help cmdline-window

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'.

How do I use vim registers?

I only know of one instance using registers is via CtrlR* whereby I paste text from a clipboard.
What are other uses of registers? How to use them?
Everything you know about VI registers (let's focus on vi 7.2) -- share with us.
Registers in Vim let you run actions or commands on text stored within them. To access a register, you type "a before a command, where a is the name of a register. If you want to copy the current line into register k, you can type
"kyy
Or you can append to a register by using a capital letter
"Kyy
You can then move through the document and paste it elsewhere using
"kp
To paste from system clipboard on Linux
"+p
To paste from system clipboard on Windows (or from "mouse highlight" clipboard on Linux)
"*p
To access all currently defined registers type
:reg
I was pleased when I discovered the 0 register. If you yank text without assigning it to a particular register, then it will be assigned to the 0 register, as well as being saved in the default " register. The difference between the 0 and " registers is that 0 is only populated with yanked text, whereas the default register is also populated with text deleted using d/D/x/X/c/C/s/S commands.
I find this useful when I want to copy some text, delete something and replace it with the copied text. The following steps illustrate an example:
Yank the text you want to copy with y[motion] - this text is saved in " and 0 registers
Delete the text you want to replace with d[motion] - this text is saved in " register
Paste the yanked text with "0p
where " is the command to use a register for the next command.
On the final step, if you were to paste from the default register (with p), it would use the text that you had just deleted (probably not what you intended).
Note that p or P pastes from the default register. The longhand equivalent would be ""p (or ""P) and "0 holds the last yank, "1holds the last delete or change.
For more info see :help registers.
One of my favorite parts about registers is using them as macros!
Let's say you are dealing with a tab-delimited value file as such:
ID Df %Dev Lambda
1 0 0.000000 0.313682
2 1 0.023113 0.304332
3 1 0.044869 0.295261
4 1 0.065347 0.286460
5 1 0.084623 0.277922
6 1 0.102767 0.269638
7 1 0.119845 0.261601
Now you decide that you need to add a percentage sign at the end of the %Dev field (starting from 2nd line). We'll make a simple macro in the (arbitrarily selected) m register as follows:
Press: qm: To start recording macro under m register.
EE: Go to the end of the 3rd column.
a: Insert mode to append to the end of this column.
%: Type the percent sign we want to add.
<ESC>: Get back into command mode.
j0: Go to beginning of next line.
q: Stop recording macro
We can now just type #m to run this macro on the current line. Furthermore, we can type ## to repeat, or 100#m to do this 100 times! Life's looking pretty good.
At this point you should be saying, "But what does this have to do with registers?"
Excellent point. Let's investigate what is in the contents of the m register by typing "mp. We then get the following:
EEa%<ESC>j0
At first this looks like you accidentally opened a binary file in notepad, but upon second glance, it's the exact sequence of characters in our macro!
You are a curious person, so let's do something interesting and edit this line of text to insert a ! instead of boring old %.
EEa!<ESC>j0
Then let's yank this into the n register by typing B"nyE. Then, just for kicks, let's run the n macro on a line of our data using #n....
It added a !.
Essentially, running a macro is like pressing the exact sequence of keys in that macro's register. If that isn't a cool register trick, I'll eat my hat.
Other useful registers:
"* or "+ - the contents of the system clipboard
"/ - last search command
": - last command-line command.
Note with vim macros, you can edit them, since they are just a list of the keystrokes used when recording the macro. So you can write to a text file the macro (using "ap to write macro a) and edit them, and load them into a register with "ay$. Nice way of storing useful macros.
The black hole register _ is the /dev/null of registers.
I use it in my vimrc to allow deleting single characters without updating the default register:
noremap x "_x
and to paste in visual mode without updating the default register:
vnoremap p "_dP
If you ever want to paste the contents of the register in an ex-mode command, hit <C-r><registerletter>.
Why would you use this? I wanted to do a search and replace for a longish string, so I selected it in visual mode, started typing out the search/replace expression :%s/[PASTE YANKED PHRASE]//g and went on my day.
If you only want to paste a single word in ex mode, can make sure the cursor is on it before entering ex mode, and then hit <C-r><C-w> when in ex mode to paste the word.
To make it more convenient :
cnoremap <c-g> <c-r>"
cnoremap <C-Right> <c-r><c-w>
A cool trick is to use "1p to paste the last delete/change (, and then use . to repeatedly to paste the subsequent deletes. In other words, "1p... is basically equivalent to "1p"2p"3p"4p.
You can use this to reverse-order a handful of lines:
dddddddddd"1p....
I think the secret guru register is the expression = register. It can be used for creative mappings.
:inoremap \d The current date <c-r>=system("date")<cr>
You can use it in conjunction with your system as above or get responses from custom VimL functions etc.
or just ad hoc stuff like
<c-r>=35+7<cr>
q5 records edits into register 5 (next q stops recording)
:reg show all registers and any contents in them
#5 execute register 5 macro (recorded edits)
From vim's help page:
CTRL-R {0-9a-z"%#:-=.} *c_CTRL-R* *c_<C-R>*
Insert the contents of a numbered or named register. Between
typing CTRL-R and the second character '"' will be displayed
<...snip...>
Special registers:
'"' the unnamed register, containing the text of
the last delete or yank
'%' the current file name
'#' the alternate file name
'*' the clipboard contents (X11: primary selection)
'+' the clipboard contents
'/' the last search pattern
':' the last command-line
'-' the last small (less than a line) delete
'.' the last inserted text
*c_CTRL-R_=*
'=' the expression register: you are prompted to
enter an expression (see |expression|)
(doesn't work at the expression prompt; some
things such as changing the buffer or current
window are not allowed to avoid side effects)
When the result is a |List| the items are used
as lines. They can have line breaks inside
too.
When the result is a Float it's automatically
converted to a String.
See |registers| about registers. {not in Vi}
<...snip...>
I use the default register to grep for text in my vim window without having to reach for the mouse.
yank text
:!grep "<CTRL-R>0"<CR>
Use registers in commands with #. E.g.:
echo #a
echo #0
echo #+
Set them in command:
let #a = 'abc'
Now "ap will paste abc.
One overlooked register is the '.' dot register which contains the last inserted text no matter how it was inserted eg ct] (change till ]). Then you realise you need to insert it elsewhere but can't use the dot repeat method.
:reg .
:%s/fred/<C-R>./
A big source of confusion is the default register ". It is important to know the way it works. It is much better if the default register is avoided most of the times. The explanation from the Vim documentation:
Vim fills this register with text deleted with the "d", "c", "s", "x" commands
or copied with the yank "y" command, regardless of whether or not a specific
register was used (e.g. "xdd). This is like the unnamed register is pointing
to the last used register.
So the default register is actually a pointer to the last used register. When you delete, or yank something this register is going to point to other registers. You can test that by checking the registers. There is always another register that is exactly the same as the default register: the yank register ("0) , the first delete register("1) , small delete register("-) or any other register that was used to delete or yank.
The only exception is the black hole register. Vim doc says:
An exception is the '_' register: "_dd does not store the deleted text in any
register.
Usually you are much better off by using directly: "0, "- and "1-"9 default registers or named registers.
My favorite register is the ':' register. Running #: in Normal mode allows me to repeat the previously ran ex command.
So we can verify some commands in with :MY_commandXXXXX then put MY_commandXXXXX in vimrc
My friend Brian wrote a comprehensive article on this. I think it is a great intro to how to use topics. https://www.brianstorti.com/vim-registers/
My favorite feature is the ability to append into registers by using capital letters. For example, say you want to move a subset of imports from buffer X to buffer Y.
Go to line x1 in buffer X.
Type "ayy to replace register a with the content of line x1.
Go to line x5.
Type "Ayy (capital A) to append line x5 at the end of register a.
Go to buffer Y and type "ap to paste
<content of line x1>
<content of line x5>

Resources