I'd like to create a vim function to automatically add the code highlight tags, e.g. ```python
and ```
around fenced code blocks to have syntax highlighting in my markdown previewer (I'm using iamcco/markdown-preview.nvim
which is great). The best I've got so far is using ^\S.*$\n\(^$\n\)\+\(\s\{4,}.*$\n\|^$\n\)\+
to match the code blocks and a preceding non-empty line. I then go on to use (defined as a function)
let lang = input('enter the highlighting language : ')
inoremap jj <Esc>
g@^\S.*$\n\(^$\n\)\+\(\s\{4,}.*$\n\|^$\n\)\+@ execute "normal :delmarks!" | execute "normal jmai```".lang."jjld$}Pmb" | 'a,'bs/^\s\{4}//g
trying to add the highlighting flags and removing the four leading spaces. However, it is not working properly since }
in the third line jumps to the first empty line inside the code blocks instead of the end. For example, for the code blocks (four leading spaces are explicitly shown)
def foo(*args):
print args
foo(1, 2, 3) # (1, 2, 3)
the above function ends up with
```python
def foo(*args):
print args
```
foo(1, 2, 3) # (1, 2, 3)
How do I properly jump to the end of the match and fix the function? Or does anybody has better alternatives in mind?
:s
command. To unindent the block, you can use\=
on the replacement and thesubstitute()
function to perform the further substitution.:call search('', 'e')
:s
to do the workflow, adding language tags first and then unindent in one time. An complete answer will be appreciated!}
with:call search('', 'e')
in mycmd
part, however, fails to jump to the end.