Home > Enterprise >  NVIM: executing external command does not execute command - but also throws no error
NVIM: executing external command does not execute command - but also throws no error

Time:07-10

I am trying to get Nvim to execute the ls command by typing :!ls, this is the output: output picture - the german text is irrelevant (interestingly after typing :!ls and pressing enter, the ls gets put in quotes)

my init.vim file looks like this: init.vim picture

The interesting thing is, if I change the shell-setting to cmd, the output of :!ls now looks like this (which is exactly what a new cmd shell looks like): output picture with cmd shell

So it looks like, using the external command, nvim starts a shell but does not execute the ls command. I should also mention that before I created the init.vim file, the external command was working - however - it was using cmd instead of powershell, which I do not want and which is why I created the init.nvim file.

PS: at first I've also tried setting shell=powershell, which didn't work either

PPS: in vim :!ls works as expected

CodePudding user response:

(To get cmd.exe to work, you'd need to set shellcmdflag to /c, otherwise any command passed is ignored, as you've experienced.)

The culprit turned out to be \" as the shellquote setting, as a result of which what you typed was accidentally getting enclosed in \"...\" when passed to the PowerShell's CLI's -Command parameter, causing PowerShell - after its command-line parsing - to execute "ls", including the quotes; the next section explains how PowerShell interprets such a command.

The correct setting is:

set shellquote="

Your first screenshot implies what PowerShell ended up executing was "ls", including the double quotes, which predictably results in verbatim ls getting printed:

To PowerShell, "ls" is a double-quoted string literal, whose value is simply echoed when submitted as a command, due to PowerShell's implicit output behavior (see the bottom section of this answer for background information).

That is, "ls" is equivalent to echo "ls", to put it in terms of a traditional shell (echo in PowerShell is an alias of the Write-Output cmdlet).

To submit ls (an alias of Get-ChildItem) as a command:

  • Do not use quoting, given that it isn't needed here (the command name, ls, contains neither spaces nor other PowerShell metacharacters):

    :!ls
    
  • Alternatively, use &, the call operator:

    :!& "ls"
    

See this answer for why use of & is needed with quoted commands.

  • Related