Vim: How to insert in visual block mode? - vim

How can you insert when you are in visual block mode (by pressing ctrl-V) in Vim?

Try this
After selecting a block of text, press Shift+i or capital I.
Lowercase i will not work.
Then type the things you want and finally to apply it to all lines, press Esc twice.
If this doesn't work...
Check if you have +visualextra enabled in your version of Vim.
You can do this by typing in :ver and scrolling through the list of features. (You might want to copy and paste it into a buffer and do incremental search because the format is odd.)
Enabling it is outside the scope of this question but I'm sure you can find it somewhere.

press ctrl and v // start select
press shift and i // then type in any text
press esc esc // press esc twice

You might also have a use case where you want to delete a block of text and replace it .
Like this
Hello World
Hello World
You can visual block select before "W" and hit Shift+i - Type "Cool" - Hit ESC and then delete "World" by visual block selection .
Alternatively, the cooler way to do it is to just visual block select "World" in both lines. Type c for change. Now you are in the insert mode. Insert the stuff you want and hit ESC. Both gets reflected with lesser keystrokes.
Hello Cool
Hello Cool

if you want to add new text before or after the selected colum:
press ctrl+v
select columns
press shift+i
write your text
press esc
press "jj"

Related

How to hold the highlighted section for sometime in VIM editor

I am right now analyzing some code using VI editor. In my use case, I have selected code spanning 2 Pages by using ESC SHIFT v & selecting all the lines (Spanning 2 Pages). Now the issue I have is I am not able to hold the highlight until I need. As soon as I press ESC and move the cursor the highlight goes off.
How do I hold the highlight until my need
If you just want to reselect whatever you previously selected when you leave visual mode you can use gv. You can't keep highlight when leaving a visual mode, though.
Edit:
If you just need to view selected text and you don't want to be distracted by surrounding text, you can simply copy it to an empty buffer. To do so select your text in visual mode, press y then :new then P. When you finish you can close newly created buffer with :bd!.

How do I paste text at multi-line selection in vi?

I know how to use this with manual typing:
Use Ctrl+V to enter visual block mode
Move Up/Downto select the columns of text in the lines you want to comment.
Then hit Shift+i and type the text you want to insert.
Then hit Esc, wait 1 second and the inserted text will appear on every line.
But i don't want to want type the text. I want just to paste it.. (because is a long string..)
Thanks, Mor.
Once you are in insert mode (after I), you can press <C-r>" to insert the content of the default register or <C-r>a for register a to z.
You can also use completion in that context: <C-n> for example.
If the text you want to use is in a register, use <c-r> (CtrlR). So, after you press I, instead of typing, press CtrlR, and the register name you want.
Since the OS clipboard is in the + register, you would do: <c-r>+ (CtrlR++).

How to use a copied text with the "I" and "A" command in a visual block?

I often insert text before or after a visual block using the "I" (insert before visual block) or "A" command (insert after visual block).
After typing the text and pushing the <ESC> key the text is inserted before or after every line in the block.
But what if I want to insert a piece of copied text?
This doesn't work: After pasting the text, the ESC key doesn't work anymore and the copied text is only inserted in 1 line.
How can I insert a copied text on every line before or after a block?
Do the selection and enter insert mode as before with I / A. Then, instead of typing, paste your register from insert mode with <C-R>". (" means the default register; you can use others (e.g. a) as well.) Conclude with <Esc> as before.
See :help i_CTRL-R for more information.

Copy lines in visual mode in vim

Just a question about copy/paste workflow in gVim.
Right now I'm working on a document. I want to select some lines of code and copy and paste. I'm using gVim (Windows).
If I use Control + C and Control + V, gVim takes sometimes 2, 3 seconds to paste...
The other way is using, Shift + v (right, now I'm in visual mode), then I keep the Shift key pressed and drag my selection with the mouse. Now I can copy with 'y' or 'c' and paste with 'p'. This is faster, but I have to keep the Shift key pressed.
Is there a way to use the mouse to select text in visual mode without keeping the Shift key pressed? I want to use y/d/p instead of slow Control + c and Control + v. Is there a better workflow or setup that I should try?
Thanks,
[]'s
Mateus
Stay away from using CTRL-C/V and your mouse in vim, or at least until you're familiar with vim's way of text handling.
--
SHIFT-V to enter visual line mode
Press 3j to go down 3 lines, or press j 3 times
y to yank/copy, x to cut, p to paste after cursor, P to paste before cursor.
Using hjkl will improve your workflow greatly as you don't have to move your hands from the typing position to the arrows and the mouse.
There are a couple of ways to yank multiple lines without entering visual mode. One of which is to type <action><number><direction>. For example, y3j means to yank from your current row to 3 rows down.
If you want to yank the entire paragraph or sentence you're in, type yip (yank IN paragraph) or yis (yank IN sentence) respectively. You can also do yi" (yank IN ") or ya" (yank AROUND ") to yank everything that's surrounded by " on your current position.

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