Vim: Convert hard wrap to soft wrap [duplicate] - linux

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Unwrap text in vim
How to convert hard wrap into soft wrap in a text file, using vim or (if simpler) some other standard GNU/Linux tool? And this while preserving paragraphs as such. It would be easy to remove all line breaks, but not serviceable.
By hard wrap I mean a document where each line ends at most at (for example) column 80.
I noticed that the program aquamacs has a function to do this, but I don't know what its output looks like, and it is Mac OS X only.

You can do this:
:%norm vipJ
It will unwrap all the paragraphs in you text. However, if you want to do it manually, simply do ipJ inside visual mode on each paragraph you want to unwrap.
I also found another way to achieve this
:g/^\s*\n.*\S$/+norm vipJ
which means:
:g "Execute command when pattern matches
Pattern:
^\s*\n "A line with only spaces or tabs (or none)
\n.*\S$ "A line with anything but ending with a non-space character
Command:
+norm vipJ "Join the lines in the paragraph
Please note that you'll need an empty line before the first paragraph too.

Related

How to call a function when text is wrapped in vim?

In vim I want to visually make transparent the space I have to write a text in markdown. I use hard wrapping with textwidth=79. I know by some calculation that I'll have 20 lines for a chapter for example. So, what I do is inserting 20 empty lines to get a visual feeling for what I can write. After writing some lines, I manually delete the number of lines already written from the empty lines, so that the visual impression still is correct.
What I want to do, is to automate this deletion process. That means I want vim to automatically remove one line below the last written line if this line is empty and after vim automatically started a new line because I reached 79 characters in the line before. How can I do this?
I know that there are autocommands in vim but I haven't found an <event> that fits to the action: after vim automatically hard wraps a line / reached new line in insert (or however you would like to describe it)
I don't think there's an event for that particular action but there's a buffer-local option called formatexpr that gq & co will use, if set. So you can write a function that inspects any placeholder whitespace, if existing. That function can call the text format command gqq to maintain original feel (+ the cursor movement to the new, empty line).

Adding words in front and at the end of line in VIM [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Replace while keeping certain “words” in vi/vim
Let's say I have several lines like
$adv_id;
$am_name;
$campaign_ids;
$repeat_on;
$opt_days;
$opt_time;
$am_or_pm;
Let's say I use visual mode to select all the lines.. how can I add characters at the start and the end of each line so it looks something like
$_REQUEST($adv_id);
$_REQUEST($am_name;
$_REQUEST($campaign_ids);
$_REQUEST($repeat_on;
$_REQUEST($opt_days);
$_REQUEST($opt_time);
$_REQUEST($am_or_pm);
Pretty similar to your other question, so the explanation there should help you to understand this substitute. With the lines selected as a visual block, use this substitute command:
:'<,'>s/\$\(.*\);/$_REQUEST(\1);
As before, the '<,'> will be auto-filled for you in the command-line if you have a visual selection.
The difference here is that we're using \(\) to make a capturing group, which will capture part of the regex, and use it again in the replacement, using \1, which refers to the first capturing group.
Also, since this regex uses $ literally to position the replacement, it needs to be escaped: \$, since it has a special meaning in the regex: end of line.
If you'll need to have multiple replacements on a single line, you'll need to add the g flag, and you may want to remove the semicolon:
:'<,'>s/\$\(.*\);/$_REQUEST(\1)/g
The reverse regex, e.g. replacing $_REQUEST($adv_id); with $adv_id;, is pretty similar:
:'<,'>s/\$_REQUEST(\(.*\))/\1
Here we capture everything between the parens in a $_REQUEST(...); in a capturing group, and that capturing group is the entire replacement.
In visual mode, hit : and use this in the command line:
:'<,'>s/^\(.*\);$/$_REQUEST(\1);/g
The \( and \) capture the matched expression for the line and the \1 recalls the captured group in the substitution.
Using the :'<,'> tells Vim to filter the current selection through the following command (which is s in this case).

How to repeat the same search and replace command over disjunct line ranges in Vim?

I had a situation where I wanted to replace FOO with BAR through out a file. However, I only want to do it in certain places, say, between lines 68–104, 500–537, and 1044–1195. In practice, I dropped markers at the lines of interest (via ma, mb, mc, etc.) and ran the following:
:'a,'b s/FOO/BAR/g | 'c,'d s/FOO/BAR/g | 'e,'f s/FOO/BAR/g
I had to repeat this dozens of times with different search and replace terms s/CAT/DOG, etc., and it became a pain to have to rewrite the command line each time. I was lucky in that I had only three places that I needed to confine my search to (imagine how messy the command line would get if there were 30 or 40).
Short of writing a function, is there any neater way of doing this?
On a related note. I copied FOO to the s (search) register, and BAR to the r (replace) and tried running
:'a,'b s/\=#s/\=#r/ | 'c,'d s/\=#s/\=#r/ | 'e,'f s/\=#s/\=#r/
This would have saved me having to rewrite the command line each time, but, alas, it didn’t work. The replace bit \=#r was fine, but the \=#s bit in the search pattern gave me an error.
Any tips would be appreciated.
If you need to perform a set of line-wise operations (like substitutions) on a bunch of different ranges of lines, one trick you can use is to make those lines look different by first adding a prefix (that isn't shared by any of the other lines).
The way I usually do this is to indent the entire file with something like >G performed on the first line, and then use either :s/^ /X/ commands or block-visual to replace the leading spaces with X on the lines I want.
Then use :g in conjunction with :s. eg:
:%g/^X/s/FOO/BAR/g
:%g/^X/s/BAZ/QUUX/g
Finally, remove the temporary prefixes.
In order to get rid of the necessity to retype the same search
pattern, substitution string and flags, one can simply use the
:& command with the & flag:
:'a,'bs/pat/str/g | 'c,'d&& | 'e,'f&&
(See :help :& for details.)
Instead of using marker use this one :
:68,104s/FOO/BAR/g << substitue from line 68 to 104
This should make your job a little bit easier and clearer.
inspired by #Vdt's answer:
I am not sure but you could write all the substitutions down in a file and source that file i think.
substitutions.vim:
68,104s/FOO/BAR/g
168,204s/FOO/BAR/g
618,644s/FOO/BAR/g
681,1014s/FOO/BAR/g
.
.
.
68,104s/BAZ/BOOO/g
168,204s/BAZ/BOOO/g
and then :so substitutions.vim maybe you can also use this for multiple files of same structure. you can add an e to add an ignore error message, if it is not clear that the substitutions are found on the corresponding line blocks.
With q:, you can recall previous command lines and edit them as a normal Vim buffer, so you can quickly replace FOO and BAR with something else, then re-execute the line with Enter.
The s/\=#s/\=#r/ doesn't work; as you said, this only works in the replacement part. But for the pattern, you can use Ctrl + R Ctrl + R s to insert the contents of register s, instead of \=#s. Preferably use the default register, then it's a simple s//, but you probably know that already.
When performed over a closed fold, substitutions are limited to that fold.
fold each region
put the cursor on one closed fold
perform the substitution: :s/foo/bar<CR>
move to the next closed fold with zj or zk
use the command-line history: :<C-p><CR> or :<Up><CR> to perform the same substitution
repeat…
You can also add the c flag at the end of your substitution so that Vim asks you for a confirmation before actually performing it. This can be tedious if you have lot of matches.
Here's the simplest way to do it
:5,10s/old/new/g
5,10 : startlinenum,endlinenum

How to unwrap text in Vim?

I usually have the tw=80 option set when I edit files, especially LaTeX sources. However, say, I want to compose an email in Vim with the tw=80 option, and then copy and paste it to a web browser. Before I copy and paste, I want to unwrap the text so that there isn't a line break every 80 characters or so. I have tried tw=0 and then gq, but that just wraps the text to the default width of 80 characters. My question is: How do I unwrap text, so that each paragraph of my email appears as a single line? Is there an easy command for that?
Go to the beginning of you paragraph and enter:
v
i
p
J
(The J is a capital letter in case that's not clear)
For whole document combine it with norm:
:%norm vipJ
This command will only unwrap paragraphs. I guess this is the behaviour you want.
Since joining paragraph lines using Normal mode commands is already
covered by another answer, let us consider solving the same issue by
means of line-oriented Ex commands.
Suppose that the cursor is located at the first line of a paragraph.
Then, to unwrap it, one can simply join the following lines up until
the last line of that paragraph. A convenient way of doing that is to
run the :join command designed exactly for the purpose. To define
the line range for the command to operate on, besides the obvious
starting line which is the current one, it is necessary to specify
the ending line. It can be found using the pattern matching the very
end of a paragraph, that is, two newline characters in a row or,
equivalently, a newline character followed by an empty line. Thus,
translating the said definition to Ex-command syntax, we obtain:
:,-/\n$/j
For all paragraphs to be unwrapped, run this command on the first line
of every paragraph. A useful tool to jump through them, repeating
a given sequence of actions, is the :global command (or :g for
short). As :global scans lines from top to bottom, the first line
of the next paragraph is just the first non-empty line among those
remaining unprocessed. This observation gives us the command
:g/./,-/\n$/j
which is more efficient than its straightforward Normal-mode
counterparts.
The problem with :%norm vipJ is that if you have consecutive lines shorter than 80 characters it will also join them, even if they're separated by a blank line. For instance the following example:
# Title 1
## Title 2
Will become:
# Title 1 ## Title 2
With ib's answer, the problem is with lists:
- item1
- item2
Becomes:
- item1 - item2
Thanks to this forum post I discovered another method of achieving this which I wrapped in a function that works much better for me since it doesn't do any of that:
function! SoftWrap()
let s:old_fo = &formatoptions
let s:old_tw = &textwidth
set fo=
set tw=999999 " works for paragraphs up to 12k lines
normal gggqG
let &fo = s:old_fo
let &tw = s:old_tw
endfunction
Edit: Updated the method because I realized it wasn't working on a Linux setup. Remove the lines containing fo if this newer version doesn't work with MacVim (I have no way to test).

How do you enable word wrap in vim when printing

I wanted to print a simple text document and make sure words wrap on word boundaries. I tried both
set linebreak
and
set wrap
but when printing, it just breaks on the right column in the middle of words. Is this possible for printing?
You are creating a text file without any built-in linebreaks so each paragraph is a single "line", even though with linebreak and wrap set, it looks like they are multiple lines). This is why printing breaks at fixed places. (According to http://www.vim.org/htmldoc/various.html#printing it does not seem like you can have vim respect linebreak/wrap during print.)
To avoid this, if you want text to wrap while you are editing, do
set textwidth=70
to wrap at the 70th column. If you want your file to have long lines (e.g., so it formats fine when loaded into MS Word or something), then you will have to pre-process the text version before printing it. So, for example, you can try:
fmt file.txt | lpr
or if you have enscript installed, you should be able to try:
enscript --word-wrap file.txt
to print. An existing file can be wrapped by running in vim:
gggqG
that is, 'gg' to go to start of file and 'gqG' to reformat 'gq' from the current position (i.e. the first line) to the last line (by going to 'G'). 'gq' will respect your current textwidth setting.

Resources