Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I insert text in the middle of the line to multiple lines in Vim?

Tags:

vim

vi

People also ask

How do I put multiple lines of text in Vim?

vim Inserting text Insert text into multiple lines at oncePress Ctrl + v to enter into visual block mode. Use ↑ / ↓ / j / k to select multiple lines. Press Shift + i and start typing what you want. After you press Esc , the text will be inserted into all the lines you selected.

How do you go to the middle of a line in Vim?

Vim Screen Navigation H – Go to the first line of current screen. M – Go to the middle line of current screen.

How do I move text to the next line in Vim?

I know that pressing 'o' in normal or visual mode moves the cursor to a new line and switches the mode to insert.


Go to the first foo, press Ctrl-v to enter visual block mode and press down until all the lines with foo are marked. Then press Shift-i to insert at the beginning (of the block). When you are finished and press Esc, the inserted characters will be added to each line at the left of the marked block.

To insert at the end, press again Ctrl-v, move up/down to mark all affected lines and then press End or $ to extend the selection until the end of the lines. Now you can press Shift-a to append at the end of all the lines, just like previously with Shift-i.

The visual selection can also be done with normal movement commands. So to comment a whole block in C you could move to the opening brace and type Ctrl-v % Shift-i // Esc.


To answer your first question, the below

:%s/foo/bar(&)/g

will look for foo, and surround the matched pattern with bar(). The /g will do this multiple times in one line.

Since you're just matching foo, you could do a simple :s/foo/bar(foo)/g. The above will work, however, if you decide to match on a regular expression rather than a simple word (e.g. f[a-z][a-z]). The '&' in the above represents what you've matched.


To prefix a set of lines I use one of two different approaches:

One approach is the block select (mentioned by sth). In general, you can select a rectangular region with ctrl-V followed by cursor-movement. Once you've highlighted a rectangle, pressing shift-I will insert characters on the left side of the rectangle, or shift-A will append them on the right side of the rectangle. So you can use this technique to make a rectangle that includes the left-most column of the lines you want to prefix, hit shift-I, type the prefix, and then hit escape.

The other approach is to use a substitution (as mentioned by Brian Agnew). Brian's substitution will affect the entire file (the % in the command means "all lines"). To affect just a few lines the easiest approach is to hit shift-V (which enables visual-line mode) while on the first/last line, and then move to the last/first line. Then type:

:s/^/YOUR PREFIX/

The ^ is a regex (in this case, the beginning of the line). By typing this in visual line mode you'll see '<,'> inserted before the s automatically. This means the range of the substitution will be the visual selection.

Extra tip: if your prefix contains slashes, you can either escape them with backslash, or you can use a different punctuation character as the separator in the command. For example, to add C++ line comments, I usually write:

:s:^:// :

For adding a suffix the substitution approach is generally easier unless all of your lines are exactly the same length. Just use $ for the pattern instead of ^ and your string will be appended instead of pre-pended.

If you want to add a prefix and a suffix simultaneously, you can do something like this:

:s/.*/PREFIX & SUFFIX/

The .* matches the whole line. The & in the replacement puts the matched text (the whole line) back, but now it'll have your prefix and suffix added.

BTW: when commenting out code you'll probably want to uncomment it later. You can use visual-block (ctrl-V) to select the slashes and then hit d to delete them, or you can use a substitution (probably with a visual line selection, made with shift-V) to remove the leading slashes like this:

:s:// ::

:normal to the rescue!

:%norm Wibar(

:%norm WEa)

:norm(al) replays the commands as if you had typed them:

W - goes to the next word

i - starts insertion mode

bar( - types the sequence 'bar('

Or in one line:

:%norm Wibar(ctrlvESCEa)

If you're running Windows then type ctrlq instead of ctrlv.


Yet another possibility (probably not-so-useful in your test case, but handy in other situations) is to cordon off the area you want to change with marks.

  • Put the cursor anywhere in the top line and press 'a
  • Put the cursor anywhere in the last line and press 'b
  • Issue the command :'a,'b s/foo/bar(&)/

I usually like visual block mode if everything is visible on the screen, and I usually prefer marks if the start and stop are separated by many screens.


Another simple regular expression is:

%s/^/<text you want to prepend>/