How to edit a py file from terminal? - command-line

I am working in a VM via PuTTY.
Via terminal, I want to open, edit, and save a .py file. How can I do it?
Thank you for your help.

The easiest way is to use vim
vim your_script.py
Edit your file and save it using :w or :x
You can also use emacs or nano

You also have to enter a command like i to get into insert mode. Then hit esc and :wq to save and quit. If you are using terminal often it may be helpful to have a cheat sheet

Either do:
python3 -i pythonfile.py
At which you'll be entering the python editor after closing the program, or
Use a text editor like nano (since it's installed by default with most operating systems), or emacs, which also is a great terminal text editor.
nano pythonfile.py
emacs pythonfile.py -nw
(-nw is a non-gui mode)

You can simply use an editor such as vi, vim or nano:
$ vi myFile.py

Related

How can I turn off vim in terminal of Mac?

I was trying to setup Mongo DB Community on my Mac(m1).
I installed Home-brew first and followed the install instruction of the Mongo DB.
However I could not run the command brew and I found that it was the problem that I didn't set up the Path in the zshell. So I tried it with my terminal but It just got stuck in this here. I tried to escape it with the :wq, :q! commands and it works. but whenever I restart the terminal it goes right back to this screen not to main screen. How can I solve this problem? plz help :(
(Im trying to learning so I'm not familiar with the terminal and codes stuffs)
You are in this situation because you typed the wrong command in the terminal and I suspect it is because you copy-pasted it instead of typing it out.
Your command is helpfully spelled out in the title bar of the window:
$ vi ~/.zshrcexport PATH=/opt/homebrew/bin:
Judging by the content of the buffer, it is almost certainly truncated and the actual command was probably:
$ vi ~/.zshrcexport PATH=/opt/homebrew/bin:$PATH
So what's wrong with that command? Everything, actually.
For starter, it should have been:
$ vi ~/.zshrc
with ~/.zshrc being your shell's main configuration file.
Everything after that shouldn't be here.
Vim, which is the program providing the vi command, takes one or more filenames as arguments. With that command, you told Vim to open two files:
~/.zshrcexport
PATH=/opt/homebrew/bin:$PATH
the former is the one shown in your screenshot,
the latter is not shown but it is likely to have a rather long name,
neither of those files are supposed to exist.
How to get out of that mess?
Assuming you are in the situation shown by that screenshot, do the following:
Press the esc key to make sure you are in what is called "normal mode" in Vim.
Press the : key to enter "command-line mode".
Type qa!, then press the return or ↩︎ key.
At that point you should be outside of Vim and in your shell. It is time to delete the non-wanted files you created with these two commands, each followed by a press on the return or ↩︎ key:
$ rm ~/.zshrcexport
$ rm PATH=/opt/homebrew/bin:$PATH
Now you should finally be able to edit your shell's configuration file but I recommend you don't do it with the vi command. nano is a much simpler editor that doesn't require as much learning.
Open the configuration file in nano with:
$ nano ~/.zshrc
Move around with your cursor keys until you find the right spot, just like in a regular text editor. The file is probably empty anyway.
Type that "export" line:
export PATH=/opt/homebrew/bin:$PATH
Press control+X to quit, as instructed at the bottom of the screen.
Press the appropriate key when asked if you want to write the file.
Avoid vi or vim in the future until you actually have or want to learn it.
As for why you end up in Vim when you open a new terminal window I have no clue. Maybe another one of your mistakes?

copy/paste from emacs in command line

I run emacs from command line using the command emacs -nw
However when I do this, and I try to copy paste something from, say, my browser to my emacs session, it returns me the error "kill ring is empty".
Can someone please let me know how I can copy/paste this way? Thanks.
Programs run in terminal sessions don't have access to the windowing system clipboard. Use the cut and paste functionality provided by your terminal emulator. For example, in Gnome's terminal program press C-S-v (shift-control-V) to paste.
Alternatively, if you're using X11 you can use the xsel program to access the X selection. For example, this function will paste the current X selection into the current buffer:
(defun paste-from-x ()
(interactive)
(call-process "xsel" nil t))
Does marking the text you want to copy with the mouse and then pressing Shift-Insert in Emacs work for you?
I think this previous answer of mine might help you:
First you need to install xclip
sudo apt-get install xclip
For Emacs 24
M-x package-list-packages
Select
xclip //mine was version 1.3
Detailed info and other operating systems
https://stackoverflow.com/a/14659015/54848

Run latex within Emacs

I used to launch latex within Emacs on a .tex file by C-c C-c.
I just changed my machine, and need to reconfigure all. I have already installed texlive-full, and copied .emacs from the previous machine to the new machine.
When I open a .tex file under Emacs, launch C-c C-c, and then type LaTeX, it opens another buffer, named *tex-shell*, and looks like as so:
Could anyone tell me what happened?
I don't really now what happen, but it seem you want to use auctex:
sudo apt-get install auctex
and relaunch Emacs should solve your problem.

Make emacs run in console mode (-nw) by default

How can I make Emacs run in terminal (or console) mode by default, as if I'd used -nw? And once that's done, how do I force it to run in GUI mode (once) if I need to?
On my Debian testing, alias emacs='emacs -nw' followed by emacs opens Emacs in the terminal. And, in the same session, emacs23-x opens Emacs with the GUI.
In case you decide to use emacs in terminal mode always, install emacs-nox (emacs with no X support).
After installationis still ran by $ emacs so you no need to create aliases again!:)
Its there in repos of Debian Squeeze so must be in Ubuntu also.
This answer suggests the method I found works best for me (was having problems with aliases). Essentially, create an executable script
#!/bin/sh
emacs -nw "$#"
and point $EDITOR to it in your shell rc file.
I ran into this problem and didn't want to set an alias (because I also wanted "emacs -nw" to be my default shell EDITOR but it wouldn't work) so I started grepping around and saw this line in configure
If you are sure you want Emacs compiled without X window support, pass
--without-x to configure.
So, if you want to download and build from source, you can just do
curl http://gnu.mirrors.hoobly.com/emacs/emacs-25.3.tar.xz
tar -xvzf emacs-25.3.tar.xz && cd emacs-25.3
./configure --without-x
make && sudo make install
alias emacs='emacs -nw'
Or
unset DISPLAY

.emacs Edit to Always Start Emacs in Terminal Mode?

I use emacs as my editor-of-choice, and since I'm doing a lot of work in a terminal I always run emacs as
emacs -nw
so that it runs in the terminal instead of in a window.
I'd like to just run emacs and have it know that it should run in a terminal. My question is - how do I edit my .emacs file so that this is the default behavior?
You can't do this in the .emacs file. By the time that file is being parsed, the "chosen" emacs binary is already running.
You can install the emacs-nox package as one commenter suggests, or create an alias in your shell so that "emacs" is always treated as "emacs -nw".
Randy
I'm using a bash alias instead of .emacs to do that.
Add this line to your ~/.bashrc.
alias emacs='emacs -nw'
There is any easy way to solve the problem in general that has nothing to do with emacs at all and will work for any program that can choose between running in the console vs X:
unset DISPLAY
Of course you may not want to put that in your configuration file to be applied globally to all your shell sessions, so if you want it to apply to only emacs, then either call it from the command line like this:
DISPLAY= emacs
note the space!!! if you leave the space out it means you're setting the DISPLAY to emacs instead of setting DISPLAY to nothing... this command is a shorthand for:
DISPLAY=; emacs
So either use the above from the command line(s) or put that in a wrapper script that would look something like this:
#!/bin/bash
unset DISPLAY
exec emacs
I recommend the exec there because it will replace your wrapper script with emacs; to see the difference between the two you can run:
pstree -p
When I was first setting up a "emacs -nw" alias for emacs in windows I got stuck in a situation where I thought tototoshi's explanation hadn't worked. Yet all that was required was a restart of my terminal. Therefore, i think its worth mentioning that in windows (at least) if you are using emacs within the git bash terminal to create the .bashrc file and add "alias emacs='emacs -nw" to it (as tototoshi mentions) you have to close and reopen your terminal for it to work.