Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reusing the previous range in ex commands in VIM

Tags:

vim

Is it possible to reuse the range of ex commands in VIM?

As an example, I can write (copy) lines 4 to 10 from my current file to a new file using the following command:

:4,10w foo/bar.txt

But what I really want to do is move the lines to a new file. I can do this like so:

:4,10w foo/bar.txt
:4,10d

But it's a bit annoying to have to type 4,10 both times.

So I have two questions:

  1. Generally, Is there a way to reference the previously used range in ex commands?
  2. Specifically, if there is not a way to do (1), is there any easier way to cut and paste a number of lines from one file into a new one.
like image 437
latentflip Avatar asked Jun 13 '12 08:06

latentflip


People also ask

How do I repeat a previous command in vim?

The " @: " command repeats the last command-line change (a command invoked with " : ", for example :s/old/new/ ). You can move the cursor before using either of the repeat commands. Suppose you press dd to delete a line. Next, you might move the cursor, then press 5.

How do I view vim history?

To search the specific previous command in the command line prompt, use : and your search key. For example; type :p and then press the upper arrow key. It will search the command that starts with p and display those commands for you. You can scroll through the history by using up and down arrow keys.

How do I go back to where I were in vim?

Vim makes it easy to navigate previous locations: Ctrl + o navigate to the previous location in the jump list (think o as old) Ctrl + i navigate to the next location in the jump list ( i and o are usually next to each other) g; go to the previous change location.


1 Answers

I usually use cat to do this:

:4,10!cat > foo/bar.txt

This works because you're piping the lines through cat and replacing them with the resulting output, which is nothing. And of course you can append to the end of an existing file by doing >> instead of >.

like image 53
Ben Rady Avatar answered Oct 27 '22 06:10

Ben Rady