I want to reformat my file so the max line width is 79. I did :set tw=79 and gggqG and the results weren't what I expected. When a line is less than 79 col, characters from the line below it move up and lines with over 79 col don't break into two lines.
edit: Well I was semi-mistaken in that it DOES break lines over 79 except in the line with asterisks.
--CONVENTIONS**************************************************************************************
In addition it still moves characters up when the line has <79.
One possible solution, although not the best one.
Undefine formatexpr and let external fold program to format your text to 79 characters width.
:set formatexpr=
:set formatprg=fold\ -w\ 79
And now:
gg # Go to beginning of file.
gq # Format until...
G # End of file.
And last remove those carriage returns (^M):
:%s/\r//g
In my test it changed some accented characters and some other lines were mangled, but try it yourself. Else you may write your own format function and use it with formatexpr option.
Put empty line between different paragraphs. Then do the reformat.
Related
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 %.
I have a file in which first 10 lines are the columns of a table and the rest 10 lines are the values of each column.
How can I use norm in VIM to append the values after each column names like this:
column1
...
column10
value1
...value10
-->
column1: value1
...
column10: value10
It is a little similar with this(Vim - Copy Nth word of each line, from line 10-100, to end of line), but I don't know how to go to line 1:10 and append the copied lines.
Any idea will be appreciated!
Fairly naive and crude way to do this, but:
:1,10norm! 10j0d$10kA: ^[p
Explanation:
1,10norm!: for lines 1 to 10, do the following (the ! means any custom mapping you have will be ignored, thanks to D. Ben Knoble for reminding of this):
10j: move down 10 lines
0d$: delete the whole line (not including newline)
10k: move back up 10 lines
A:: append (at the end of the line) ': ' (note the trailing space)
^[: input escape character, going back to normal mode. This (^[) is a single character and is inputted by typing Ctrl-v then escape, not by typing ^[.
p: paste the line deleted in step 3
Another (copy-pastable) way, (ab)using the substitute command:
:1,10s/\v(.*)\ze(.*\n){10}(.*)/\1: \3/ | 11,20d
which does:
1,10s/: for lines 1 to 10, execute the following substitution:
\v: use very-magic regex mode (see :help \v)
(.*): capture the entire current line (eg column1)
\ze: signal the end of the match. This way everything read (and captured) afterwards will not be affected (but can still be read)
(.*\n){10}: skip 10 (including current) lines, ie skip selector to 10 lines below
(.*): capture the line (eg value1)
/: end the 'select' part of the substitute command
\1: \3: replace with captured groups (eg column1: value1)
|: command separator
11,20d: delete lines 11 to 20
Use blockwise-visual mode to perform the operations.
You can enter visual block mode with Ctrl-V and it allows you to select and operate on columns. It also allows you to perform the same action on a block, which you can use to add the : to the lines with the column names.
I'll use normal Vim syntax for keystrokes in my examples, <C-v> means Ctrl-V.
Start by deleting the values into the default register, using a visual block:
11G<C-v>9j$d
Then Add the : to the column lines, also using a visual block:
1G<C-v>9j$A: <Esc>
Then add some more spaces to the first line, to ensure there's room for all the column names to fit:
A <Esc>
Finally, put the visual block at the end of the first line:
$p
It will actually put it on all lines all the way to the end.
This is slightly different from what you specified, since the values are all aligned on the same column. If you want different spacing, you can perhaps use a :s operation to fix spacing.
10:s/: */: /<cr>
Depending on where you pasted (if some column names had more trailing spaces than the first one), you might have some trailing spaces after the pasted values to fix as well, but that should be easy to do using a similar procedure.
Visual block operations are really powerful, it's a great feature to learn and keep in your "toolbox" in Vim. They're really handy with this kind of problem where thinking in "columns" makes the most sense.
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)
I have a text file containing some very long lines with no spaces.
These lines contain no whitespace or other common delimiters.
I would like to split these long lines into seperate lines so that no line in the text file is longer than 80 characters. Is this possible inside vim or perhaps using some other tool?
Quick vim solution:
assume we have a long line of text:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
that we want to split into pieces of 10:
gg (for first line),
q[letter] (for recording a macro),
10l (for going right n times),
a return ESC (for entering a linebreak after current character and leaving insert mode),
q (to stop recording).
We should now be on line 2 with our cursor, looking like this:
aaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
A quick 100#[letter] formats the entire line:
aaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaa
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).