Vim Tips: Writing ASCII lines quickly
I sometimes need to write an ASCII line (hr) quickly, like ----------
, here
are two ways I do it in Vim.
I sometimes will separate blocks of code with a comment like:
// ---------------------------------------------------------------------------
To do that quickly in Vim, I will press:
i
to enter Insert Mode/
,/
,space
to type out//
Esc
to exit Insert ModeA
,-
,Esc
append-
to the end of the line, 75 times
You can also use o
instead of A
to use this trick with lines instead of
characters, i.e. 5o- [ ] task
, Esc
would use o
5 times to write a new
line below the cursor with - [ ] task
.
When I want the line to be exactly as wide as the line above/below, like with a Markdown header or table:
Header
======
Here, when I have my cursor on the “Header” line, I press:
yyp
yank and paste the line belowj
move down to the newly pasted “Header” lineV
visually select the entire 2nd “Header” liner
replace the entire line with…=
the=
character
I’ll also use this with Markdown tables, and do something like:
| Num | Header 1 | Another Long Cell |
yypjVr-
copy and paste the header row, move down to it, visually select the new line, replace with all-
charactersk
move back up to the header row0
move the cursor all the way leftj
move back down to the first-
r|
replace with a|
characterl
move right one character to the 2nd-
r<space>
replace with a single ` ` characterk
move back up to header rowf|
move to next|
character
From here, I’ll rinse and repeat until the table looks like:
| Num | Header 1 | Another Long Cell |
| --- | -------- | ----------------- |
Happy lining!