mouse select copy from "vim" and "less" show different results - linux

Case1: Open file1 in vi. Select a few lines(select copy is enabled). Paste in a different place.
Case 2: run the command less file1. From the console, select some lines. Paste in a different place.
In case2, I see that there are new lines introduced at where the line display shifts to new line. So, if the terminal width is 80 characters and my line is 100 characters, then 20 characters will be shown in the new line. If I copy from vim, all 100 characters are copied without any line-break. However, if I copy from "less" command, line-break is introduced after 80th character.
This messes up things like path.
Does "less" introduce line-break dynamically for lines longer than the display width?

less is not designed to handle mouse events. So when you select text while running it, the selection will be handled by the terminal behind, which doesn't give any sense to lines, paragraphs and so on; the text buffer is copied as it is displayed, that's all.
On the opposite, if you use vim with the right configuration, mouse events will be detected and treated by vim itself : the terminal will gracefully let vim handle them, for convenience. Then the line layout will be restored correctly when copying lines of text.

Related

Commenting several lines in vim not working

All the instructions I see say pretty much the same thing here
What's a quick way to comment/uncomment lines in Vim?
First, go to the first line you want to comment, press CtrlV. This will put the editor in the VISUAL BLOCK mode.
Then using the arrow key and select until the last line
Now press ShiftI, which will put the editor in INSERT mode and then press #. This will add a hash to the first line.
Then press Esc (give it a second), and it will insert a # character on all other selected lines.
When I pretty shift+I, the multiple highlighting of lines disappears, and all that happens is that it goes into regular insert mode at the first line only. When I type something, it's only typed on the first line.
I feel that there is a step missing, but I can't figure out what.

Vim real tab characters start at column 8- I can't move all the way left

I'm using real tab characters in my files, and when whitespace characters are set to invisible (which it is by default), then the farthest left I can go is column 8. But if I make whitespace visible (:set list!) then it still says that I'm at column 8, but it at least shows my cursor all the way to the left. What could be causing this?
This is not a matter of correct or incorrect. Vim just chooses to put the (single cell) cursor on the last cell of the Tab, and the jumping forward movement probably makes Tabs easier to detect, so most people like that behavior. Only with :set list, where a Tab is represented by a start and follow-up characters does this change.
If you really can't get used to it, the only workaround (short of modifying Vim's source code directly) is:
set list listchars=tab:\ \ " Note: trailing space after the last backslash!
Note that this has other side effects, e.g. when soft wrapping words.
It's not a bug or a problem, other than a visual annoyance. In the upper screenshot your cursor is at first character of the line. Try a character modifying command like rx on it, you'll see.

vim: Change the behavior of visual block mode to force highlighting of columns

I am new to vim and this was difficult for me to google because I am not sure how to articulate what I want to do.
Using this screenshot as a reference:
I want to highlight the following block of text:
Is there a way to force vim to highlight an arbitrary block of text like this?
I can highlight text in the square from line 8 to line 11, but when I move down to the closing bracket it just highlights a single column.
From here:
If I move down one row it only selects the text in the first screenshot.
You can use -- VISUAL BLOCK -- mode (by default you enter this mode with Ctrl+v). Start on the column you want to start on. Enter the mode and move to end of the longest line (with $ if you wish). This will highlight the entire line for other blocks as you continue to move up.

Vim select the ends of multiple lines (block mode, but where the ending column varies)

Is there any way in vim that I can select the end of all these lines? (I'm only showing the end of the lines in these screenshots).
In block mode I can get them all if the bottom line is longer than the rest, but if the bottom line is shorter, the longer lines are truncated.
EDIT | I guess I can just pad out the bottom line with spaces before I select, then delete the spaces later.
Put your cursor on the top-left character you want to be part of the block.
Enter block selection mode with ctrl+v
Select to the end of the line with $ (this is the step you're missing; if you move to the end of the first line using $ then the selection will extend to the end of subsequent lines as well)
Move down 3 lines with 3j
There's more information in the Vim documentation's section on visual mode which you can read online, or just type :help v_$ in Vim.
Click somewhere (anywhere) in the first line you wish to append text to.
Press Control + V.
Press Down to create an arbitrary vertical block selection that spans the desired lines.
Press $ to expand the visual block selection to the ends of every line selected.
Press Shift + A to append text to every selected line.
Type the text you want to append.
Press Escape and the text will be appended across the selected lines.
Alternately, you can set the virtualedit (:h 'virtualedit') setting so that, any time you're in visual block mode, you can move the cursor around even past the ends of lines. E.g. :set virtualedit=block.
If you're looking to select the very last character of every line, like if you want to add something after the quotes at the end of each line, you can do the following:
Put your cursor over the very last character (in this example, the last quote on the first line)
Enter block mode: control + V
Move down to select as many lines as you want to change.
Insert at the end of the line: shift + A
Type what you want to add and then exit Visual mode
You text should now be inserted at the end of each selected line!
Hope this is helpful to others like me searching for an answer similar, but not exactly the same, as the above.
I don't know if is a new thing. But if you press $ two times (instead one) the block goes to the end of all lines without creating extra spaces).
Tested on nvim 0.7.2.

Paste multiple times

What is the best way replace multiple lines with the contents of the clipboard?
The problem I'm having is when I yank a line and paste it over another line the "yank" is replaced with the line I just replace. Now, if I want to replace another line with the same line I have to go back up and yank it again.
There's got to be a better way to do this.
I have this in my .vimrc:
xnoremap p pgvy
(note: this will work only with the default register, but this mapping is easy to remember). Writing a more elaborate version would be possible. Also, you still can use P to get the old behaviour.
"0 should have the contents of your yank. It's a bit more tedious to type, but "0p should do what you want.
Alternatively, don't select-and-replace the old lines up front. If you find those lines with a search, just hit n. over and over (after an initial p), then when they're all pasted, do ndd followed by as many n.s as necessary.
The biggest mental switch I've needed to make when moving to Vim is to figure out how to apply group edits sequentially. I.e. rather than doing a bunch of edits on a line and then doing a bunch of the same edits on another line, I'll do the first edit on a bunch of lines (using . to great effect), then the second edit on a bunch of lines, etc. Alternatively, the use of macros may help as they are fantastic, but sometimes a little more tedious to get working correctly with "complex" changes.
I often use another registry, copy the line you need to some named registry "ay and then paste from there "ap
When you paste over a selection in Vim it will replace the default register with the contents of the selection. If pasting over a selection is wiping out the contents of the clipboard register then very likely you have the following line in your .vimrc
set clipboard=unnamed
One option is to remove that and use the explicit clipboard register "+
Another option is to use any of the other explicitly named registers (a-z). After the first paste yank the line back into "c for example and then use "cp to paste from there on out.
Instead of using copy/paste, it is often better to use a text object command such as ciw to change the inner word. This method has the advantage of being easily repeatable using the . repeat command.
yiw Yank inner word (copy word under cursor, say "first").
... Move the cursor to another word (say "second").
ciw<C-r>0 Change "second", replacing it with "first" ( is Ctrl-R).
... Move the cursor to another word (say "third").
. Change "third", replacing it with "first".
use np where n is the number of how much time you want to paste the lines eg 3p will paste 3 lines.

Resources