Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6720231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:11:17+00:00 2026-05-26T09:11:17+00:00

I’m trying to write a simple function in Vim to return the results of

  • 0

I’m trying to write a simple function in Vim to return the results of a powershell command. I keep getting gibberish in the results though.

I think this may be an encoding problem, but as you’ll see the problem is strange since it “sort of works”. I don’t have any solution to the problem though.

With the following non-default shell options set in vim:

set shell=powershell
set shellcmdflag=-c

Given the following function:

function! Test()
   let result = system("ls")
   call setline(1, result)
endfunction

When I run (from C:\Windows):

:call Test()

The following is written to my buffer:

^@^@    Directory: C:\Windows^@^@^@    Mode            LastWriteTime   Length Name ^@-------
 ....continues

However when I run the following command:

:r!ls

I get back exactly what I would expect (i.e. the powershell results of ls)
Even more interesting is when I run the command:

:echo system("ls")

The results look correct

I’ve tried modifying my original function as follows:

function! Test()
   let result = system("ls")
   echo result
   call setline(1, result)
endfunction

and the value echo’ed out is exactly what I would expect – yet I still see gibberish

I’ve also tried the following modification to my function:

function! Test()
   let result = system("ls")
   let conv = iconv(result, "utf-8", &enc)
   call setline(1, conv)
endfunction

But the results are exactly the same (i.e. they include the ^@^@ symbols and other gibberish)

My guess as to what’s happening is that powershell cmds which are redirected using > produce utf-16 output, and vim is unable to deal with this. I get the following from powershell (file in this case is the gnu32 program):

PS> ls > test
PS> file test
test; Little-endian UTF-16 Unicode text, with CRLF, CR line terminator

I’ve also tried playing around with $OutputEncoding, without any success, as described here:
http://blogs.msdn.com/b/powershell/archive/2006/12/11/outputencoding-to-the-rescue.aspx

Anyone have any ideas what I’m doing wrong here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:11:17+00:00Added an answer on May 26, 2026 at 9:11 am

    I’ve finally figured this out.

    The following modification to my function does pretty much exactly what I want:

    function! Test()
      let @r = system("ls")
      put! r
    endfunction
    

    I think Artomegus is correct, the problem is really the carriage return, and not an encoding problem.

    As a side note, I ran into problems when I had only the following in my .vimrc file:

    set shell=powershell
    set shellcmdflag=-c
    

    With just these settings when I ran :call Test() I would get an error about Vim being unable to read the temp file.

    The fix for this problem is to add the following to your .vimrc file:

    set shell=powershell
    set shellcmdflag=-c
    set shellquote=\"
    set shellxquote= 
    

    You must set the shellquote to be \” and shellxquote to be a blank space (i.e. empty) because by default on windows, shellxquote is set to \” which overrides the value of shellquote. This is problematic when using the system function, which runs the vimrun.exe program behind the scenes.

    Hopefully this helps someone else. I’ve been stuck on this for a long time, but now powershell works perfectly for me with Vim.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.