How to add characters to non-blank lines using vim - vim

I have a text document that has blank lines and lines that start with capitalized months.
I want to add "- " to the beginning of each non-blank line.
Tried this:
1,$s/^[A-Z]/- /
and it removes the first letter of the month (e.g. "- une" when it should be "- June")
How can I add that letter back? Or is there a "better" way of doing this for a large document where I need to keep the blank lines untouched.

try this, it should give what you want
%s/^[A-Z]/- &/
or use :g:
:g/^[A-Z]/s/^/- /

You have an answer that would either add the matched text back in, or avoid matching the text altogether during the substitution. Another method is to specify the match as zero-width, or set the ending of the match, so that the text is matched but not replaced:
%s/^[A-Z]\#=/- /
or
%s/^\ze[A-Z]/- /
See :help zero-width, :help /\#=, :help /\ze.

Related

Notepad ++ add new paragraph after specific symbol

I have a text that it is in one row. My purpose is to bring every new statement to a new row. After every "." symbol I want next statement to be on new row. How to achieve that. I have already tried to replace .\s* with .\n but that delete rest of text and just replace it with a "."
Use the Notepad++'s Regular Expression search feature with ". matches newline" checked, Now type \.. in Find what and .\nText Text Text\n in Relace with and hit Replace all.
Updated
For every character including "-".
Find what: \.([^.]+),
Replace with: .\n$1
Use replace tool in Extended mode (not the regexp) and put "." to replace with "\n". Or escape the point which is a special character in regexp mode:

How to complete the whole list with Vim?

I have a list of products to place on a rails seed and I would like to instead of put brackets one by one on the list with a command place the brackets on the whole list?
for example:
1. Dakine
2. Dale of Norway
3. Dan Post
1. ["Dakine"],
2. ["Dale of Norway"],
3. ["Dan Post"],
I searched on the help but did not find any about. Thanks.
You can record a macro in Vim and repeat that.
If you are on number 1, you can do following:
qqf a["Esc$a"],Esc0jq
Explanation:
qq: Start recording macro in register q
f: Go to first space character
a: : Insert after (the space character from above)
\[": Insert those characters
Esc: Back to normal mode
$: Go to end of line
a: Insert after (end of line)
"],: Insert the characters
Esc: Back to normal mode
0: Jump to start of line
j: Go down one line
If you have 100 such lines, you can do 100#q to achieve your result.
With vim substitute command:
:%s/.*/["&"]/
If you don't want to operate on all lines, then select the ones you want to transform or note the related line numbers, and then type :s/..... without the %. You'll see actually :'<,'>s this range represent the visually selected lines, and vim adds it automatically in visual mode.
On Atom you can enable the find to use Regex in the search(there is a button next to the search field)
Then you can search for something like (^.*$) to get every line separated by groups and in the Replace field you use ["$1"],. The $1 represents the value matched by the Regex.
Then just do a Replace All and remove the last comma in your list if needed.

Vim - Insert something between every letter

In vim I have a line of text like this:
abcdef
Now I want to add an underscore or something else between every letter, so this would be the result:
a_b_c_d_e_f
The only way I know of doing this wold be to record a macro like this:
qqa_<esc>lq4#q
Is there a better, easier way to do this?
:%s/\(\p\)\p\#=/\1_/g
The : starts a command.
The % searches the whole document.
The \(\p\) will match and capture a printable symbol. You could replace \p with \w if you only wanted to match characters, for example.
The \p\#= does a lookahead check to make sure that the matched (first) \p is followed by another \p. This second one, i.e., \p\#= does not form part of the match. This is important.
In the replacement part, \1 fills in the matched (first) \p value, and the _ is a literal.
The last flag, g is the standard do them all flag.
If you want to add _ only between letters you can do it like this:
:%s/\a\zs\ze\a/_/g
Replace \a with some other pattern if you want more than ASCII letters.
To understand how this is supposed to work: :help \a, :help \zs, :help \ze.
Here's a quick and a little more interactive way of doing this, all in normal mode.
With the cursor at the beginning of the line, press:
i_<Esc>x to insert and delete the separator character. (We do this for the side effect.)
gp to put the separator back.
., hold it down until the job is done.
Unfortunately we can't use a count with . here, because it would just paste the separator 'count' times on the spot.
Use positive lookahead and substitute:
:%s/\(.\(.\)\#=\)/\1_/g
This will match any character followed by any character except line break.
:%s/../&:/g
This will add ":" after every two characters, for the whole line.
The first two periods signify the number of characters to be skipped.
The "&" (from what I gathered) is interpreted by vim to identify what character is going to be added.
Simply indicate that character right after "&"
"/g" makes the change globally.
I haven't figured out how to exclude the end of the line though, with the result being that the characters inserted get tagged onto the end...so that something like:
"c400ad4db63b"
Becomes "c4:00:ad:4d:b6:3b:"

Delete all text in line before/after highlighted search pattern

I know in VIM how to search a string and delete the text till the start/end of line but I would like to know if it is also possible to delete all text in line before or after highlighted search pattern.
If you want to do this across all lines and don't want to retype your search term I'd suggest the following:
:%s/.*\ze<Ctrl-r>///
What this does is:
%s/: substitute across all lines in a file
.*: match any character
\ze: end matching so the rest of the pattern is not substituted
<Ctrl-r>/: insert text from the '/' register (which is the search register)
//: replace with nothing
Edit: Forgot about the after part. My suggestion to remove both at the same time would be:
:%s/.*<Ctrl-r>/.*/<Ctrl-r>//
To delete the text before FOO on the same line:
:s/^.*\(FOO\)/\1/
From beginning of line to the beginning of highlighted search pattern: 0dn
From after end of highlighted search pattern to the end of line: $N//e<Enter>lD
These will work in most of the cases.
I can't comment on other answers, so I answer here, but I am referring to the answer from xofon:
Just add a '%' in the command line, which would make do for all lines in a file.
delete all chars after ']' in all lines
:%s/\(\]\).*$/\1/
delete all chars before ' -- ' in all lines in a file
:%s/^\( -- \).*/\1/
To delete all text in the line line both before and after the search match you could also do:
:g//norm gnd0PlD
This executes normal mode commands on all lines that match the last search pattern. The commands are gn to select the match, d to delete it, 0P to paste it at the beginning of the line, l to move to the left (after the text that was just pasted) and D to delete until the end of the line. I'm given to understand gn is a fairly recent addition to vim, so YMMV.

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