How to clear the line number in Vim when copying? - linux

I copy some code from one part of one file to another part in vim, I find that, there are line numbers in each line and the format is gone, how to set correct format as origin ?
like this:
40 root /opt/release/current/public;
67 41 passenger_enabled on;
68 42

If you have line numbers, I'm quite sure you are not using Vim's yank/put operations (these will never copy the linenumbers, foldcolumn, icons etc) because in terms of the edit buffer, they don't exist.
My guess is you are working in a terminal emulator and using the mouse to copy stuff to the clipboard, which possibly selects the 'extraneous' room of the screen (including virtual spaces at the end, line numbers, fold markers etc)
You might have luck setting
:se mouse+=a
in order to get the behaviour of the mouse like you expect it. Otherwise, do the selection with V<movement>...y (y for yank, which corresponds to 'copy')
Then on the destination use p (put at cursor), or P (put before cursor)
Let me know if that helped or you need more info

In case anyone wants a quicker way (on Linux anyways), I have noticed in vim you can hold down ctrl and drag over the region you want to copy and you'll avoid the line numbers and select the part you want.
Steps:
ctrl and drag over area
release ctrl
copy (either keyboard shortcut or right click)

In normal mode, type :se nonu
This is the easiest way to remove the line numbers and you will be able to copy the text without the line numbers.

A permanent solution for this is to add the below code at the end of your .vimrc file located in your home directory.
se mouse+=a
By adding this you will be able to select only text and not the line numbers as shown in below image:
If you are not getting your .vimrc file in your home directory (i faced this problem), type the command :scriptnames in vi editor, it will display the location of your .vimrc file. Reference

On Mac: I found out that you can select the desired area with Option+Command and copy paste it to another editor.

All the previously entered solution are very good one. However, sometimes your are using vim on a remote server (so you cant copy to your clipboard using "+y). Often terminals support copy paste operation.
I use vim to output visual selection to a new shell where I can copy text using terminal feature:
:'<,'>w ! bash -c cat
Then I can easily copy the output.
Same pattern for pasting in vim:
:r ! bash -c cat
Then I paste and send EOF to cat using Ctrl+d. This method also avoid reindenting the text you paste (Note: you can disable automatic indentation using :set pi!).

Have a look at the pastetoggle option sometimes set to F11.
As an alternative you could always write the section you want to copy into a temporary file
(ma, goto end line then use :'a,.w tempfile) then read it into the second file.
For further investigation you might want to look at the autoindent option.

On Windows VIM GUI: :set nu and then hold down Ctrl-Shift while
highlighting the desired text with the mouse. This yanks the line numbers
into the buffer.

On Windows using Putty, I have noticed in vim you can hold down ALT and drag over the region you want to copy and you'll avoid the line numbers and select the part you want.
Steps:
Press and Hold ALT
Drag over area
Release ALT
Copy using CTRL+Shift+C (or right click if that is turned on)
Variations:
The specific key may be different based on your specific set up.
If ALT does not work, try the following keys instead:
CTRL
CTRL+Shift
ALT+Shift
Note:
If anyone knows of any other key combinations that have worked for them let me know and I will update this answer with them.

I mapped the below command to a key.
It strips the whitespace around the copied line numbers.
It ignores the line text and any blank lines behind.
:1,$s/^\s*[0-9]\+\s//\|1,$s/^\s*[0-9]\+\n/\r/<cr>

You can also consider using sed and pbcopy to copy the lines to a clipboard, where you can paste to another terminal or apps outside of vim.
sed -n <line start #>,<line end #>p <file name> | pbcopy

simplest way just: $ cat "your file"
and then just copy it from your terminal without numbers

Related

SLES12 vim - background changes scrolling + copy paste issues

I am using gnome-terminal with SLES12 and I encounter an issue where I am scrolling down during showing file in vim, the background color is changing.
Using some exploration during the internet I got the following solution :
if &term =~ '256color'
" Disable Background Color Erase (BCE) so that color schemes
" work properly when Vim is used inside tmux and GNU screen.
set t_ut=
endif
But using this solution, it creates a new one.
When I copy paste a line from vim and paste it on other vim , the copy consider also the blank lines in as characters , and creates really long lines.
To emphasize, lets say I have the following line which contain 11 char (including the space)
the copy paste consider also the rest of the line (the blank ones) as characters.
I would like that the copy paste would stop at char 'd '
hello world
Any idea how to combine a solution for these two issues?
The two issues are completely unrelated.
When you "copy paste a line from vim and paste it on other vim", you are presumably using your terminal emulator's or system's copy/paste feature which have no idea about where what you consider as a line starts and where it ends. The terminal emulator's window is n characters wide so a line is n characters and that's all they care about.
A much better approach would be to use Vim's built-in :help y and :help p, which have the same idea of what a line is as you.
But this creates a second problem: the default Vim is generally not built with clipboard support so you can't really use yy to yank a line in one Vim and p to put it in another one. You will have to install a proper Vim for that: use your package manager for that.
Once you are set, you can yank to clipboard with "+y and put from clipboard with "+p. See :help registers for "+ and :help 'clipboard' if you would like to synchronise Vim's default register with the system clipboard.
That said, why don't you simply open those two files in a single Vim instance?

How to select all text in vim normal mode?

Is there "keyboard-only" way to select an entire vim document in a way that is equivalent to a left-click and drag with a mouse in normal mode? Please do not confuse this with selecting all text in visual mode (ggVG). I want to be able to follow this up with a right-click paste into notepad++ (ggVG/ggVGy followed by a right-click paste in notepad++ does not copy the document). Thanks
Again, the "ggVG" commands are not working, nor are the "+y" commands (which I should have mentioned in my original post). Perhaps it is worth noting that I am working on a Windows local machine (where I have notepad++ open) and am generating the vim file on a linux virtual machine (slurm cluster). Under these working conditions, if I left-click drag over the vim doc and right-click paste in notepad++, the selected text copies over. However, the process is cumbersome for large files, hence my inquiry. Thanks again.
You should have mentioned that, of course, as it is not a meaningless detail at all.
Manual selection in a terminal can only select the text currently displayed in the viewport, which is obviously cumbersome for larger files. The only practical way to copy on the remote machine and paste on the local machine (and vice-versa) is to enable X-forwarding and build Vim on the remote machine against X libraries. This will give you what you want: a shared clipboard.
You won't be able to reach your goal in a practical way if you can't or don't want to install the necessary stuff on the remote machine.
As a lightweight alternative, you could simply scp the remote file to your local machine.
Just use (esc) :%y+. This will copy the entire document to your clipboard. Then you can go to notepad++, or whatever else you want to use, and paste it with a right click.
Explanation:
%: Tells vim the next command will be applied to all lines.
y: to all 'yank' lines
+: Copies all lines to clipboard, You can also use Ctrl + C instead. Note: + is sometimes bound as *. And sometimes both are equivalent.
Or you can also use the slightly longer way: ggVG+.
If you really want to be fancy you can remap Ctrl + A to ggVG or %y by adding this line to your .vimrc:
map <C-a> <esc>ggVG<CR>
Try to use Xshell remote login software. in there is a option called "To Text Editor".
Just open the file using "vi filename.c" it will displayed on the screen after that just made a left click on the work area and choose "To Text Editor--->all" . then these all text moved to a notepad file then u can simply copy and paste in notepad++.

How to copy all the text from vim editor using vim command line?

I want to select all the text from the vim editor, I tried the command :%y+ but getting error E850: Invalid register name. I get this command from this link. Please help me how to copy all the text from file which is open in vim. They are using yank, what is meaning of it..
I had a similar problem. Don't know why you got so many down votes.
The problem is that you haven't installed vim-gnome which takes about 24 MB and adds a feature to the inbuilt vim.
sudo apt-get install vim-gnome
then your command will work. :%y+ This command will copy all the text in system's clipboard.
TLDR: If you want to copy text in Vim to the system clipboard type ggVG"*y. Explanation below...
Vim runs in the terminal and, depending upon how you are using it and which type of Vim you are running, it's not really designed for you to select text with a mouse and copy and paste in the traditional way.
If you want to select all of the text using Vim then use ggVGy (note the uppercase VG in the middle). This command moves the cursor to the top of the file, enters visual mode, moves to the bottom of the file (thus, selecting all of the text) and then yanks (copies) it. You can then use p to put (paste) this code but only inside of Vim.
If you want to copy to the clipboard to use somewhere outside of Vim then try this:
First, select everything using the commands outlined above but without the final y: (ggVG). Then press "*y. This should now copy it to your operating system's clipboard and you can just paste (Ctrl/Cmd+v) anywhere you want outside of Vim. This can vary depending on what settings you have for Vim but it should work.
A brief explanation of the commands used. gg goes to the top of the file. V enters visual mode by lines. G goes to the end of the file. y yanks (copies) the text but not to the clipboard. p puts (pastes) the text.
The more advanced (i.e. cool) stuff:
" allows you to access registers. For example "a provides access to register a.
The * is the system clipboard so "* provides access to the system keyboard. Therefore, "*y yanks into the system clipboard.
While there's a great explanation of how to exploit the system clipboard in vim, it sounds like you're just having trouble getting your vim to access the clipboard in the first place. Try installing vim-gnome, it gives you the packages you need to get to the system clipboard.
For some reason, "* didn't work for me, but the exact same command with the "+ register did.
To select the whole file you can jump to the beginning, start visual mode, jump to the end:
ggVG
This question is a few years old now, but I had this same problem on Linux Mint 18. I found using xclip worked for me. You can map the command vmap <F7> :!xclip -sel c<CR><CR> in your .vimrc to have your current selection in visual mode copied to the system clipboard.
Here is a thread containing the above (and other) solutions.
You can use
Vggy/vggy or,
VGy/VGy
To visually select any number of text and then copy it, in your case it is gg / G as you want all text on the file,
gg is to copy while your cursor is at bottom of the file, gg for go to top
G is to copy while your cursor is at top of the file
Or even you can always use
Vk(as number of time)y to copy the selected lines of text.

How to copy from one split and paste in another in byobu?

So let's say I have two splits open in byobu, side by side. Furthermore, both splits have different files open in vim. I want to highlight text from one file in one split and copy it to a separate file in the other split. Any ideas?
All the results I found while searching for this talked about using the scrollback feature to copy and paste in byobu, however, that only seems to work inside a single split; not across splits.
Looks like I posted to quickly; seemed to have found the solution. I followed the steps found here:
http://linuxcommand.org/lc3_adv_termmux.php
I followed the steps:
shift-f3 - move to split to be copied from
alt-pgup - enter copy mode
space - start selection
cursor through desired text
enter - end selection
shift-f3 - shift focus to split to copy to
ensure receiving vim is in insert mode
alt-insert - paste selected text
If you are using an X Window Server, an alternative mouse-based solution to using the scrollback mode (which involves remembering a lot of keystrokes) is:
Zoom in on the current pane (Shift-F11), bringing this pane to the foreground.
You can now select the relevant text with your mouse without the vertical split getting in the way.
Unzoom the pane (Shift-F11 again)
Switch to other pane or wherever else you want to paste.
Middle click paste.
If your Vim supports the system clipboard (i.e. if vim --version output shows +clipboard), you can copy into the system clipboard from the first Vim and paste from it into the second one. This releaves us of the need to ensure the receiving Vim is in insert mode and has paste set appropriately.
The trick is to use the "+ register. So when you do the copy, prefix whatever yanking command you want to use with "+; and do likewise prefix the put command you use in the receiving Vim with it.
If you're on an X11 system, you can also use the "* register, which is X's "PRIMARY" selection buffer -- the one where text goes if you just highlight it, and which you can paste by pressing the middle button.
See :help gui-selections. GUI selection support generally requires a Vim other than "vim-tiny"; on Debian and Ubuntu the vim-gtk and vim-gnome packages are good choices.

How can I add non mouse-selectable line numbers to vim? [duplicate]

This question already has answers here:
How to clear the line number in Vim when copying?
(12 answers)
Closed 6 years ago.
So I've figured out how to add line numbers to vim (:set no or :set number) but how can I make it so that when I use my mouse in a terminal emulator to select a block of lines, it does not also select the numbers?
For example, say I have three lines that look like so in vim:
1 First line
2 Second
3 Third
If I want to select the three lines with the mouse what I want is for it to ONLY select the actual text. But what ends up happening is it selects the line numbers as well as all the space to the left and right of the line numbers.
Is there any way to change this behavior? BTW, I'm using the gnome terminal editor in gnome if that makes a difference.
Use the following:
:set mouse=a
to turn on xterm style mousing in all modes. This will allow you to do what you want. Keep in mind if the vim is remote via ssh, you'll need X11 forwarding turned on for the selection to make it to your local clipboard.
AFAIK, that is not possible.
The only thing I can add at the moment is that you'd be better off with
:set invnumber
It will inverse the current condition, so you can map it to a key, and toggle it. That way you don't have to remember two commands.
I agree with the first answer. If you use gvim, you can experiment with using set mouse=n and set mouse=a, which should change the line number selecting behavior.
When I want to select text into a terminal, I remove line numbers.
:set nonu
When I finished
:set nu
You're probably not on a Macintosh, and I can't tell if you mean you want to use system copy rather than vim's yank.
But if you are both those things, you can use option-drag to select text. This creates a 2d box over the text, selecting things that are under it. It should be functionally the same as 'take columns x1 to x2 of rows y1 to y2'.
I'm pretty sure this is not possible in terminal-vim. There is no accepted standard (e.g. a TTY escape) for indicating blocks of characters that aren't highlightable by mouse, as far as I know.
I would use gvim if you want to be able to do this at any cost. Its behavior is as you describe.
My recommendation is to get used to the little box on the right hand corner of the screen which has the current line's number and character position information. Before I used vim I could never imagine living without line numbers, but since then I've moved beyond this. Not having line numbers clutter up the screen allows for distraction free code viewing, and do you really need to know that the line number above your current active line is one less than your current line's number, and the line below is one more?
A copy without mouse:
Enter Visual mode: Position your cursor, press v (For complete line V) move up or down until desired position
Press control + c to copy into clipboard (if +clipboard exists in :ve command output) press ESC and paste it wherever you want with control + v.
Or press y to yank into register and press p to put the text after the cursor

Resources