Vim: generate table of contents for plain text files (not Markdown)? - vim

I'm aware of Vim plugins such as vim-markdown-toc, etc. but those apply to markdown files. Are there any approaches/suggestions for auto-generating a tables of contents in plain-text files (.txt, or no extension) in Vim? For example, something like:
CONTENTS
∙ Executive Summary
∙ Technical Review
∙ Biomedical Natural Language Processing
∙ Preliminary Work
... where (for convenience) headers could adopt the markdown syntax:
# EXECUTIVE SUMMARY
Lorem ipsum dolor sit amet, consectetur adipiscing elit. ...
...
# TECHNICAL REVIEW
Lorem ipsum dolor sit amet, consectetur adipiscing elit. ...
...
## BIOMEDICAL NATURAL LANGUAGE PROCESSING
Lorem ipsum dolor sit amet, consectetur adipiscing elit. ...
...
# PROPOSED WORK
Lorem ipsum dolor sit amet, consectetur adipiscing elit. ...

You could use something like that:
:g/^#* /t0
This will copy all this lines to the top. Unfortunaly in the wrong order, reverse it:
:0,4g/^/m0
(You may need to increase the numer 4 here, it is for your example). Then select the lines in visual line mode and replace # with tab or spaces.
:'<,'>s/#/ /g
Then do some formating. You could of course automate this further.

Related

Vim: same change to all line-endings of selected lines?

In https://stackoverflow.com/a/11790464/5354137, there is a very detailed description how to, e.g. perform the same insert to the beginnings of all selected lines by placing the cursor over the first character of the first line, then after <C-v> moving down, then Isomething<Esc><down> inserting something everywhere.
But how does one do the same thing in the case of lines of unequal length, such as:
'Lorem ipsum
'dolor sit amet,
'consectetur adipiscing elit,
'sed do...
to their respective line endings? E.g. if I wanted to insert ', after m, ,, , and ., how would I do that without resorting to :substitute? (Or to a plugin, just using original vim-fu...)
Substitute (original answer)
The easiest way to do it is with the :substitute command. Select the lines in visual mode and then do:
:'<,'>s/$/',/<CR>
The $ stands for the end of the line. Note that vim will already write the :'<,'> part, so you only have to type from there.
Using a macro
With the question having been updated and :substitute off the table, I'd suggest recording a macro. Place the cursor on the line with "Lorm ipsum" and do:
qaA',<Esc>jq
then call the macro three times for the remaining lines. Either by hitting # a lot:
#a####
or with the more elegant
3#a
Using a recursive macro
As commented by #AndR, it's possible to record a macro that calls itself which will run until it reaches the end of the buffer:
qaqqaA',<Esc>j#aq
Note that the macro already calls itself with #a.
Afterwards, call #a and single quotes will be appended to every line until the end of the buffer.
Without visual selection
Both #AndR's answer using :normal and my :substitute answer above rely on a visual selection of the lines to modify. It is possible without that by typing in a range or line numbers. For "Lorem ipsum..." on lines 1 through 4, type either of the two commands:
:1,4s/$/',/
:1,4norm A',
This has the benefit that it works without a visual selection and from anywhere in the buffer.
Based on your reference link and reproduction steps, this is your problem halfway:
But how does one do the same thing in the case of lines of unequal length ...
Follow the link and use the first method Using only visual-block mode, let's start with step 5:
Select any column, hit <C-v> to enter visual-block mode and expand your selection toward the bottom:
'Lo[r]em ipsum
'do[l]or sit amet,
'co[n]sectetur adipiscing elit,
'se[d] do...
Hit $ or <End> (see :help $), will get the selection like this:
'Lo[rem ipsum]
'do[lor sit amet,]
'co[nsectetur adipiscing elit,]
'se[d do...]
Hit A',<Esc> to achieve your goal:
'Lorem ipsum',
'dolor sit amet,',
'consectetur adipiscing elit,',
'sed do...',
Note and supplement:
If you use the latest Vim9 and your virtualedit option contains block, when you hit $ or <End>, you may see this selection (Vim8 won't):
'Lo[rem ipsum ]
'do[lor sit amet, ]
'co[nsectetur adipiscing elit, ]
'se[d do... ]
This could be a issue with the VIM9 or an intentional design? I don't know, but don't care, just press A. You'll see your cursor at the end of the first line, which is fine (| is the cursor position):
'Lorem ipsum|
'dolor sit amet,
'consectetur adipiscing elit,
'sed do...
Then you enter the text you need (e.g. ',) and <Esc> ends editing, it works fine:
'Lorem ipsum',
'dolor sit amet,',
'consectetur adipiscing elit,',
'sed do...',
As a supplement, oppositely, if you want to insert text in the same column where has no text when there are irregular endings : You can add block to your virtualedit option (see :h 'virtualedit'), using other left-right-motions (e.g. l or |) instead of $ to get this selection:
'Lo[rem ipsum ]
'do[lor sit amet, ]
'co[nsectetur adipiscing elit, ]
'se[d do... ]
When you press A, the cursor will be at (| is the cursor position):
'Lorem ipsum |
'dolor sit amet,
'consectetur adipiscing elit,
'sed do...
Then enter the text you want (e.g. ',), hit <Esc> to obtain:
'Lorem ipsum ',
'dolor sit amet, ',
'consectetur adipiscing elit, ',
'sed do... ',
Here the goal can be achieved by appending ' at the end of each line. In one line you'd normally do A'. In order to do this this in each line of the visual selection, run
:<,>norm A'
Assuming that you have selected the needed lines visually (with v or V).
See :h :normal for details.

How to append a character after any number followed by a dot in Vim using regex?

I want to append a character after any number followed by a dot in Vim using regex.
Example (append -):
1. Contrary to popular belief, Lorem Ipsum is not simply random text.
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.
4022. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Becomes:
1.- Contrary to popular belief, Lorem Ipsum is not simply random text.
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.
4022.- Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Please help.
:%s/\(\d\.\)/\1-/g
It changes this:
1. this 2. will
3_ do 4305. what 5.. you
ask 6*
to this:
1.- this 2.- will
3_ do 4305.- what 5.-. you
ask 6*
The way it works is to match and remember any digit \d followed by a dot \.
(the stuff between \( and \) is remembered for later use - these are called
capture groups). Then replace this with the remembered capture group using \1
and add a -. The g at the end makes happen for every match on each line.

Sublime Text 3 - Scroll Only Until the Last Line

Whenever I scroll a file all the way to the bottom of a text file, it will scroll far beyond the last line, and I end up with a blank screen.
Then, to view the last lines of text, I have to scroll a little bit up, and in huge log files it usually gets scrolled too much.
I'll give an example.
When I scroll all the way down, this is what I expect to see:
560 lorem ipsum
561 lorem ipsum
562 lorem ipsum
563 lorem ipsum
564 lorem ipsum
565 lorem ipsum
566 lorem ipsum
567 lorem ipsum
568 lorem ipsum
569 lorem ipsum
570 lorem ipsum
But instead I see this:
I know I can use Ctrl+End. But what I would like to know, is if there a way to change the annoying behavior of scrolling 50+ lines too much.
There is a setting that controls this, which defaults to being turned on except on MacOS, where it is disabled by defaul:
// Set to false to disable scrolling past the end of the buffer.
// On OS X, this value is overridden in the platform specific settings, so
// you'll need to place this line in your user settings to override it.
"scroll_past_end": true,
You can select Preferences > Settings from the menu and set the setting to false to disable this behaviour.

Variable-width hanging indents in vim?

This isn't a programming question per se, since I'm aiming to use this for note-taking -- but there just isn't any community more reliable for in-depth vim knowledge than this one.
What I'd like to do is set a hanging indent in vim - you know, the kind that looks like this:
Lorem ipsum dolor sit amet consectetur
adipisicing elit neque amet fuga
dolores voluptatem aspernatur
explicabo quasi. Nostrum...
Except I know you can do this with
set formatoptions+=2
What I'd really like is for vim to automatically set the width of the indent based on the presence of a special character in the first line. For instance, I might want to set this special character to ":" to format my text like so:
Lorem: Ipsum porro dolorem nostrum
incidunt similique a? Eaque
minus aliquid dolorem veritatis
omnis odit.
Quidem: distinctio quibusdam distinctio
accusamus alias magnam.
Voluptatem: dignissimos exercitationem
deleniti aliquam ratione?
Necessitatibus expedita
praesentium.
In my ideal scenario, this would also be compatible with vim's breakindent setting, which visually indents soft-wrapped lines (officially included in vim as of patch 7.4.338).
Does anyone know if this is possible - or even if this functionality can be achieved with a plugin?

is this correct for DK V1.0.0?

console.print('<Text>'>
prints text?
console.printVar(<Variable Name>)
prints variable?
Lorem ipsum dolor sit amet
Yes.
It's it's correct, and there are more commands coming soon

Resources