Edit a text file on the console using Powershell - text-editor

I'm trying to figure out the easiest way to edit text files in the console (PowerShell in my case). I'm using Windows 7 64 bit. It galls me that I can't just type edit filename.txt to edit a file. That used to work, but that's all changed. What are my options to view and edit text files within the windows console, and if you tell me to install and learn VIM I'm going to punch you in the face. :-)

Why not use notepad?
notepad.exe filename.txt
The old edit.com works in PowerShell (at least on my box: Windows 7 Pro x86) but in x64 it doesn't work due to its 16bit architecture.
You can take a look at this easy editor.

Kinesics Text Editor.
It's super fast and handles large text files, though minimal in features. There's a GUI version and console version (k.exe) included. Should work the same on linux.
Example: In my test it took 7 seconds to open a 500mb disk image.

While risking you punching me, I guess you are stuck with the solution you mentioned. Have a look at this posting on SuperUser:
Which are the non-x text editors in Powershell?
Also, there is a nano version for windows:
Nano Editor
I'll duck and cover now, hopefully someone will have a more sufficient answer.

Bit of a resurrect but for anyone else coming to this question, take a look at the Micro editor. It's a small standalone EXE with no dependencies and with native Windows 32\64 versions. Works well in both PowerShell and CMD.EXE.

I agree with Sven Plath. Nano is a great alternative. If you have Chocolatey or scoop, you can install nano by typing the following in Powershell:
PS C:\dev\> choco install nano
# --OR--
PS C:\dev\> scoop install nano
Then, to edit somefile.txt enter:
PS C:\dev\> nano somefile.txt
It's pretty neat!
Edit:
Nano works well on my Windows 10 box but takes incredibly long to load the first time on my Windows 7 machine. That made me switch to vim (vi) on my Win 7 laptop
PS C:\dev\> choco install vim
PS C:\dev\> vim $profile
Add a line in the powershell profile to Set-Alias (sal)
sal vi vim
Esc - : - x - Enter :-)

If you use Windows container and you want change any file, you can get and use Vim in Powershell console easily.
To shelled to the Windows Docker container with PowerShell:
docker exec -it <name> powershell
First get Chocolatey package manager
Invoke-WebRequest https://chocolatey.org/install.ps1 -UseBasicParsing | Invoke-Expression;
Install Vim
choco install vim
Refresh ENVIRONMENTAL VARIABLE
You can just exit and shell back to the container
Go to file location and Vim it vim file.txt

You could install Far Manager (a great OFM, by the way) and call its editor like that:
Far /e filename.txt

You can install nano in powershell via choco - It's a low friction way to get text editing capabilities into powershell:
Install Choco
Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
Install Nano
choco install nano
Profit
nano myfile.txt
Best part is it becomes part of the path, and stays working across reboots etc :)

install vim from online, and then you can just do:
vim "filename" to edit that file

I'm thinking you could just use notepad, like this:
notepad myfile.extension
It should open in notepad.

I am a retired engineer who grew up with DOS, Fortran, IBM360, etc. in the 60's and like others on this blog I sorely miss the loss of a command line editor in 64-bit Windows. After spending a week browsing the internet and testing editors, I wanted to share my best solution: Notepad++. It's a far cry from DOS EDIT, but there are some side benefits. It is unfortunately a screen editor, requires a mouse, and is consequently slow. On the other hand it is a decent Fortran source editor and has row and column numbers displayed. It can keep multiple tabs for files being edited and even remembers where the cursor was last. I of course keep typing keyboard codes (50 years of habit) but surprisingly at least some of them work. Maybe not a documented feature. I renamed the editor to EDIT.EXE, set up a path to it, and invoke it from command line. It's not too bad. I'm living with it. BTW be careful not to use the tab key in Fortran source. Puts an ASCII 6 in the text. It's invisible and gFortran, at least, can't deal with it. Notepad++ probably has a lot of features that I don't have time to mess with.

Not sure if this will benefit anybody, but if you are using Azure CloudShell PowerShell you can just type:
code file.txt
And Visual Studio code will popup with the file to be edit, pretty great.

Well there are thousand ways to edit a Text file on windows 7.
Usually people Install Sublime , Atom and Notepad++ as an editor.
For command line , I think the Basic Edit command (by the way which does not work on 64 bit computers) is good;Alternatively I find type con > filename as a very Applaudable method.If windows is newly installed and One wants to avoid Notepad. This might be it!!
The perfect usage of Type as an editor :)
reference of the Image:- https://www.codeproject.com/Articles/34280/How-to-Write-Applet-Code

I had to do some debugging on a Windows Nano docker image and needed to edit the content of a file, who would have guessed it was so difficult.
I used a combination of Get-Content and Set-Content and base 64 encoding/decoding to update files. For instance
Editing 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>
The trick is piping the base 64 decoded string to Set-Content
[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String("...")) | Set-Content foo.txt
Its no vim but I can update files, for what its worth.

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.

You can do the following:
bash -c "nano index.html"
The command above opens the index.html file with the nano editor within Powershell.
Alternatively, you can use the vim editor with the following command
bash -c "vi index.html"

If you have windows subsystem for linux (wsl), you will find the following command very useful:
bash -c "vi filename.txt"

Sadly powershell doesn't come with a built in console-text editor.
You can redirect standard input for simple oneliners like so:
# write text and overwrite the file with that text
"my text that will appear in the file" > file.txt
For anything more complicated you will need a package manager Chocolatey
Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
and install any of the following editors:
Nano
choco install nano
Issues
Very laggy on windows
Problems with arrow control
Vim
choco install vim
Issues
No major issues just remember esc + :!qa will exit vim

Related

open vim file in new unix terminal

How to open existing vim file from unix shell (bash) in new terminal (not in same/new tab of existing terminal) on local machine ?
Also is there any way to split file on new terminal (not in same/new tab of existing terminal) from inside vim ?
How to open a new terminal is platform dependent; This doesn't really have a whole lot to do with vim itself.
For example, if you're using GNOME you could do this by running gnome-terminal -e "vim $filename" & disown. Look up the documentation for the terminal emulator you're using to find out how to launch a new terminal and execute commands in it.
Another (IMHO much better) solution is to simply use GVim for situations like these, unless you have a very good reason to run vim in the terminal (if you're running this over ssh this won't work anyway, in that case you're better off using a terminal multiplexer like screen or tmux).
PS: bash isn't a terminal (emulator); bash is a shell. If you just run a new instance of bash it'll run in the same terminal, which is not what you want here.
Try this:
vim [your file]
If this isn't working for you, make sure you have it installed with:
sudo apt-get install vim
If you're already IN vim do
:edit [your file]

Can linux or mac terminal commands scroll up instead of down?

I'm wondering if there is a way set a linux or mac terminal so that you are always entering commands at the top of the terminal screen, and previous commands are below it.
Example
-----Screen top----
>EnterCommand
>PreviousCommand 1
>final line of Previous Command 1 output
>second to final line of previous command 1 output
-----Bottom of screen-----
Thanks!
Such a feature is not built into the standard *nix terminal driver; a terminal application would have to be specifically written to translate newlines and line wrapping into upward motion instead of downward. Note that this will likely break a number of TUI applications, so it is not recommended to use them with such a terminal application.
This seems pretty close to what you're asking for:
https://github.com/swirepe/alwaysontop
To use it run:
git clone https://github.com/swirepe/alwaysontop.git
cd alwaysontop/
source alwaysontop.sh
If you decide you like it just source it in your .bashrc or .bash_profile with something like:
echo "source ~/alwaysontop/alwaysontop.sh" >> ~/.bashrc
Hope that helps!

Linux command to DOS

I have a file include some linux command and I want to run in on windows (DOS command).
The command is:
cat tmp/$id/index.html | sed -e 's/ID/$id/g' > a;mv a tmp/$id/index.html
What is the similar command in MS-DOS?
Thank you!
The problem is that natively there is no equivalent command to sed. You have two options from my point of view. Either create a vb script that does what you want (It will not take 1 line though - more like 10-15 I guess), or use something like GnuWin32 that gives you the option to run unix commands in windows terminal.
You could consider using powershell to do approximately the same thing. It supports cat and mv and you can get a sed like equivalent by using %{_ -replace "expression", "replace"}. Details here http://blogs.msdn.com/b/zainnab/archive/2007/07/09/grep-and-sed-with-powershell.aspx
Or consider using a linux like command prompt like bash which should be available through cygwin
I think this is impossible to do in "bare" command line (as you called DOS command), because cat and sed are separate utilities. If you want to port this script from Linux command shell to windows command line, I would advise you to download and install CygWin
DOS itself does not have support for that. You could try with a port of SED for DOS available here. If you can get Powershell, that's an option. Here's an example of using grep/sed with Powershell.
There are many options.
You can try to install cygwin or download and install Git and use Git-bash or add the bin directory to your PATH so you can run this command on your CMD prompt.
There is no such command(s) for MS-DOS.

Is it possible to "extend" IntelliJ such that I can open the current file in VIM

I use IntelliJ for most of my Java programming, but every once in a while I need to make certain changes which are much easier done in VIM.
Thus, the question:
While I'm editing a file in IntelliJ, it is somehow possible to tell IntelliJ to start VIM with the same file that is currently under cursor.
Thanks !!
Absolutely!
Go to File -> Settings -> External Tools and add a new one.
Program and Working Directory fields should be self-explanatory. The parameter field should be $FilePath$. Click the Insert macro button to see more token values that you could use.
You can also bind keyboard shortcuts to the external tools you define.
See the help page on configuring third party tools for more info.
Also, see the answers below with helpful screenshots for Windows or Mac.
Even though the original question is about VIM, I'd like to share my settings for GVIM. Especially useful is the parameter used to take it to the current line.
I find it useful to have Gvim open up the file AND take me to the current line. This is done using the following parameters: +$LineNumber$ $FilePath$.
When executed, Gvim should open up and take you to the line you were at in Intellij.
Here's how to set this up with the Homebrew version of MacVim (i.e. the mvim command) on Intellij IDEA 14.
Go to Preferences->Tools->External Tools and click the plus symbol.
Set up the new tool with these parameters
Program:
/usr/local/bin/mvim
Parameters:
+"silent! "$LineNumber$"|norm! zz" $FilePath$
Working Directory:
$SourcepathEntry$
For macOS 10.12+ users, If you want to open it in iTerm2, here is a latest solution:
#!/bin/sh
vim="vim $1"
osascript -e "
tell application \"iTerm\"
set myterm to (create window with default profile)
tell myterm
activate current session
launch session \"Default Session\"
tell the current session
write text \"$vim\"
end tell
end tell
end tell
"
Date: Apr 5, 2018
iTerm2: 3.1.5
IntelliJ IDEA: 2018.1
https://gist.github.com/backslash112/open_vim_in_iterm.sh
This was somewhat more involved on the Mac OS X (Lion), especially if you'd like to use Terminal.app
The only difference on Linux is the shell script would be much simpler... (really it'd be simple to not even use a shell script for this on Linux, with a terminal that accepts the -e argument!)
Here's my working solution:
#! /bin/sh
vim="vim $1"
osascript -e "tell app \"Terminal\"
activate
do script \"$vim\"
end tell"
I am using these parametrs for launching vim: +$LineNumber$ --remote-tab-silent "$FilePath$". It allows you to open each new file in vim's tabs. Works fine)
Go to Settings -> Tools -> External Tools
Click the + to add a tool.
Here are the settings I use:
Name: Vim
Program: /usr/bin/gvim
Arguments: --servername $ProjectFileDir$ --remote "+call cursor($LineNumber$,$ColumnNumber$)" $FileDir$/$FileName$
Working directory: $ProjectFileDir$
I turn off "Open console for tool output".
The arguments will use Vim in client-server mode, so if you jump to Vim from IntelliJ in the same project dir multiple times, the existing Vim instance will be reused, and they will also place the cursor in Vim exactly where it is in IntelliJ.
Try out the VIM Plugin, http://plugins.intellij.net/plugin/?id=164
I have experienced some bugs but for the most part it works.

getting vi and rxvt to work in cygwin

I am trying to use rxvt on my cygwin w win XP but the terminal appears and disappears. What could be wrong? This is true for all except rxvt-native . I have tried a few commands found online but with no success. I include 2 I have tried:
start C:\cygwin\bin\rxvt.exe -sb -sl 3000 -fg gray -bg black -fn "Lucida Console-14" -e /bin/bash --login -i
.
path C:\cygwin\bin;%path%
ssh-agent rxvt -e bash --login -i
Another problem I am facing is trying to get vi to work in my cygwin bash shell. Setting term to xterm or vt100 does not work. Hitting enter, I see a string 78 or some other issue pops up. I have never modified my .inputrc.
My main issue was trying to get vi to work properly. I just found out that if I run
/etc/postinstall/terminfo.sh.done , I can now navigate properly in vi. The 78 (newline) M still appears but at least I can navigate in vi.
Feb 25 - This problem went away after a few windows updates. Would it make sense to ANSWER my question on the basis that this is longer reproducible?
Can't answer the first question, but have you tried invoking rxvt from its shortcut in the Cygwin folder of the start menu?
Regarding the second question, the TERM variable tells applications what terminal they're running in, so if you set it to 'xterm' while running in the Cygwin console (where normally TERM=cygwin), they'll be sending xterm control sequences that the Cygwin console doesn't understand. So basically: don't do that!
Btw, you might also be interested in Cygwin's mintty package, which is another terminal that doesn't need an X server. Installing it also creates a start menu shortcut in the Cygwin folder.
I would assume you need an X server running.
You could install Cygwin/X
One thing which may not have been clear was that the whole exercise was to get vi to work. vim did not work either at that time. But after some windows update, the problem went away. Closing it...

Resources