How can I avoid copying line numbers to my clipboard from vim? - vim

I like having numbers and relative numbers set in my vim, but I want to be able to select text without grabbing the line numbers.
Were I to paste this text with ctrl-v it would contain the line numbers which is not what I want.
8 Plug 'michal-h21/vim-zettel'
7 Plug 'mattn/calendar-vim'
6 call plug#end()
5
4 let wiki_1 = {}
3 let wiki_1.path = '~/vimwiki/'
This question does not have a solution. I want to be able to select the text with my mouse without typing additional settings into the vim window before copying.
So, without changing settings mid-operation, is there a way to grab just the text and put it on my clipboard?

If you want to be able to do so with the mouse, you have to ask that to your terminal.
I use URxvt, for instance, and I can select a rectangle of text as I like by holding Alt while I click and then move the mouse, so I can just avoid selecting the numbers:
A better option is to become aware of the registers + (clipboard) and * (primary selection) and make use of them. (See :help quote+, :help registers and other pages liked from there.)
For instance, if you want to copy lines from 5 to 10 (the exact lines you have highlighted in your screenshot), you would move to line 5, e.g. via 5gg, and then hit "+y10gg to store them in the clipboard that you can paste in other programs often via Ctrl-V.

First problem: selecting text without line numbers
Your terminal emulator can't tell a line number from genuine buffer content so you can't rely on it to be smarter than you about what it copies. FWIW, you would have the same problem with vertical window separators, the fold column, etc.
If you absolutely need to select text with your mouse, then you should set the :help 'mouse' option in your vimrc. A good, general purpose, value would be a:
set mouse=a
This has the effect of disabling your terminal emulator's mouse handling to let Vim, which knows where line numbers are, control it.
Second problem: yanking the selected text to system clipboard
You can check if your Vim was built with clipboard support with:
:echo has('clipboard')
A 1 means that you have clipboard support and that you should be able to do "+y or "*y on the selected text. "+y yanks to the system clipboard (like Ctrl-c/Ctrl-v/etc.) and "*y is X11's primary selection (right click, etc.). If you feel those two extra keystrokes are too much, see :help 'clipboard'.
A 0 means that you don't have clipboard support and that you should get a different package. Which one and how depends on your operating system.

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 use different registers in Vim to copy/paste from/to OS clipboard -- how exactly?

I've read this How to make vim paste from (and copy to) system's clipboard? and I know what the hotkeys are. But I'm unable to execute any of those commands. For example, what exactly should I press to call
"* or "+? In which mode also? I've tried different things and none of them worked.
I am assuming you've double-checked that :echo has('clipboard') returns 1. If it returns 0, you're out of luck since vim isn't compiled with access to the system clipboard.
If you have clipboard powers, then yanking and pasting inside of vim is done with pressing just the letter y and the letter p, in normal mode. Start doing this first, to confirm that you can yank and paste inside of vim. For instance, yank a line: yy and paste it p.
Next, confirm that you can yank inside of vim to the system clipboard with "*y. (That means pressing the double-quote (shift-'), then an asterix (shift-8), then the letter y, all in quick-ish succession. Toggle over to another app and paste with the regular control-V. (You may be able to look at the bottom left of status line to see what you are literally typing, which might help).
If that works, then the clipboard functionality is "good to go." If it doesn't you might need to tweak your .vimrc to get things working. Try setting the clipboard to unnamed: set clipboard=unnamed and retest step 3 again.

Why it is so difficult for vim to copy into system clipboard?

According to official vim wiki copying any text into system clipboard is a something I've never expected to see in a text editor created by humans.
gg"+yG – copy the entire buffer into + (normal mode)
Copy/Pasting between the browser and a text file with 6 keystrokes is something I refuse to accept as normality.
Are there any sane alternatives?
It's not difficult, it's powerful. As the yank command takes a {motion}, you can copy arbitrary (and precisely selected) text areas, all with the same command. Likewise, "+ is just one destination of many, and Vim's named registers are very useful.
You may have noticed that most people (sometimes heavily) customize their Vim setup. On top of the powerful editing abstractions, that puts it on yet another level (at the expense of now being dependent on your Vim configuration).
So, if you need to copy the entire buffer to the system clipboard often, create your own shortcut, and persist it in your ~/.vimrc. For example:
:nnoremap <F2> :%yank +<CR>
There you have it: copying with a single keystroke (cp. :help key-notation for how keys are specified; as function keys are sparse, I would prefer <Leader>y instead).
If you often yank (various areas) to the system clipboard, making that the default register might also be worthwhile:
:set clipboard^=unnamedplus
To break it down:
gg
Move the cursor to the beginning of the buffer
"+
Set the target for copy to the system clipboard register
yG
Copy ("yank") everything from the cursor position to the end of the buffer (i.e. the whole file).
To give another example. If you want to copy just the current word to the clipboard you might perform:
"+yw
Since "w" is the command to move the cursor to the end of the current word.
If you want to shorten a regularly repeated action you could record a macro, or map a keyboard shortcut.
When I want to copy a whole file I just do :%y+
: Enter ex command
y[ank]
+ To clipboard register

Configure Macvim's text selection to not include character under cursor

Using macvim, when I copy a text selection, it always includes the character under the cursor.
For example, if the cursor is at the far left and I press shift-down arrow, it selects the entire line plus the first character of the next line (since the cursor is sitting over the next line's first character).
Is there a way to configure macvim to not include the cursor character in text selections?
Take a look at the selection option. By default it's set to inclusive, but you can change it to exclusive to make text selections act the way you want:
:set selection=exclusive
You can also set it to exclusive with the behave command:
:behave mswin
This also sets several other options, however, which may or may not be what you want. See the Vim help for the specifics.
:help :behave
:help 'selection'
I am guessing that shift-down arrow activates visual character mode, and moves the cursor down a line. If you are trying to select entire lines, you would be better off using visual line mode, which is activated from normal mode by pressing V (shift-v). This will select the current line in its entirety. You can then extend your selection to include the lines above and below using the k (or up arrow) and j (or down arrow) keys.
When using Vim, I think it is better to go with the grain rather than to fight against it. Don't expect it to work the same way as other text editors. Accept that the Vim way is different.

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