Easily print-debug any text object in Vim

vim, linux

First things first, dependencies:

And add this to your vim config:

VIM
function! LogInline(text)
return 'console.log(' . a:text . ')'
endfunction
function! LogBelow(text)
let endPos = g:TextTransformContext['endPos']
let lineNumber = endPos[1]
call append(lineNumber, split('console.log(' . a:text . ')', "\n"))
endfunction
call TextTransform#MakeMappings('', '<Leader>l', 'LogInline')
call TextTransform#MakeMappings('', '<Leader>L', 'LogBelow')

Examples

First lets try doing an inline log.

JS
"hello world"
<Leader>ll
JS
console.log("hello world")

Next we'll log an existing variable.

1
cursor must be somewhere here
JS
const1myvar= "hello world"
1
cursor must be somewhere here
<Leader>Liw
JS
const myvar = "hello world"
console.log(myvar)

It even works on more complex text objects like HTML

1
cursor must be somewhere here
2
or here
JS
1<div>
<p>hello</p>
2</div>
1
cursor must be somewhere here
2
or here
<Leader>Lat
JS
<div>
<p>hello</p>
</div>
console.log(<div>
<p>hello</p>
</div>)

Hopefully this saves someone's fingers.

Subscribe

Webmentions