Deleting lines upward in vim Linux - linux

How to do that in vim, lets say im in line 100 and i want to delete 20 lines upward? how to do that in vim linux?

You can do it relative if you know the number of lines:
d20k
Or absolute if you know the line number:
d80G
d = delete, 20k = 20 lines up, 80G = goto line 80
Or without taking you current position into account:
:80,100d
Personally, I use visual mode a lot becuase it gives me a nice feedback:
hit V to enter visual line mode
move 20 lines up with 20k
adjust selection with k and j if necessary
press d to delete selection

In normal mode you can use d and the movenment key k to delete upwards. And then you prefix that command with the number of times you want to repeat the command like 100dk.
If you just want to delete to the beginning you can use gg along with d, gg sends the cursor to the first character in the file. So ggd will delete the first line to the line you are standing on.

You can do it upwards with the range.
:-20,.d
In this you can specify the range of line to delete it .
Will deletes 20 lines upwards to the current and this is kind of an ex-mode command.

Related

How to select from line to line in vi?

I frequently want to select multiple lines in vi. e.g. from line 1 to line 10.
So, what I usually do when I want to jump from line to line is I type :110 to jump to line 110, e.g.
And, when I want to select from line to line, I usually press v to get into visual mode, and then I just scroll down using k or l.
So, intuitively it makes sense to me to just press v, and then type :<line number>. but that doesn't work.
How to select from line X to line Y in vi?
Let's assume you want to highlight from line 10 to line 20. You can use:
10GV20G
Breakdown:
10 enters 10 into the buffer
G goes to the line number in the buffer
V enters visual line mode
20 enters 20 into the buffer
G goes to the line number in the buffer
Note that G means Shift+g (capital G).
Source and a : command are here.
Selection by itself isn't meaningful; you usually want to invoke a command on the selection. Many commands that work on the visual selection have a corresponding Ex command. With that, going through visual mode is unnecessary if you already know the exact ranges. The great benefit of visual mode is that you can interactively and iteratively adapt the selected area if there's no single motion or text object.
The benefit :help :range is that you can succinctly specify the lines. For example, lines 110 to 120 can be written as :110,120, but also as :110;+10.
simple just press Shift v line number gg
example: your current line to line 41
Just press Shift v 41 gg
note: you can move to selected line by press line number gg
If you set both number and relative number it becomes easy to see the target end line.
:set number relativenumber
So, let's say you are at the line 10 and the target line shows 11, you start your selection with capital V, then press 11j
V11j
If your block has blank lines before and after, just type vip (visuall inner paragraph)

VIM Delete From Cursor to Another Location in File

I'm trying to learn VI/VIM. I would like to know how to deleted the text from my cursor to some other spot in the file. I know how to delete a line (dd) and multiple lines (5dd) and to the end of a line (d$), but not, for example, from the cursor to the middle of the next line or the middle of the next two or three lines.
Thanks for any tips.
Cheers!
You can use any motion after a d. For example, to delete two words, you can do d2w. Or to delete 10 characters to the left, you can do d10h, or to delete next two lines, do d2j. For something more complicated like 'delete up to middle of next line', I usually just do v to go into selection mode, select what I need with hjkl, and hit d to delete it. If you do block selection mode Ctrl+v you can select a block that needs deletion and hit d. Hope that helps.
What do you mean "the middle"?
You delete with d{motion}, and that includes things like:
d5w - delete the next 5 words
d/test - delete up to the word test
see
:help d
:help motion
and the motion.txt linked in the help (also online http://vimdoc.sourceforge.net/htmldoc/motion.html )
I used to have vi macros to delete between marks. e.g. use ma to make mark a, and then put your cursor where you want it and:
mb'a"ad'b will make mark b and delete from mark a to b into buffer 'a'
mb'a"ay'b will copy (not delete)
"ap will get the text back.
(from memory this is whole line based, not "position on a line")

Difference between cutting lines with 10dd and d9 in vim?

If I understand correctly both commands cut 10 lines and allow you to paste them anywhere.
Are they both the same as (n-1)dd and dn+enter where n is the number of lines to be cut?
The two relevant help section are copied below.
d
["x]d{motion} Delete text that {motion} moves over [into register
x]. See below for exceptions.
dd
["x]dd Delete [count] lines [into register x] linewise.
10dd is the second one which deletes 10 lines from you current position.
d9 does nothing. d9j (or d9<CR>) is delete from the cursor to where the cursor ends up (which is9j) is nine lines below the current one. However the j or <CR> makes it linewise so the same thing is deleted.
Both of these commands delete 10 lines. so ndd is equivalent to d(n-1)j.
d9j might be easier to type than 10dd if you have set relativenumber turned on because the difference between the line you are on and the line you want to delete to are on the left hand side of your screen.
You can use d9k to delete 10 lines up from your cursor line which you can't do with dd. Or you can use dfa to delete upto and including the next a. d{motion} is more powerful than dd because it isn't restricted to only linewise deletions.
Which one you use is up to you but certain combinations are easier depending on where your cursor is.

Vim: faster way to select blocks of text in visual mode

I have been using vim for quite some time and am aware that selecting blocks of text in visual mode is as simple as SHIFT+V and moving the arrow key up or down line-by-line until I reach the end of the block of text that I want selected.
My question is - is there a faster way in visual mode to select a block of text for example by SHIFT+V followed by specifying the line number in which I want the selection to stop? (via :35 for example, where 35 is the line number I want to select up to - this obviously does not work so my question is to find how if something similar to this can be done...)
In addition to what others have said, you can also expand your selection using pattern searches.
For example, v/foo will select from your current position to the next instance of "foo." If you actually wanted to expand to the next instance of "foo," on line 35, for example, just press n to expand selection to the next instance, and so on.
update
I don't often do it, but I know that some people use marks extensively to make visual selections. For example, if I'm on line 5 and I want to select to line 35, I might press ma to place mark a on line 5, then :35 to move to line 35. Shift + v to enter linewise visual mode, and finally `a to select back to mark a.
G Goto line [count], default last line, on the first
non-blank character linewise. If 'startofline' not
set, keep the same column.
G is a one of jump-motions.
V35G achieves what you want
Vim is a language. To really understand Vim, you have to know the language. Many commands are verbs, and vim also has objects and prepositions.
V100G
V100gg
This means "select the current line up to and including line 100."
Text objects are where a lot of the power is at. They introduce more objects with prepositions.
Vap
This means "select around the current paragraph", that is select the current paragraph and the blank line following it.
V2ap
This means "select around the current paragraph and the next paragraph."
}V-2ap
This means "go to the end of the current paragraph and then visually select it and the preceding paragraph."
Understanding Vim as a language will help you to get the best mileage out of it.
After you have selecting down, then you can combine with other commands:
Vapd
With the above command, you can select around a paragraph and delete it. Change the d to a y to copy or to a c to change or to a p to paste over.
Once you get the hang of how all these commands work together, then you will eventually not need to visually select anything. Instead of visually selecting and then deleting a paragraph, you can just delete the paragraph with the dap command.
v35G will select everything from the cursor up to line 35.
v puts you in select mode, 35 specifies the line number that you want to G go to.
You could also use v} which will select everything up to the beginning of the next paragraph.
For selecting number of lines:
shift+v 9j - select 10 lines
simple just press Shift v line number gg
example: your current line to line 41
Just press Shift v 41 gg
Shift+V n j or Shift+V n k
This selects the current line and the next/previous n lines. I find it very useful.
You can press vi} to select the block surrounded with {} brackets where your cursor is currently located.
It doesn't really matter where you are inside that block (just make sure you are in the outermost one). Also you can change { to anything that has a pair like ) or ].
v%
will select the whole block.
Play with also:
v}, vp, vs, etc.
See help:
:help text-objects
which lists the different ways to select letters, words, sentences, paragraphs, blocks, and so on.
v 35 j
text added for 30 character minimum
Text objects: http://vim.wikia.com/wiki/Creating_new_text_objects
http://vimdoc.sourceforge.net/htmldoc/motion.html#text-objects
You can always just use antecedent numbers to repeat actions:
In visual mode, type 35&downarrow; and the cursor will move down 35 times, selecting the next 35 lines
In normal mode:
delete 35 lines 35dd
paste 35 times 35p
undo 35 changes 35u
etc.
} means move cursor to next paragraph. so, use v} to select entire paragraph.
It could come in handy to know:
In order to select the same ammount of lines for example use 1v
You should have done some modification to be able to use 1v, blockwise or linewise.
Today I saw this amazing tip from here:
:5mark < | 10mark > | normal gvV
:5mark < | 10mark > | normal gv
You can also reset the visual block boundaries doing so:
m< .......... sets the visual mode start point
m> .......... sets the visual mode end point
I use this with fold in indent mode :
v open Visual mode anywhere on the block
zaza toogle it twice
For selecting all in visual:
Type Esc to be sure yor are in normal mode
:0
type ENTER to go to the beginning of file
vG
Presss V to select the current line and enter the line number on keyboard and the press G.

Difference between :d[count] and d[count]

As a novice vim user, I used d[count]<Enter> to delete lines.
It striked me as odd that there were always count+1 lines deleted.
If I wanted to delete 2 lines, I typed d1, 3 lines took d2, ...
I finally took the time trying to understand why and it appears I should have been using :d<count>.
That does beg for the question though, why is :d1<Enter> <> d1<Enter>
d<count> in normal mode doesn't do anything, because the count isn't followed by a motion. So presumably you've been hitting d<count><Enter>, in which case the motion associated with d is <count><Enter>, which moves <count> lines downward. Since <Enter> is a linewise motion, the d will also be linewise, deleting all lines from the current one to the line <count> downward, inclusive.
The command you actually wanted is <count>dd.
d{motion} deletes the text that {motion} moves over. When you type 3<ENTER>, the cursor moves 3 lines below the current and therefore d3<ENTER> deletes that area.
:d[count] simply deletes [count] lines.
The difference is that {motion} is not the same as count.
To get around that, you could use the visual mode and select what you're going to delete and then simply press d.

Resources