Text Editing in VI - vim

How do I delete text probably multiple lines in vi(m)?
Tried the backspace button to delete. Works not!
Deleted lines of text.

By default, in Vi/Vim, backspace does not work as we are used to] (cf :help bs, in Vim).
If you are in Vim, you can set it to behave the way you want with:
:set bs=indent,eol,start
If you are in Vi only, I refer you to #user31264's answer.

Some most common delete commands:
Delete 123 lines from the current one: d123d or 123dd
Delete one line: dd
Delete 1 character: x
Delete 123 characters: 123x
Delete till the end of the current word (word is a sequence of alphanumeric characters, or one non-alphanumeric non-space character): dw or de
As above, word is a sequence of any characters except whitespace: dW or dE
As above, delete 123 words: 123dw, 123de, 123dW, or 123dE

Related

delete first 2 white spaces from start of line in gvim

I am reading a file which has 2 unwanted spaces in each line.
import uvm_pkg::*;
`include "uvm_pkg.svh"
And it continues till last line
How to delete first 2 white spaces in each line in gvim?
Enter :%s/^ //, which substituttes 2 leading whitespaces (^ ) with nothing (the nothing between the second and third /) on every line (%).
position cursor at line 1, column 1 (press gg0)
enter visual block mode (Ctrl+v)
select first two columns of all lines by moving the cursor to the bottom and one to the right (Gl)
delete selection (x)
:%s/^ //
substitutes the two first leading spaces on every line of the buffer with nothing, effectively deleting them.
Note that, in this case, you don't even need the replacement part because it is implied: :%s/^ .
See :help :s and :help :range for the %.

How to dd entire line and paste insert to other line without newline and space?

for example:
1: if doc.text =~ /test/
2: content = doc.xpath('shoud be replaced') // this string in quotes will be replaced with `vi'p`
3: end
4: /html/body/table[3]/tbody/tr/td[5] // dd this line
Step: dd line 4 and jump to line 2 then vi'p
if i dd the line 4 and paste to replace the string in the quotes with line 2, the line will contain linebreak and spaces, like following:
1: if doc.text =~ /test/
2: content = doc.xpath('
3: /html/body/table[3]/tbody/tr/td[5]
')
4: end
how to avoid the linebreak and spaces?
Before you put, rewrite the contents of the default register to trim whitespace, which you can do with
:let ## = trim(##)
By removing leading and trailing whitespace, including the trailing newline, the register will become character-wise (rather than linewise), so Vim won't break the line before the register's contents while putting it.
Alternatively, if you can control the time when you cut the contents, then use D to delete to newline, after going to the first non-blank character with ^. A character-wise delete will end up in register "-, which won't be overwritten by a subsequent line delete (dd to delete the remaining whitespace in that last line), so later on you can put it with "-p.
Variant 1:
4Gy$dd2Gvi'"0p
4G - go to the 4th line, 1st non-blank char (also you can use _ or ^)
y$ - yank until (but not including) the end of line
...
"0p put last yanked text
Variant 2:
" hotkey to trim register
nnoremap <leader>t :call setreg(v:register, trim(getreg()))<CR>
4Gdd<leader>t2Gvi'p
I needed to "cast" register contents into a certain (characterwise / linewise / blockwise) mode so often, I wrote the UnconditionalPaste plugin for it. It provides gcp, glp, etc. alternatives to the built-in paste commands that force a certain mode (and by now several more variations on this theme, like pasting with joined by commas or queried characters).
With it, you can just use gcp / gcP to paste after / before the cursor position, regardless of how you've yanked the text. Inner newline characters and indent are flattened to a single space, leading and trailing removed.

Delete word group in Vim

I am in vim editing a python file, how can you delete the sequence throw=it,? After searching online I see the command daw, but that doesn't work with this word group.
one two three throw=it, now
another way is daW. with a capital W, it will delete any sequence of non-space characters, regardless of where inside the sequence you are.
This is different from dE, because dE only deletes from the cursor until the next end of the sequence of non-space characters, whereas daW will also delete the whole sequence between whitespaces.
assuming cursor is at the start of throw=it, dE should do the trick
E Forward to the end of WORD [count] |inclusive|.
Does not stop in an empty line.
Another way is Bdt<space>. Note your cursor can be on any character on throw=it,
B: puts the cursor at the very front of the block after the preceding space
dt<space>: delete till space
Use caW to delete your sequence and enter insert mode.
Use daW to delete your sequence and stay in normal mode.

How do I change autoformat in vim?

I use autoformat (textwidth = 72) to write my emails in vim.
The problem is that every time I write a list or short phrases, vim joins it to the line above.
p.e.
These are my options:
- option nr. 1
When I write "-", the "-" is immediately joined with the phrase above:
These are my options: -
Same when I use other kind of lists p.e. numbered lists or other symbols before the list.
Same thing when I write phrases shorter then 72 characters p.e.
This is my text.
This is my text on the 2nd line.
Autoformat changes it to:
This is my text. This is my text on the 2nd line.
How can I change this behavior?
I only want to format long lines when there are no Carriage Return <CR> in the first 72 characters.
If there is a <CR> it has to break there.
:help autoformat gives some useful hints:
You need to properly define paragraphs. The simplest is paragraphs that are
separated by a blank line. When there is no separating blank line, consider
using the 'w' flag and adding a space at the end of each line in the
paragraphs except the last one.
So, either :setlocal fo+=w, or turn off autoformat (maybe only temporarily, with a quick toggle mapping).

How to reformat "gq"ed text in one line per paragraph format in Vim

I have some text files previously formatted in vim using "gggqG". Now I need to convert them to one line per paragraph format. I saw a way(using :g command) to do that before, but I have forgot it. Anyone knows?
There are two approaches I know of:
Set textwidth to something big and reformat:
:set tw=1000000
gggqG
Use substitute (this is more appropriate if you want to do it in a mapping):
:%s/.\zs\n\ze./ /
Explanation of the latter:
:%s " Search and replace across the whole file
/ " Delimiter
.\zs\n\ze. " Look for a character either side of a new-line (so ignore blank lines).
" The \zs and \ze make the replacement only replace the new-line character.
/ / " Delimiters and replace the new-line with a space.
If your text paragraphs are separated by a blank line, this seems to work:
:g!/^\s*$/normal vipJ
:g global (multi-repeat)
!/^\s*$/ match all lines except blank lines and those containing only whitespace.
normal enters 'normal' mode
vip visually select inner paragraph
J join lines
Maybe you should set textwidth to very large value (like 99999999) (0 does not work for some reason) and use gggqG?
// I cannot tell you a way to reformat your paragraph with :g without knowing exactly what the paragraph is. Maybe somebody else can.

Resources