How to delete selected text in the vi editor - linux

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

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.

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

Highlighting Text With Keyboard Between Limits On Sublime Text

In Sublime Text I can highlight text between specific locations by holding down shift and clicking the start of the region I want highlighted and then the end.
How do I do this with just the keyboard?
Method 1
Explore the commands in the Edit > Mark submenu.
Move your cursor to the start of the desired selection and run Set Mark (Ctrl+K, Ctrl+space).
Then move your cursor to the end of the desired selection and run Select to Mark (Ctrl+K, Ctrl+A).
Method 2
Or, as noted in the comments, hold down Shift as you move across the desired selection (using arrows, etc.).
Try the key command Ctrl+Shift+End on Windows when you are at the beginning or in the middle somewhere of a document. This works with any program.

Vim: How do I paste a column of text (from clipboard) after a different column of text?

I'm testing proxies with my script that looks like that:
$proxy = "http://name:pass#133.245.122.91:80";
$proxy2 = "http://name:pass#133.245.229.241:80";
$proxy3 = "http://name:pass#133.245.113.197:80";
...
$proxy100 = "http://name:pass#133.245.212.197:80";
I get new proxies by email so can I copy new proxies and insert it instead of the old ones by Vim:
"http://name:pass#133.245.122.91:80";
"http://name:pass#133.245.229.241:80";
"http://name:pass#133.245.113.197:80";
...
"http://name:pass#133.245.212.197:80";
Right know I'm doing it as was described on this page How do I paste a column of text after a different column of text in Vim?
Use visual block (ctrl-v) to cut the letter column. Then move to the
first line of the number column. Move to the end and make one space.
Then paste the letter column.
I'm curious, how it can be done without extra step, just paste data from clipboard?
The short version: you can't. There are ways around it, but they aren't necessarily simpler. Longer version follows.
Vim has three ways of marking regions of text: linewise (you start this mode when you press V), characterwise (triggered when you press v), and blockwise (when you press Ctrl-v). The marked region is copied to a register, and this register has an attribute, the "type", that reflects the way you did the marking, linewise, characterwise, or blockwise. What happens when you paste from a register depends on this type.
Now, when you copy from system's clipboard the result is stored in the * register, and the type is always set to linewise. Thus you can't paste a column mode "without extra step". You can however set the type of the * register to blockwise before pasting:
call setreg('*', #*, 'b')
Thus, replacing the list of your proxies would go something like this:
copy the new list to clipboard, from the mail message
run :call setreg('*', #*, 'b') to set the type of the * register to blockwise
go to the old list, press Ctrl-v and mark it; assuming there's nothing else in the file aside from the proxies, a Vim golfer's way of doing that might be something along the lines of:
f" - go to the first "
Ctrl-v - start marking
?;Enter - go to the last ;
paste the new list over the selection, with "*p.
You can simplify the last step a little, by making the * and + registers always refer to the same value. To do that, add this to your vimrc:
set clipboard=unnamedplus,autoselect,exclude:cons\\\\|linux
With this setting the incantation becomes:
copy the new list from mail
run :call setreg('+', #+, 'b')
go to the old list and mark it with Ctrl-v as above
press p to paste the new list over it.
You don't need this dance if you have the new list in a file that you can open with Vim:
open the file with the old list
open the file with the new list in a separate copy of Vim
mark the new proxies with Ctrl-v and yank them with y
in the other Vim mark the old list with Ctrl-v and paste the new one over it with p.
This still involves using the system clipboard under the hood, but the second copy of Vim takes care of setting the type of the relevant register to blockwise.
I don't know any direct way to do this. If it is really important to you, you will probably need some set up before you do the actual editing, which only adds to the amount of typing you have to do (however you can add commands to your vimrc to make it permanent). You might set up some keyboard macro, or use the following map command:
:imap <CR> <Esc>j011lC
Now move to the first " sign and press C, then start pasting (only works in a terminal). Whenever you paste a newline, the map will move you to column 11 in the next line.
Remember to :iunmap <CR> when you are done.

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.

Resources