Commenting several lines in vim not working - vim

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.

Related

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

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.

How to delete selected text in the vi editor

I am using PuTTY and the vi editor. If I select five lines using my mouse and I want to delete those lines, how can I do that?
Also, how can I select the lines using my keyboard as I can in Windows where I press Shift and move the arrows to select the text? How can I do that in vi?
I am using PuTTY and the vi editor. If I select five lines using my mouse and I want to delete those lines, how can I do that?
Forget the mouse. To remove 5 lines, either:
Go to the first line and type d5d (dd deletes one line, d5d deletes 5 lines) ~or~
Type Shift-v to enter linewise selection mode, then move the cursor down using j (yes, use h, j, k and l to move left, down, up, right respectively, that's much more efficient than using the arrows) and type d to delete the selection.
Also, how can I select the lines using my keyboard as I can in Windows where I press Shift and move the arrows to select the text? How can I do that in vi?
As I said, either use Shift-v to enter linewise selection mode or v to enter characterwise selection mode or Ctrl-v to enter blockwise selection mode. Then move with h, j, k and l.
I suggest spending some time with the Vim Tutor (run vimtutor) to get more familiar with Vim in a very didactic way.
See also
This answer to What is your most productive shortcut with Vim? (one of my favorite answers on SO).
Efficient Editing With vim
Do it the vi way.
To delete 5 lines press: 5dd ( 5 delete )
To select ( actually copy them to the clipboard ) you type: 10yy
It is a bit hard to grasp, but very handy to learn when using those remote terminals
Be aware of the learning curves for some editors:
(source: calver at unix.rulez.org)
If you want to delete using line numbers you can use:
:startingline, last line d
Example:
:7,20 d
This example will delete line 7 to 20.
Highlighting with your mouse only highlights characters on the terminal. VI doesn't really get this information, so you have to highlight differently.
Press 'v' to enter a select mode, and use arrow keys to move that around. To delete, press x.
To select lines at a time, press shift+v.
To select blocks, try ctrl+v. That's good for, say, inserting lots of comment lines in front of your code :).
I'm OK with VI, but it took me a while to improve. My work mates recommended me this cheat sheet. I keep a printout on the wall for those odd moments when I forget something.
Happy hacking!
When using a terminal like PuTTY, usually mouse clicks and selections are not transmitted to the remote system. So, vi has no idea that you just selected some text. (There are exceptions to this, but in general mouse actions aren't transmitted.)
To delete multiple lines in vi, use something like 5dd to delete 5 lines.
If you're not using Vim, I would strongly recommend doing so. You can use visual selection, where you press V to start a visual block, move the cursor to the other end, and press d to delete (or any other editing command, such as y to copy).
If you want to remove all lines in a file from your current line number, use dG, it will delete all lines (shift g) mean end of file

Resources