4

Sometimes, when I send a shell command from vim, I receive an error.

For example, when I type the following command :!chmod +x shellescape("%"), zsh complains with :

zsh:1: unknown sort specifier

shell returned 1

I know a way to fix this specific problem :

:execute "!chmod +x " . shellescape("%")

What I don't know is how to read the exact command that the shell tried to execute when I make a mistake.

The last commands are not sent directly to the shell, vim evaluates a few things before doing so. I would like to see the result of this evaluation.

I tried looking at the output of the history command in zsh but it seems that the commands typed in vim are not logged there.

1
  • 5
    set the 'verbose' option to 5 before executing a shell command. Commented Dec 1, 2015 at 8:05

1 Answer 1

2

A bigger problem here is that your command is wrong.

:!chmod +x shellescape("%")

What makes you think you can run Vim functions like this? Use %:S to escape special shell characters, eg:

:!chmod +x %:S

I realize this doesn't answer your question directly, but for all practical purposes all you need to know "If error, use %:S, if still error, read what you typed", as there aren't any other substitutions done in the :! command.

5
  • Thank you for your answer. Yes I agree my example was poorly chosen. There's a very good reason why I used it though (for testing and understanding a few things). But in fact the errors I come across are given by other more complex shell commands that I still haven't understood. For example, I would like to populate a local arglist with the output of a shell command (a find command with a wildcard). It should be possible. There are even examples given in the help (:help backtick-expansion), but on my system they don't work. I've got the error E79.
    – saginaw
    Commented Dec 2, 2015 at 6:40
  • I need to see what it sends exactly to understand where the problem comes from. I hope the answer of Christian Brabandt will help me. I didn't want to use this more complex shell command as an example because this is a separate problem, I will open a new topic for it if I can't find an answer on my own. Thank you again for your answer, I didn't know the :S filename modifier, I'll use it from now on.
    – saginaw
    Commented Dec 2, 2015 at 6:40
  • Well, in fact, you are completely right, my problem doesn't come from the shell but from vim itself. E79 is not a shell error, sorry I made another mistake.
    – saginaw
    Commented Dec 2, 2015 at 6:46
  • 1
    @saginaw The :S modifier is a relatively recent addition to Vim, so depending on your portability requirements, it may be safer to stick with :exe and shellescape().
    – jamessan
    Commented Dec 4, 2015 at 13:38
  • @jamessan I didn't know that, thank you. Besides, shellescape() (for shell arguments) and fnamescape() (for vim commands arguments) stick together better in my memory than :S and fnameescape().
    – saginaw
    Commented Dec 4, 2015 at 13:51

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.