In my Vim setup I'd like to add a configuration that runs a shell command with a certain mapping. Is there any possible way to do this in Vimscript?
2 Answers
vim has a a system() function:
:call system('date')
1 Comment
unrealapex
vim.fn.system() if you are using Neovim's API.I do this with traditional vi, so I assume it would work with vim as well.
In my .exrc I have:
map ^_ !}fmt 71 72^M
(That's a ^_ entered by typing ctrl-V ctrl-_, and a ^M entered by typing ctrl-V ctrl-M
When I hit ctrl-_ in vi, it reformats my current line to 72 characters.
3 Comments
ZyX
There is
<C-_> for ^_ and <CR> for ^M (in vim, not in vi). Do not suggest using raw control codes in vimrc, this scraps the output of tools that expect text (like cat vimrc | grep fmt). It may also make VCS systems take vimrc as a binary file effectively disabling plain vcs diff and scrapping output of vcs diff -a just as it does for grep. There is also nnoremap in vim for this sort of thing (you need neither mode other then normal nor user mappings expansion here).ghoti
Good points. Perhaps between my (vi-oriented) answer and your refinement, the OP will get a good answer. :-)
Lambart
What on earth does this have to do with the question?