Reputation: 30308
Sometimes I want to edit a certain visual block of text across multiple lines.
For example, I would take a text that looks like this:
name
comment
phone
email
And make it look like this
vendor_name
vendor_comment
vendor_phone
vendor_email
Currently the way I would do it now is...
" vendor_"
without the quote. Notice the extra space we had to put back.I don't need to indent if there is at least one column of whitespace before the words. I wouldn't need the whitespace if I didn't have to clear the visual block with c.
But if I have to clear, then is there a way to do what I performed above without creating the needed whitespace with indentation?
Also why does editing multiple lines at once only work by exiting out of insert mode with Esc over Ctrlc?
Here is a more complicated example:
name = models.CharField( max_length = 135 )
comment = models.TextField( blank = True )
phone = models.CharField( max_length = 135, blank = True )
email = models.EmailField( blank = True )
to
name = models.whatever.CharField( max_length = 135 )
comment = models.whatever.TextField( blank = True )
phone = models.whatever.CharField( max_length = 135, blank = True )
email = models.whatever.EmailField( blank = True )
In this example I would perform the vertical visual block over the .
, and then reinsert it back during insert mode, i.e., type .whatever.
. Hopefully now you can see the drawback to this method. I am limited to only selecting a column of text that are all the same in a vertical position.
Upvotes: 495
Views: 343856
Reputation: 81
you can do with replace with new line (except first line)
name
comment
phone
email
with
%s/\n/\rvendor_/g
and its replace every new line with new line + vendor_ string so seems like this
name
vendor_comment
vendor_phone
vendor_email
you can edit first line manuel
Upvotes: 0
Reputation: 652
An alternative that can be more flexible:
Example: To enter the text XYZ at the beginning of the line
:%norm IXYZ
What's happening here?
%
== Execute on every linenorm
== Execute the following keys in normal mode (short for normal
)I
== Insert at beginning of lineXYZ
== The text you want to enterThen you hit Enter, and it executes.
Specific to your request:
:%norm Ivendor_
You can also choose a particular range:
:2,4norm Ivendor_
Or execute over a selected visual range:
:'<,'>norm Ivendor_
Or execute for each line that matches a 'target' regex:
:%g/target/norm Ivendor_
Upvotes: 20
Reputation: 1224
If the change is required in the entire file,
:1,$s/^/vendor_/
If the change is required for only a few lines,
Go to the first line where change is required, and either give the command
:.,ns/^/vendor_/
Substitute n with the line number of the last line in the block.
Or,
:.,+ns/^/vendor_/
Substitute n with number of lines minus 1 in which the change is required.
Upvotes: 1
Reputation: 141
:.,+3s/^/vendor_/
Another example, I needed to just add two spaces to a block of 125 lines, so I used (with cursor positioned at the beginning of the first line of the block):
:.,+125s/^/ /
Worked great.
Upvotes: 1
Reputation: 608
I came here to paste in many lines an already copied string. When copy with y
we can paste, in the INSERT MODE, pressing Ctrl+r and right after press ''. This will have the same result as being in NORMAL MODE and press p. This is called paste from registry.
Suppose the following text in the buffer:
vendor_something
text
to_receive
the_paste
pattern
Then we can put the cursor pointing to v in vendor_ and press v, move to right using l until select the underscore symbol we want to paste in the text bellow. After that, we can point the cursor at the beginning of "text" (two lines bellow vendor_something) and press Ctrl+v. Then I
to go into INSERT MODE where we press 3j
Ctrl+r '' Esc. The result of this sequence will be:
vendor_something
vendor_text
vendor_to_receive
vendor_the_paste
vendor_pattern
Upvotes: 1
Reputation: 1312
Another approach is to use the . (dot
) command in combination with i.
vendor_
)I find this technique is often faster than the visual block mode for small numbers of additions and has the added benefit that if you don't need to insert the text on every single line in a range you can easily skip them by pressing extra j's.
Note that for large number of contiguous additions, the block approach or macro will likely be superior.
Upvotes: 122
Reputation: 129139
n
in name
.I
(capital i).vendor_
. Note: It will only update the screen in the first line - until Esc is pressed (6.), at which point all lines will be updated.An uppercase I
must be used rather than a lowercase i
, because the lowercase i
is interpreted as the start of a text object, which is rather useful on its own, e.g. for selecting inside a tag block (it
):
Upvotes: 1330
Reputation: 2877
Press:
Type the new text.
Upvotes: 89
Reputation: 5247
Upvotes: 4
Reputation: 3522
Suppose you have this file:
something
name
comment
phone
email
something else
and more ...
You want to add "vendor_" in front of "name", "comment", "phone", and "email", regardless of where they appear in the file.
:%s/\<\(name\|comment\|phone\|email\)\>/vendor_\1/gc
The c
flag will prompt you for confirmation. You can drop that if you don't want the prompt.
Upvotes: 7
Reputation: 32399
I would use a macro to record my actions and would then repeat it.
You now have a nice macro.
Type 3@q to execute your macro three times to do the rest of the lines.
Upvotes: 43
Reputation: 21934
You might also have a use case where you want to delete a block of text and replace it.
Like this
Hello World
Hello World
To
Hello Cool
Hello Cool
You can just visual block select "World" in both lines.
Type c for change - now you will be in insert mode.
Insert the stuff you want and hit escape.
Both get reflected vertically. It works just like 'I', except that it replaces the block with the new text instead of inserting it.
Upvotes: 10
Reputation: 5129
I wanted to comment out a lot of lines in some config file on a server that only had vi (no nano), so visual method was cumbersome as well Here's how i did that.
vi file
:set number!
or :set number
:35,77s/^/#/
Note: the numbers are inclusive, lines from 35 to 77, both included will be modified.
To uncomment/undo that, simply use :35,77s/^#//
If you want to add a text word as a comment after every line of code, you can also use:
:35,77s/$/#test/
(for languages like Python)
:35,77s/;$/;\/\/test/
(for languages like Java)
credits/references:
Upvotes: 10
Reputation: 5247
Use Ctrl+V to enter visual block mode
Move Up/Down to select the columns of text in the lines you want to comment.
Then hit Shift+i and type the text you want to insert.
Then hit Esc, wait 1 second and the inserted text will appear on every line
Upvotes: 5
Reputation: 37155
Updated January 2016
Whilst the accepted answer is a great solution, this is actually slightly fewer keystrokes, and scales better - based in principle on the accepted answer.
n
in name
.I
.vendor_
.Note, this has fewer keystrokes than the accepted answer provided (compare Step 3). We just count the number of j actions to perform.
If you have line numbers enabled (as illustrated above), and know the line number you wish to move to, then step 3 can be changed to #G where # is the wanted line number.
In our example above, this would be 4G. However when dealing with just a few line numbers an explicit count works well.
Upvotes: 19