Copy lines in linux

Copy all the lines to clipboard

Is there any way to copy all lines from open file to clipboard in VI editor. I tried y G but it’s not using clipboard to store those lines. So is it possible?

If you’re on Windows and yank doesn’t work, see the 17th answer from the top: stackoverflow.com/questions/1620018/…

28 Answers 28

  • % to refer the next command to work on all the lines
  • y to yank those lines
  • + to copy to the system clipboard

NB: In Windows, + and * are equivalent see this answer.

This solution only works if vim was compiled with the clipboard option (vim —version will show +clipboard in that case). If it wasn’t, Brian’s solution (:%w !pbcopy) is a solution for Mac. Alternatives are available for other operating systems (e.g. xclip).

If anyone getting E850: Invalid register name try :%y»+ to copy all content to the clipboard and «+p to paste it

You should yank the text to the * or + registers:

  • gg to get the cursor to the first character of the file
  • «*y to start a yank command to the register * from the first line, until.
  • G to go the end of the file

Hello. its not working?clipboard data remains unaltered. when I type gg cursor goes to first line. but when I type «*yG ..cpu beeps at * ..so is it normal or some problem with my vim? I am on Ubuntu..

Do you have Deadkeys enabled? (e.g., when you hit ‘a you get á) If so, you need to press space after » .

Vim can only access the system clipboard if it is compiled with xterm_clipboard enabled. To find out whether you have this feature, run :version in vim, and look for the +xterm_clipboard flag. If it is preceded by a minus sign, then you won’t have this functionality. If this is the case, you might want to compile vim yourself, or run gvim which usually has this feature enabled.

The problem with this approach is that it moves the cursor and scrolls the window around. Twice. The other answer has far fewer keystrokes and drawbacks 🙂

  • copy selected part: visually select text(type v or V in normal mode) and type :w !pbcopy
  • copy the whole file :%w !pbcopy
  • past from the clipboard :r !pbpaste

Oh sorry, I meant over ssh where the client is a Mac and the server is Linux (which I assumed was what the OP was looking for but now that I think about it, that’s an unrelated problem). In my defense, I’d avoid vim for copying and pasting for local files, but that’s my personal taste. Good tips.

Another easy way to copy the entire file if you’re having problems using VI, is just by typing «cat filename». It will echo the file to screen and then you can just scroll up and down and copy/paste.

And if you want to do that without leaving vim, you can run the following command: :!cat % . And to copy the file’s contents programmatically to the clipboard on Mac OS: :!cat % | pbcopy

Needed to copy file from Ubuntu VM on Windows to Debian in GCP opened in browser window. :/ Variations of vim yanking did not work, this worked to paste to Windows then upload file to GCP.

Читайте также:  Linux password hash generator

The clipboard is buffer + . To copy to clipboard, do » + y and [movement].

So, g g » + y G will copy the whole file.

Similarly, to paste from clipboard, » + p

Just to add to this (4 years later. ), this has a slight drawback in that your cursor position will jump to the top of the file. To avoid this (e.g. if you are mapping this to some shortcut in your vimrc), you can do something like: mqgg»+yG`q

This is what I do to yank the whole file:

I’m afraid this does not answer the question, as you are yanking to the default register, not to the clipboard.

You can use «cat» command to open file and use mouse to copy

But that wont be the «vi» way, if file is huge then also it will be a problem. This might work but not a suggested way.

Fastest way for large files (under 10 thousand lines) in different remote servers if xterm_clipboard is unavailable

(in reply to @rshdev, and to avoid having to recompile vim with +xterm_clipboard per @nelstrom in comments on OP)

there’s a program called xclip that works like putclip on Ubuntu 11:

it’s not installed by default. to install, use:

sudo apt-get install xclip 

This works with vi as well, and not just vim. ESC :version On Linux you are probably not running «vi» even if that is what you typed.

To avoid having to type u after the command, just insert a w after the % like so :%w !xclip -sel clip

On Ubuntu 12

you might try to install the vim-gnome package:

sudo apt-get install vim-gnome 

I tried it, because vim —version told me that it would have the flag xterm_clipboard disabled (indicated by ), which is needed in order to use the clipboard functionality.

-> installing the vim-gnome package on Ubuntu 12 also installed a console based version of vim, that has this option enabled (indicated by a + before the xterm_clipboard flag)

On Arch Linux

you may install vim-clipboard for the same reason.

If you run neovim then you should install xclip (as explained by help clipboard-tool )

you can press gg to locate your curser to the start of the file,then press yG to copy all the content from the start to end(G located) to buffer.good luck!

Do copy the whole file inside the vim or its tabs

then move to a tab and paste by

and to cut the whole file use

If you’re using Vim in visual mode, the standard cut and paste keys also apply, at least with Windows.

  • CTRL A means «Mark the entire file.
  • CTRL C means «Copy the selection.
  • ESC means «De-select, so your next key press doesn’t replace the entire file 🙂

Under Ubuntu terminal (Gnome) at least, the standard copy also works ( CTRL SHIFT C , although there doesn’t appear to be a standard keyboard shortcut for select all (other than ALT E followed by A ).

@Mark, this behavior is from a standard Vim install so I assume that’s the default. All bets are off if the environment is configured differently, though if that were the case, I suspect the OP would know what they’re doing 🙂 In either case, OP stated in a comment they were on Ubuntu so the Windows part of my answer probably doesn’t apply. The Gnome terminal stuff would.

:%y a Yanks all the content into vim’s buffer, Pressing p in command mode will paste the yanked content after the line that your cursor is currently standing at.

Читайте также:  Удалить права acl linux

gVim:

'a' Autoselect: If present, then whenever VISUAL mode is started, or the Visual area extended, Vim tries to become the owner of the windowing system's global selection. This means that the Visually highlighted text is available for pasting into other applications as well as into Vim itself. When the Visual mode ends, possibly due to an operation on the text, or when an application wants to paste the selection, the highlighted text is automatically yanked into the "* selection register. Thus the selection is still available for pasting into other applications after the VISUAL mode has ended. If not present, then Vim won't become the owner of the windowing system's global selection unless explicitly told to by a yank or delete operation for the "* register. The same applies to the modeless selection. 

I tried a few of the commands that people have mentioned above. None worked. Then I got hold of the simplest of them all.

Step 1: vi
Step 2: Right click on the title bar of the Putty window
Step 3: Select «Clear scrollback» (to avoid copying the rest of your SSH session)
Step 4: Right click again and select «Copy all to clipboard».

For 15000 line file, this didn’t grab it all. I needed to increase my Putty’s Window > Settings > lines of scrollback to 40000 (also available by right-clicking the title bar). It also helps to use cat instead of vi since that scrolls through all the text.

I ended up always losing lines from the top and bottom of the file this way. What I do now is cat, then middle-click at the start and end of the file to extend selection.

When I tried this I only got the 111 lines on the screen but there are 174 lines in my file, so Noumenon’s cat answer worked for me

Источник

Vim/Vi Copy and Paste Line

Vim/Vi Copy Line

Vim or Vi provides different keys and commands in order to copy the line. In this tutorial, we will learn how to copy lines in different ways. the yank or copy command can be used to copy the current line, multiple lines, some part of the line to the begging or end of the line. Also, we can paste this copied text using the paste command.

Copy (Yank) Current Line

The yy keys are used to copy the current line. The current line means where the cursor is located currently. Follow these steps to copy the current line.

  • Press the ESC key in order to change normal mode.
  • Press yy keys in order to copy the current line where the cursor is located.

Copy 3 Lines From The Cursor

We can copy a specified number of lines by using the yank (yy) command. The copy starts from the cursor positions and goes up to a specified number of lines. In the following example, we copy 3 lines by starting from the cursor position. First press ESC and then type the following yank command.

Copy All Lines From Cursor To The End Of Line

We can copy everything from the cursor positions to the end of the line with the following yank command. First press ESC and then type the following yank command.

Copy All Lines From Cursor To The Start Of Line

Another useful copy case is copying everything from the cursor position to the start of the current line. First press ESC and then type the following yank command.

Читайте также:  Lenovo g580 with linux

Copy First Line Of The File

We can copy the first line of the file. Even when the file is opened for the first time the cursor will be located at the start of the first line. But during usage, we will move the cursor into different lines. We may want to copy the first line from the locations and lines in a file.

  • Press the ESC key in order to change normal mode.
  • First press the gg keys to move the first line and then press yy keys in order to copy the current line where the cursor is located.

Copy Last Line Of The File

We can copy the last line of the file. Even when the file is opened for the first time the cursor will be located at the start of the first line. But during usage, we will move the cursor into different lines. We may want to copy the last line from the locations and lines in a file.

  • Press the ESC key in order to change normal mode.
  • First press the G key to move the last line and then press yy keys in order to copy the current line where the cursor is located.

Paste Copied Line

Lines are generally copied in order to paste another location So after the copy operation, we can paste the line to the cursor’s current location.

  • Press the ESC key in order to change normal mode.
  • First press the p key in order to paste the copied line into the current location where the cursor is located.

Get Help About Copy (yy)

The copy keys “yy” are very popular and help about them can be displayed with the following :help yy command in Vim/Vi. First press “ESC” and then run the following command.

The help screen can be closed with the ESC and then :q command.

Источник

how to copy lines 10 to 15 of a file into another file, in unix?

I want to copy lines 10 to 15 of a file into another file in Unix. I am having files file1.txt and file2.txt . I want to copy lines 10 to 15 from file1.txt to file2.txt .

2 Answers 2

Open a terminal with a shell then

sed -n '10,15p' file1.txt > file2.txt 

If you want to append to the end instead of wiping file2.txt , use >> for redirection.

sed -n '10,15p' file1.txt >> file2.txt ^^ 

AWK is also a powerful command line text manipulator:

@JigarGandhi «Insert at start» is really another topic. Your best bet is to save the output to file3.txt and then append the original file ( cat file2.txt >> file3.txt ).

If I want to pass the line number 10 & 15 of sed using two variables say start_line & end_line then what to do?

@SoumyadipDas Just use shell variable processing: sed -n «$,$p» . Make sure you avoid single quotes where no variable substitution happens.

Nice, it’s the easiest one. I did something with the help of askubuntu.com/a/76842 and working, but I will now use your’s solution in my shell.

In complement to the previous answer, you can use one of the following 3 solutions.

Print only the lines in the range and redirect it to the output file

sed -n '10,15p' file1.txt > file2.txt 

head/tail combination

Use head and tail to cut the file and to get only the range you need before redirecting the output to a file

head -n 15 file1.txt | tail -n 6 > file2.txt 

Print only the lines in the range and redirect it to the output file

Источник

Оцените статью
Adblock
detector