22

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 2

24

vim has a a system() function:

:call system('date')
Sign up to request clarification or add additional context in comments.

1 Comment

vim.fn.system() if you are using Neovim's API.
4

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

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).
Good points. Perhaps between my (vi-oriented) answer and your refinement, the OP will get a good answer. :-)
What on earth does this have to do with the question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.