Reputation: 319
I have a vim function:
function! Pycom(word)
let a:inserted_word = ' ' . a:word . ' '
let a:word_width = strlen(a:inserted_word)
let a:length_before = (118 - a:word_width) / 2
let a:hashes_before = repeat('#', a:length_before)
let a:hashes_after = repeat('#', 118 - (a:word_width + a:length_before))
let a:hash_line = repeat('#', 118)
let a:word_line = '# '. a:hashes_before . a:inserted_word . a:hashes_after
:put =toupper(a:word_line)
endfunction
noremap <leader>pc :call Pycom('')<Left><Left>
that creates Python comments. The output is like so:
# ########################################### Hello World ############################################
How can I create a key mapping to place in my vimrc to create my comment string with only Vim commands? I need to do this because I use PyCharm and in Vim emulation mode it does not allow calling functions.
Upvotes: 0
Views: 62
Reputation: 172500
If just :function
were missing, you could inline the individual commands into a long, unsightly command sequence. But it's unlikely that a Vim emulation will just omit functions; the problem is that Vimscript itself is tightly bound to Vim itself, and mostly just specified by the behavior by its sole implementation inside Vim. So I'd guess that strlen()
, repeat()
, and even :put ={expression}
won't work in PyCharm, neither.
Most Vim emulations just offer basic :map
commands. Without knowning anything about PyCharm, the following general approaches can be tried:
:!
(or :{range}!
) command to invoke external commands, you can implement the functionality in an external command, implemented in whatever programming language you prefer. (If the emulation doesn't offer the :{range}!
command, you'd have to :write
the buffer first and pass the filename and current location somehow to the external command.)In general, Vim emulations are quite limited and can only emulate basic vi features (though I've also seen custom re-implementations of some popular Vim plugins, like surround.vim).
To get more of Vim's special capabilities into your IDE, use both concurrently; it is very easy to set up a external tool in your IDE that launches Vim with the current file (and position). Automatic reloading of changes (in Vim via :set autoread
) allows you to edit source code in both concurrently.
Upvotes: 3