我试图找出在控制台中编辑文本文件的最简单方法(在我的例子中是 PowerShell)。我正在使用 Windows 7 64 位。我不能只输入edit filename.txt
来编辑文件,这让我很恼火。这曾经有效,但这一切都改变了。在 Windows 控制台中查看和编辑文本文件的选项是什么,如果你告诉我安装和学习 VIM,我会打你的脸。:-)
17 回答
为什么不用记事本?
notepad.exe filename.txt
旧edit.com
的在 PowerShell 中工作(至少在我的盒子上:Windows 7 Pro x86)但在 x64 中由于其 16 位架构而无法工作。
你可以看看这个简单的编辑器。
它超级快,可以处理大型文本文件,尽管功能很少。包括 GUI 版本和控制台版本 (k.exe)。应该在linux上工作相同。
示例:在我的测试中,打开一个 500mb 的磁盘映像需要 7 秒。
在冒着打我的风险的同时,我想您仍然坚持您提到的解决方案。看看 SuperUser 上的这个帖子:
此外,还有一个适用于 windows 的 nano 版本:
我现在就躲起来,希望有人能给出更充分的答案。
有点复活,但对于其他提出这个问题的人来说,看看Micro编辑器。它是一个小型的独立 EXE,没有依赖项,具有本机 Windows 32\64 版本。在 PowerShell 和 CMD.EXE 中运行良好。
我同意斯文普拉斯的观点。Nano 是一个很好的选择。如果您有Chocolatey或scoop,您可以通过在 Powershell 中键入以下内容来安装 nano:
PS C:\dev\> choco install nano
# --OR--
PS C:\dev\> scoop install nano
然后,要编辑 somefile.txt 输入:
PS C:\dev\> nano somefile.txt
它很整洁!
编辑: Nano 在我的 Windows 10 机器上运行良好,但第一次在我的 Windows 7 机器上加载需要很长时间。这让我在我的 Win 7 笔记本电脑上切换到 vim (vi)
PS C:\dev\> choco install vim
PS C:\dev\> vim $profile
将 powershell 配置文件中的一行添加到 Set-Alias (sal)
sal vi vim
Esc - : - x - 输入 :-)
您可以安装Far Manager(顺便说一下,一个很棒的OFM)并像这样调用它的编辑器:
Far /e filename.txt
如果您使用 Windows 容器并且想要更改任何文件,您可以在 Powershell 控制台中轻松获取和使用 Vim。
使用 PowerShell 转入 Windows Docker 容器:
docker exec -it <name> powershell
首先获取 Chocolatey 包管理器
Invoke-WebRequest https://chocolatey.org/install.ps1 -UseBasicParsing | Invoke-Expression;
安装 Vim
choco install vim
刷新 ENVIRONMENTAL VARIABLE 你可以直接
exit
和 shell 回容器转到文件位置并 Vim 它
vim file.txt
从在线安装 vim,然后你可以这样做: vim "filename"来编辑该文件
您可以通过 choco 在 powershell 中安装 nano - 这是一种将文本编辑功能引入 powershell 的低摩擦方式:
- 安装巧克力
Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
- 安装纳米
choco install nano
- 利润
nano myfile.txt
最好的部分是它成为路径的一部分,并在重新启动后保持工作等 :)
我想你可以只使用记事本,像这样:
notepad myfile.extension
它应该在记事本中打开。
不确定这是否会使任何人受益,但如果您使用的是 Azure CloudShell PowerShell,您只需键入:
code file.txt
并且 Visual Studio 代码将弹出要编辑的文件,非常棒。
In linux i'm a fun of Nano or vim, i used to use nano and now vim, and they are really good choices. There is a version for windows. Here is the link https://nano-editor.org/dist/win32-support/
However more often we need to open the file in question, from the command line as quick as possible, to not loose time. We can use notepad.exe, we can use notepad++, and yea, we can use sublim text. I think there is no greater then a lightweight, Too powerful editor. Sublime text here. for the thing, we just don't want to get out of the command line, or we want to use the command line to be fast. and yea. We can use sublime text for that. it contain a command line that let you quickly open a file in sublime text. Also there is different options arguments you can make use of. Here how you do it.
First you need to know that there is subl.exe. a command line interface for sublim.
1-> first we create a batch file. the content is
@ECHO OFF
"C:\Program Files\Sublime Text 3\subl.exe" %*
We can save that wherever we want. I preferred to create a directory on sublime text installation directory. And saved there the batch file we come to write and create.
(Remark: change the path above fallowing your installation).
2-> we add that folder to the path system environment variable. and that's it.
or from system config (windows 7/8/10)
then:
then:
then we copy the path:
then we add that to the path variable:
too quick!
launch a new cmd and now you've got subl command working well!
to open a file you need just to use subl command as fellow:
subl myfileToOpen.txt
you can also use one of the options arguments (type --help to see them as in the image above).
Also note that you can apply the same method with mostly any editor of your choice.
我不得不对Windows Nano docker 映像进行一些调试,并且需要编辑文件的内容,谁会猜到这非常困难。
我使用Get-Content
和Set-Content
和 base 64 编码/解码的组合来更新文件。例如
编辑 foo.txt
PS C:\app> Set-Content foo.txt "Hello World"
PS C:\app> Get-Content foo.txt
Hello World
PS C:\app> [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String("TXkgbmV3IG11bHRpDQpsaW5lIGRvY3VtZW50DQp3aXRoIGFsbCBraW5kcyBvZiBmdW4gc3R1ZmYNCiFAIyVeJSQmXiYoJiopIUAjIw0KLi4ud29ybGQ=")) | Set-Content foo.txt
PS C:\app> Get-Content foo.txt
My new multi
line document
with all kinds of fun stuff
!@#%^%$&^&(&*)!@##
...world
PS C:\app>
诀窍是将base 64解码的字符串传送到Set-Content
[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String("...")) | Set-Content foo.txt
它没有 vim,但我可以更新文件,因为它的价值。
我是一名退休的工程师,在 60 年代与 DOS、Fortran、IBM360 等一起长大,就像这个博客上的其他人一样,我非常怀念在 64 位 Windows 中失去命令行编辑器。在花了一周的时间浏览互联网和测试编辑器之后,我想分享我的最佳解决方案:Notepad++。这与 DOS EDIT 相去甚远,但也有一些附带的好处。不幸的是,它是一个屏幕编辑器,需要鼠标,因此速度很慢。另一方面,它是一个不错的 Fortran 源代码编辑器,并且显示了行号和列号。它可以为正在编辑的文件保留多个选项卡,甚至可以记住光标的最后位置。我当然会继续输入键盘代码(50 年的习惯),但令人惊讶的是,至少其中一些是有效的。也许不是记录在案的功能。我将编辑器重命名为 EDIT.EXE,设置它的路径,然后从命令行调用它。这还不错。我和它一起生活。顺便说一句,请注意不要在 Fortran 源代码中使用 tab 键。在文本中放置一个 ASCII 6。它是不可见的,至少 gFortran 无法处理它。Notepad++ 可能有很多我没有时间去弄乱的功能。
您可以执行以下操作:
bash -c "nano index.html"
上面的命令使用 Powershell 中的 nano 编辑器打开 index.html 文件。
或者,您可以通过以下命令使用 vim 编辑器
bash -c "vi index.html"
好吧,在 Windows 7 上编辑文本文件有上千种方法。通常人们安装 Sublime、Atom 和 Notepad++ 作为编辑器。对于命令行,我认为基本编辑命令(顺便说一句,在 64 位计算机上不起作用)很好;或者,我发现类型 con > filename 是一种非常值得称赞的方法。如果新安装了 windows 并且想避免使用记事本. 这可能是它! Type 作为编辑器的完美使用:)
图片参考:- https://www.codeproject.com/Articles/34280/How-to-Write-Applet-Code
如果您有适用于 linux (wsl) 的 windows 子系统,您会发现以下命令非常有用:
bash -c "vi filename.txt"