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

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.

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.

In Vim, how do I make visual line selection for a block of code?

For example, in HTML I want to select an entire element. I could do Shift+V, and keep hitting J until I've selected all the lines, but that's cumbersome. Typing vat doesn't do what I want either -- it goes into visual mode, not visual line mode. The problem with not having visual line is that when I yank the text, its not yanking the first line's indentation.
You just do vat as usual, then press shift-v to have linewise selection.
First, you need to install matchit plugin, which provides extended functionality for %. Particularly it allows to jump between matching HTML tags using %.
After that, put cursor on opening HTML tag (on first letter of the tag, not < symbol) and press Shift + v, then %.
This will do line selection from the line you put cursor to the line containing the closing tag.

How to remove text from before a cursor to start of line in visual mode in vim?

I've recently begun challenging myself in VIM and slow to get around still. Suppose I have the following:
1 ewdawdawdeditor you can scroll the
1 page, move the cursor, delete lines, insert
2 characters, and more, while seeing the
3 results of your edits as you make them.
My cursor is on 1,1 and after vfe its on 1,10.
How can I delete the text before the cursor to the beginning of the line without pressing hd?
If your goal is to simply select from the beginning of the line up to but not including the first "e", you can use vte instead of vfe. Then you can press d to delete.
Alternatively, you can delete without entering visual mode with dte.
To the extent of my knowledge, I don't think there is a way to delete everything in a visual mode selection excluding the last character.
If the cursor is somewhere at the middle of a line and you want to delete from the the beginning until the cursor just go with :
d^
If you really want to go visual before deletion, you can use :
v^d

Paste to end of the file in VIM without moving cursor

In a text document, I'm [visually or otherwise] selecting several lines, cutting them with d... I'd like to paste these lines to the end of the file without moving the cursor. Is there a relatively simple way to do this?
Use the Implicit Mark from Last Jump
You can use the implicit mark (e.g. ') to return your cursor to the location it occupied just before the last jump. For example:
Gp''
This will (G)o to the end of the file, (p)aste the contents after the last line, and then return to your position at the time you typed G.
There are a few ways:
Marks
Set a mark, do your paste, then jump back to the mark
m':$pu<cr>``
Visual mode
Visually select your lines, copy them, append, and then restore visual selection (optionally delete)
y:$pu<cr>gv
Append to the file
Visually select your lines, use :w to append to the file, and then reload the file. (Note: will move the cursor to the start of the visually selected lines)
:w >><cr>:e!
Create your own command/mapping
You can create your own command and/or mapping that will use winsaveview() and winrestview() to append then restore the cursor.
You can define a mapping which marks the current location, pastes at the end of the buffer using :$put then returns to the original cursor location using the mark.
This works because :put allows a line number prefix (the last line being representable as $). From :help put:
:[line]pu[t] [x] Put the text [from register x]
This would map it to <leader>p:
:nnoremap <leader>p :mark '<cr>:$put<cr>`'
It sets the ' mark at the cursor, pastes at the end, then returns to the ' mark with `
Depends on how you mean "without moving the cursor".
This will paste at the bottom of the current file and then allow you to continue where you cut the lines from.
split window with :split
move to bottom with shift+g
paste with p
close the duplicate split view (zz or :q)
If you dont like the split view, you can use the ctrl+o to jump back after G
move to bottom with shift+g
paste with p
jump back with ctrl+o

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.

Resources