Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 01-12-2007, 07:41 AM   #1
LQ Newbie
Registered: Dec 2005
Posts: 28

Rep: Reputation: 15
vi commands


I'm running ubuntu on my laptop. I'm using vi to edit large files. What command do I use in vi to cut blocks of the file and output them as a separate files?

Old 01-12-2007, 07:50 AM   #2
LQ Veteran
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2392Reputation: 2392Reputation: 2392Reputation: 2392Reputation: 2392Reputation: 2392Reputation: 2392Reputation: 2392Reputation: 2392Reputation: 2392Reputation: 2392

In command mode:

linenumber,linenumber w filename

I.e: 6,10 w outfile will save lines 6 to 10 to a file called outfile.

Hope this helps.
Old 01-12-2007, 07:52 AM   #3
LQ Addict
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
With vi running in two consoles, cut or copy from one and paste into the other.

highlight the text to cut/copy
ctrl+x to cut
ctrl+c to copy
ctrl+v to paste

You can also run vimtutor for a tutorial on using vim.
Old 01-12-2007, 08:03 AM   #4
Registered: Dec 2006
Location: Germany
Distribution: Kubuntu (Feisty Fawn), Debian (SID)
Posts: 127

Rep: Reputation: 15
If you demand vi feeling, I strongly recommend vim instead of plain vi.
It offers a visual mode that is quite powerful and works really intuitive!

Having done so, it goes like this:
* Press v to enter visual mode or V to enter visual line mode (you'll see the difference)
* make your selection using hjkl or cursor keys
* press x to cut your selection into the the "clipboard" (register in vi terminology) or y to copy it instead (This exits visual mode)

Here you are unclear in what you want to do: Do you want to write these blocks to newly-created files, or do you want to paste them into other files?

* You paste your copied or cut block of text with p

If you want a selected region to be saved in a seperate file, do this:
* With your selected region still active type : to enter command mode
* Type w new-file-name.txt
It will look like this:
:'<,'>w new-file-name.txt

This means, your currently selected region will be written into new-file-name.txt

~ fabian
Old 01-12-2007, 09:29 AM   #6
Registered: Dec 2005
Distribution: slackware
Posts: 78

Rep: Reputation: 15
why do people stil use vi when ther are other easy to use editors like nano , emacs etc? i find it really difficult to use .. is there anything special about vi ??
Old 01-12-2007, 12:46 PM   #7
Senior Member
Registered: Oct 2003
Location: New York City
Distribution: Debian Sid 2.6.32
Posts: 2,100

Rep: Reputation: 49
Vi/vim has the smallest memory footprint of editors, and it is present in just about every system.

Vim/emacs is an even older flamewar within the community than KDE/Gnome. Each side has its pros and cons.

Vim does take some getting used to, but it is super powerful. It is the only editor I have needed.

Old 01-12-2007, 03:51 PM   #8
Senior Member
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Debian, FreeBSD
Posts: 1,450

Rep: Reputation: 48
Originally Posted by cyberiapost
why do people stil use vi when ther are other easy to use editors like nano , emacs etc? i find it really difficult to use .. is there anything special about vi ??
vi is exceptionally easy to use -- once you have learned the basics. Once you know the basics you can easily do more and more advanced stuff as a simple logical extension of what you already know. People use it because it's extremely powerful, quick, and easy to use. That's especially true for structured files but holds true for almost everything I do.

As for the original question, he's got answers enough already.

Personally, I use m instead of visual mode (mostly because some of my systems don't have vim and probably never will).

ma2}:'a,.w newfile^M
It's not that hard. Actually, since it makes use of all the normal movement and marking commands you're using all the time... it takes minimally more time to do that than to do anything else.

(Note, the above starts at the current line, and writes two paragraphs to the newfile the ^M stands for the <ENTER> key)

As more detailed explanation.
m[ark point] a
2} [go down "2" paragraphs]
:'[from point]a,[to].[the current line]w[rite] newfile^M[filename and return]

Last edited by frob23; 01-12-2007 at 03:57 PM.
Old 01-15-2007, 10:34 AM   #9
LQ Newbie
Registered: Dec 2005
Posts: 28

Original Poster
Rep: Reputation: 15
vi command

Hey y'all,

Tanx. Your responses have been very helpful. I like vi because it pretty easy to use. There are probably countless better editors available, but vi suits me fine.

Tanx again guys. Mucho obliged.




commands, vi

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Commands joseph7 Fedora 2 09-01-2006 05:42 PM
Commands regarding OS harishkrishnan Linux - General 11 04-24-2006 02:35 AM
Commands captain chaos Slackware 3 04-05-2006 10:17 AM
Suse Linux Commands For These Aix Commands? Vaskar Guha Linux - Software 2 12-19-2005 12:45 AM
Need help with commands? bullwinkel Linux - Newbie 5 03-21-2002 07:15 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:50 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration