Add a number of '=' in a rest (reStructuredText) document that equals to characters from last line?

StackOverflow https://stackoverflow.com/questions/4593728

  •  15-10-2019
  •  | 
  •  

Question

I want to use a shortcut to add needed = (from Section/Title reStructuredText syntax) according to the last line.

So, suppose (being | the cursor position)

Title
|

and pressing an specific mapping mapped to a function, add a number of = that equals to the last line (where Title is), becoming:

Title
=====|
Was it helpful?

Solution

This sequence will get you close:

kyyp:.s/./=/g

Duplicate the previous line, then in that line, change every character to an equals sign. Map that to a key sequence you like, and try it out.

OTHER TIPS

Another way:

:execute "normal " . strlen(getline(line(".") - 1)) . "i="

strlen(getline(line(".") - 1)) returns the lenght of the line above the current position. The result is that the command Ni= is executed, inserting = N times.

For a mapping I would have used:

put=repeat('=', col('$')-1)

For something more interactive, I would have use the same solution as Ned's.

(I don't like my mappings to change the various registers like @" or @/)

My vim-rst-sections vim plugin will convert lines to section headings:

http://www.vim.org/scripts/script.php?script_id=4486

In your case, you'd put the cursor on the line, and type <leader><leader>d to get a top-level heading like this:

#####
Title
#####

A few repeats of <leader><leader>d will take you down to the standard hierarchy of Python ReST sections to the =.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top