Lets say that i have this text:
$test = 'lorem';
$test2= 'ipsum';
and I want to copy lorem and paste into ipsum.
I tried to do yi' on lorem and then went on ipsum and did ci' but that replaced my pastebin with ipsum. and my previous copy was lost.
yi' on lorem, move to i of ipsum, vep?
Easy:
"kyi' on lorem, move to i of ipsum, ve"kp
This yanks lorem into register k, pastes it over ipsum from register k, and keeps it in register k ready to paste again anywhere else you might want to put it. ipsum still ends up in the default register, but that's no longer a problem and could be useful too.
If you've already got something in register k, you can use a different register instead (just use a different key).
I usually go to the sed command.
:%s/ipsum/lorem/g
% means do this for every line
s means sed, or search and replace
g at the end means replace every ipsum with lorem; if you omit this, it only replaces the first.
Go to the l of lorem, ye (yank to end of word).
Go to the i of ipsum, "_de (delete to end of word, putting the deleted text in the black hole register.
P (paste register before cursor).
Altogether: yej"_deP
Why don't you yank into a named buffer, using "ayi', then delete and paste with d'i"aP?
I'm not sure what do you want exactly.
You have this on two lines:
$test = 'lorem';
$test2= 'ipsum';
If you go to l and yw, it yanks lorem,
then go to i of ipsum, and cw, and p and it will replace ipsum with lorem.
You will still have both lorem and ipsum in the registers.
You can get the contents of those registers with :reg
If you want to paste something from them then "*p, or ":p, or "0p (you'll see it when you type :reg)
vi'y on lorem
vi'p on ipsum
gvy to copy back lorem to register for possible macro with vi'p
(qa - gvy - j - vi'p - q - #a - ## - ## ...)
Words here to stop Markdown treating the code that follows as plain text.
/lorem
"adw
/ipsum
"aP"bdw
``
"bp
The first text searches for 'lorem'; the next deletes the word into the buffer named 'a', leaving a pair of empty quotes behind in the text. The next search finds 'ipsum'; the "aP pulls the buffer named 'a' in before the word ipsum; the "bdw deletes the word into the buffer named 'b', leaving 'lorem' behind. The double back-tick goes back to the place the last search came from - the empty quotes; and "bp pulls the named buffer after the first quote.
You can also omit the "a and "b, but this way, the values are in the named buffers "a and "b and can be copied again, and again, and again until you don't need the values any more.
After you do ci' on impsum your lorem is in register "0. So, you can do ci'^R0 (^R means Ctrl+r) and paste your lorem in place of ipsum.
See :help quote_number for more info on numbered registers.
You want to use y to copy a selection and p to paste.
Here is a good list to keep handy.
Related
In https://stackoverflow.com/a/11790464/5354137, there is a very detailed description how to, e.g. perform the same insert to the beginnings of all selected lines by placing the cursor over the first character of the first line, then after <C-v> moving down, then Isomething<Esc><down> inserting something everywhere.
But how does one do the same thing in the case of lines of unequal length, such as:
'Lorem ipsum
'dolor sit amet,
'consectetur adipiscing elit,
'sed do...
to their respective line endings? E.g. if I wanted to insert ', after m, ,, , and ., how would I do that without resorting to :substitute? (Or to a plugin, just using original vim-fu...)
Substitute (original answer)
The easiest way to do it is with the :substitute command. Select the lines in visual mode and then do:
:'<,'>s/$/',/<CR>
The $ stands for the end of the line. Note that vim will already write the :'<,'> part, so you only have to type from there.
Using a macro
With the question having been updated and :substitute off the table, I'd suggest recording a macro. Place the cursor on the line with "Lorm ipsum" and do:
qaA',<Esc>jq
then call the macro three times for the remaining lines. Either by hitting # a lot:
#a####
or with the more elegant
3#a
Using a recursive macro
As commented by #AndR, it's possible to record a macro that calls itself which will run until it reaches the end of the buffer:
qaqqaA',<Esc>j#aq
Note that the macro already calls itself with #a.
Afterwards, call #a and single quotes will be appended to every line until the end of the buffer.
Without visual selection
Both #AndR's answer using :normal and my :substitute answer above rely on a visual selection of the lines to modify. It is possible without that by typing in a range or line numbers. For "Lorem ipsum..." on lines 1 through 4, type either of the two commands:
:1,4s/$/',/
:1,4norm A',
This has the benefit that it works without a visual selection and from anywhere in the buffer.
Based on your reference link and reproduction steps, this is your problem halfway:
But how does one do the same thing in the case of lines of unequal length ...
Follow the link and use the first method Using only visual-block mode, let's start with step 5:
Select any column, hit <C-v> to enter visual-block mode and expand your selection toward the bottom:
'Lo[r]em ipsum
'do[l]or sit amet,
'co[n]sectetur adipiscing elit,
'se[d] do...
Hit $ or <End> (see :help $), will get the selection like this:
'Lo[rem ipsum]
'do[lor sit amet,]
'co[nsectetur adipiscing elit,]
'se[d do...]
Hit A',<Esc> to achieve your goal:
'Lorem ipsum',
'dolor sit amet,',
'consectetur adipiscing elit,',
'sed do...',
Note and supplement:
If you use the latest Vim9 and your virtualedit option contains block, when you hit $ or <End>, you may see this selection (Vim8 won't):
'Lo[rem ipsum ]
'do[lor sit amet, ]
'co[nsectetur adipiscing elit, ]
'se[d do... ]
This could be a issue with the VIM9 or an intentional design? I don't know, but don't care, just press A. You'll see your cursor at the end of the first line, which is fine (| is the cursor position):
'Lorem ipsum|
'dolor sit amet,
'consectetur adipiscing elit,
'sed do...
Then you enter the text you need (e.g. ',) and <Esc> ends editing, it works fine:
'Lorem ipsum',
'dolor sit amet,',
'consectetur adipiscing elit,',
'sed do...',
As a supplement, oppositely, if you want to insert text in the same column where has no text when there are irregular endings : You can add block to your virtualedit option (see :h 'virtualedit'), using other left-right-motions (e.g. l or |) instead of $ to get this selection:
'Lo[rem ipsum ]
'do[lor sit amet, ]
'co[nsectetur adipiscing elit, ]
'se[d do... ]
When you press A, the cursor will be at (| is the cursor position):
'Lorem ipsum |
'dolor sit amet,
'consectetur adipiscing elit,
'sed do...
Then enter the text you want (e.g. ',), hit <Esc> to obtain:
'Lorem ipsum ',
'dolor sit amet, ',
'consectetur adipiscing elit, ',
'sed do... ',
Here the goal can be achieved by appending ' at the end of each line. In one line you'd normally do A'. In order to do this this in each line of the visual selection, run
:<,>norm A'
Assuming that you have selected the needed lines visually (with v or V).
See :h :normal for details.
I'm switching my TextEditor from Sublime Text to Vim with iTerm2 recently.
I'm looking for a plugin to paste text in Multiple Position of Multiple Line on Vim.
(Similar to the multicursor on Sublime Text which I can select cursor and use CMD+Click to select in other position then paste.)
I've seen vim-multiple-cursors plugin but it will only allow me to select the same word of next occurrence.
e.g - to place cursor at different location on different line as below.,
Line 1: Lorem ipsum dolor sit amet, [CURSOR HERE] consectetuer adipiscing elit. Aenean
Line 2: commodo ligula [CURSOR HERE] eget dolor. Aenean massa. Cum sociis natoque
Line 3: penatibus et magnis dis parturient montes,[CURSOR HERE] nascetur ridiculus mus.
Line 4: Donec [CURSOR HERE] quam felis, ultricies nec, pellentesque eu,pretium quis, sem.
Your help would be greatly appreciated and Many Thanks in advance.
VIM Version
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Apr 21 2014 14:54:22)
MacOS X (unix) version
Warning
I recommend against trying to port your previous editing habits unchanged to Vim; after all, Vim's mode-based editing and command-based syntax are its distingishing features and make editing highly efficient.
Vim way
The "Vim way" would be more like using a regular expression to specify the places, moving to the first match, pasting, and then repeating with n and .
Emulation
You can sort-of emulate your intended workflow by manually inserting a marker char (e.g. §) at each location, and then replacing it via:
:%s/§/\=##/g
This uses :help sub-replace-expr to replace with the default register's contents.
Vim has a different buffer that stores what you have copied. To paste you just have to use "P" and the content is pasted.
Or I would recommend you to use "." [dot] - in Vim it is used for last action. So, once you have written/copied and pasted it in one line, you can just use the "dot" everywhere you want the same text to be pasted (i.e. last action to be taken).
The canonical Vim way to do that, the "dot formula" as coined by Drew Neil, is the reverse of the Sublime Text way.
In Sublime, you mark each match of a pattern, before doing the change :
mark Cmd+D
mark Cmd+D
mark Cmd+D
mark Cmd+D
change foo
In Vim, you "mark" a pattern, do the change once and repeat it on further instances of that pattern:
mark *N
change ciwfoo<Esc>
repeat n.
repeat n.
repeat n.
But, since you have 7.4, you can use the "new" gn normal mode command/motion:
nnoremap <key> *``cgn
and shorten the process above to:
change <key>foo<Esc>
repeat .
repeat .
repeat .
repeat .
Which is not that bad, I think.
I can do :%s/<search_string>/<replace_string>/g for replacing a string across a file, or :s/<search_string>/<replace_string>/ to replace in current line.
How can I select and replace words from selective lines in vim?
Example: replace text from lines 6-10, 14-18 but not from 11-13.
Replace All:
:%s/foo/bar/g
Find each occurrence of 'foo' (in all lines), and replace it with 'bar'.
For specific lines:
:6,10s/foo/bar/g
Change each 'foo' to 'bar' for all lines from line 6 to line 10 inclusive.
The :&& command repeats the last substitution with the same flags. You can supply the additional range(s) to it (and concatenate as many as you like):
:6,10s/<search_string>/<replace_string>/g | 14,18&&
If you have many ranges though, I'd rather use a loop:
:for range in split('6,10 14,18')| exe range 's/<search_string>/<replace_string>/g' | endfor
As a side note, instead of having to type in the line numbers, just highlight the lines where you want to find/replace in one of the visual modes:
VISUAL mode (V)
VISUAL BLOCK mode (Ctrl+V)
VISUAL LINE mode (Shift+V, works best in your case)
Once you selected the lines to replace, type your command:
:s/<search_string>/<replace_string>/g
You'll note that the range '<,'> will be inserted automatically for you:
:'<,'>s/<search_string>/<replace_string>/g
Here '< simply means first highlighted line, and '> means last highlighted line.
Note that the behaviour might be unexpected when in NORMAL mode: '< and '> point to the start and end of the last highlight done in one of the VISUAL modes. Instead, in NORMAL mode, the special line number . can be used, which simply means current line. Hence, you can find/replace only on the current line like this:
:.s/<search_string>/<replace_string>/g
Another thing to note is that inserting a second : between the range and the find/replace command does no harm, in other words, these commands will still work:
:'<,'>:s/<search_string>/<replace_string>/g
:.:s/<search_string>/<replace_string>/g
Search and replace
:%s/search\|search2\|search3/replace/gci
g => global search
c => Ask for confirmation first
i => Case insensitive
If you want direct replacement without confirmation, use below command
:%s/search/replace/g
If you want confirmation for each replace then run the below command
:%s/search/replace/gc
Ask for confirmation first, here search will be case insensitive.
:%s/search/replace/gci
You can do it with two find/replace sequences
:6,10s/<search_string>/<replace_string>/g
:14,18s/<search_string>/<replace_string>/g
The second time all you need to adjust is the range so instead of typing it all out, I would recall the last command and edit just the range
In vim if you are confused which all lines will be affected, Use below
:%s/foo/bar/gc
Change each 'foo' to 'bar', but ask for confirmation first.
Press 'y' for yes and 'n' for no. Dont forget to save after that
:wq
VI search and replace command examples
Let us say you would like to find a word called “foo” and replace with “bar”.
First hit [Esc] key
Type : (colon) followed by %s/foo/bar/ and hit [Enter] key
:%s/foo/bar/
We don't need to bother entering the current line number.
If you would like to change each foo to bar for current line (.) and the two next lines (+2), simply do:
:.,+2s/foo/bar/g
If you want to confirm before changes are made, replace g with gc:
:.,+2s/foo/bar/gc
To answer this question:
:40,50s/foo/bar/g
replace foo with bar in these lines between the 40th line and the 50th line(inclusive), when execute this command you can currently in any line.
:50,40s/foo/bar/g
also works, vim will ask you for comfirm and then do the replacement for you as if you have input the first command.
:,50s/foo/bar/g
replace foo with bar in these lines between the line you are currently in and the 50th line(inclusive). (if you are in a line AFTER the 50th line vim will ask for confirm again)
To clearity the difference between vim and the vrapper plugin of Eclipse:
Note that in varpper
:,50s/foo/bar/g command will NOT work.
:50,40s/foo/bar/g will work without asking for comfirmation.
(For Vrapper Version 0.74.0 or older).
Specifying the range through visual selection is ok but when there are very
simple operations over just a couple of lines that can be selected by an
operator the best would be to apply these commands as operators.
This sadly can't be done through standards vim commands. You could do a sort
of workaround using the ! (filter) operator and any text object. For
example, to apply the operation to a paragraph, you can do:
!ip
This has to be read as "Apply the operator ! inside a paragraph". The filter
operator starts command mode and automatically insert the range of lines
followed by a literal "!" that you can delete just after.
If you apply this, to the following paragraph:
1
2 Repellendus qui velit vel ullam!
3 ipsam sint modi! velit ipsam sint
4 modi! Debitis dolorum distinctio
5 mollitia vel ullam! Repellendus qui
6 Debitis dolorum distinctio mollitia
7 vel ullam! ipsam
8
9 More text around here
The result after pressing "!ap" would be like:
:.,.+5
As the '.' (point) means the current line, the range between the current
line and the 5 lines after will be used for the operation. Now you can add
the substitute command the same way as previously.
The bad part is that this is not easier that selecting the text for latter
applying the operator. The good part is that this can repeat the insertion
of the range for other similar text ranges (in this case, paragraphs) with
sightly different size. I.e., if you later want to select the range bigger
paragraph the '.' will to it right.
Also, if you like the idea of using semantic text objects to select the
range of operation, you can check my plugin
EXtend.vim that can do the same
but in an easier manner.
/sys/sim/source/gm/kg/jl/ls/owow/lsal
/sys/sim/source/gm/kg/jl/ls/owow/lsal
/sys/sim/source/gm/kg/jl/ls/owow/lsal
/sys/sim/source/gm/kg/jl/ls/owow/lsal
/sys/sim/source/gm/kg/jl/ls/owow/lsal
/sys/sim/source/gm/kg/jl/ls/owow/lsal
/sys/sim/source/gm/kg/jl/ls/owow/lsal
/sys/sim/source/gm/kg/jl/ls/owow/lsal
Suppose if you want to replace the above with some other info.
COMMAND(:%s/\/sys\/sim\/source\/gm\/kg\/jl\/ls\/owow\/lsal/sys.pkg.mpu.umc.kdk./g)
In this the above will be get replaced with (sys.pkg.mpu.umc.kdk.) .
In vim, I often find myself deleting (or copying) large blocks of text. One can count the lines of text and say (for example) 50dd to delete 50 lines.
But how would one delete this large block of text without having to know how many lines to delete?
Go to the starting line and type ma (mark "a"). Then go to the last line and enter d'a (delete to mark "a").
That will delete all lines from the current to the marked one (inclusive). It's also compatible with vi as well as vim, on the off chance that your environment is not blessed with the latter.
I'm no vim guru, but what I use in this circumstance is "visual mode". In command mode, type V (capital). Then move up/down to highlight the block you want deleted (all the usual movement commands work). Then remove it with x or d.
You can use the visual mode also (some commands are usable with the delete option also)
vip vap to select paragraph, v2ap to select two paragraphs
dap works, d2ap also. You can delete within blocks of [ ] like da[
For reference: the types of objects.
From vim documentation : section 4. http://vimdoc.sourceforge.net/htmldoc/visual.html
4. Operating on the Visual area *visual-operators*
...
The objects that can be used are:
aw a word (with white space)
iw inner word
aW a WORD (with white space)
iW inner WORD
as a sentence (with white space)
is inner sentence
ap a paragraph (with white space)
ip inner paragraph
ab a () block (with parenthesis)
ib inner () block
aB a {} block (with braces)
iB inner {} block
a< a <> block (with <>)
i< inner <> block
a[ a [] block (with [])
i[ inner [] block
There are many better answers here, but for completeness I will mention the method I used to use before reading some of the great answers mentioned above.
Suppose you want to delete from lines 24-39. You can use the ex command
:24,39d
You can also yank lines using
:24,39y
And find and replace just over lines 24-39 using
:24,39s/find/replace/g
It sort of depends on what that large block is. Maybe you just mean to delete a paragraph in which case a dip would do.
If you turn on line numbers via set number you can simply dNNG which will delete to line NN from the current position. So you can navigate to the start of the line you wish to delete and simply d50G assuming that is the last line you wish to delete.
There are several possibilities, what's best depends on the text you work on.
Two possibilities come to mind:
switch to visual mode (V, S-V,
...), select the text with cursor
movement and press d
delete a whole paragraph with: dap
If the entire block is visible on the screen, you can use relativenumber setting. See :help relativenumber. Available in 7.3
Counting lines is too tedious for me, but counting 'paragraphs' isn't so bad. '{' and '}' move the cursor to the first empty line before and after the cursor, respectively. Cursor moving operations can be combined with deletion, and several other answers used a similar approach (dd for a line, dG for the end of the document, etc.)
For example:
/* Lorem ipsum dolor sit amet, consectetur adipiscing elit. */
Lorem *ipsum(void) {
return dolor(sit, amet);
}
If your cursor starts above the comment block, 'd}' deletes the comment block, and 'd2}' deletes both the comment block and the code block. If your cursor starts below the code block, 'd{' deletes the code, and 'd2{' deletes both. Of course, you can skip over one block by moving the cursor first: '{d{' or '}d}'.
If you're consistent with your whitespace, or you can count the paragraphs at a glance, this should work. The Vim help file has more cursor tricks if you're interested.
You could place your cursor at the beginning or end of the block and enter visual mode (shift-v). Then simply move up or down until the desired block is highlighted. Finally, copy the text by pressing y or cut the text by pressing d.
Alongside with other motions that are already mentioned here, there is also /{pattern}<CR> motion, so if you know that you want to delete to line that contains foo, you could do dV/foo<CR>. V is here to force motion be line-wise because by default / is characterwise.
You can also enter a very large number, and then press dd if you wish to delete all the lines below the cursor.
Deleting a block of text
Assuming your cursor sits at the beginning of the block:
V/^$<CR>d (where <CR> is the enter/return key)
Explanation
Enter "linewise-visual" mode: V
Highlight until the next empty line: /^$<CR>
Delete: d
Key binding
A more robust solution:
:set nowrapscan
:nnoremap D V/^\s*$\\|\%$<CR>d
Explanation
Disable search wrap: :set nowrapscan
Remap the D key (to the following commands): :nnoremap D
Enter "linewise-visual" mode: V
Highlight until the next empty/whitespace line or EOF: /^\s*$\\|\%$<CR>
Delete: d
I'm an experienced developer on the Windows platform and I'm trying to teach myself how to use Vim. I am pretty good with regular expressions and understand the principles of how to use Vim. However, I have a specific problem, and although I have a solution, it feels as though there ought to be a better one.
I have a file which contains a line similar to the following:
CODE<tab><tab>Lorem ipsum dolor sit amet
There could be a variable number of <tab> or <space> characters between CODE and Lorem. Assuming the cursor is over the 'C' of CODE in normal mode, what I want to be able to do is find a key combination that will produce the following output, and leave the cursor between the 'E' of CODE and he 'L' of Lorem in insert mode.
CODELorem ipsum dolor sit amet
My curent solution is to use the following key sequence:
w d ? \ s \ + <return>
This works, but it feels illogical to go past the thing I want to delete before I can delete it. I feel like I should move to the end of CODE and delete forwards. I realise this could simply be a Vim idiom that I'm not aware of. I could also be totally missing a key piece of Vim knowledge.
What's the best way to achieve my goal?
eldw will do it.
e - move forward to the end of the word
l - move 1 character to the right
dw - delete all the blanks
If your cursor is in the middle of some whitespace, diw will delete whitespace left and right of the cursor. (If it is somewhere in the middle of a word, it will delete the word.)
Some alternatives:
:s/\s\+//
e xx
elxx
eldtL
f<tab>xx
eldw