How do I make a command line text editor? - python

I have gotten to know my way around a few programming languages, and I'd like to try my hand at making a command-line text editor -- something that runs in the terminal, like vim/emacs/nano, but is pure text (no guis, please). Preferably, I'd like to do this in python. Where do I start? Are there any (python) libraries to do command-line applications?

try python curses module , it is a command-line graphic operation library.

Take a look at Curses Programming in Python and this as well.

Another option if you want to write a TUI (Text User Interface) without having to descend to curses is Snack, which comes with Newt.

Kids today! Sheesh! When I was starting out, curses was not in widespread use!
My first text editors worked on actual mechanical Teletype devices with actual paper (not a philosophical "TTY" device with a scrolling screen!)
This still works nicely as a way to edit.
Use the cmd module to implement a bunch of commands. Use the 'ex' man page for hints as to what you need. Do not read about the vi commands; avoid reading about vim.
Look at older man pages for just the "EX COMMANDS" section. For example, here: http://www.manpagez.com/man/1/ex/.
Implement the append, add, change, delete, global, insert, join, list, move, print, quit, substitute and write commands and you'll be happy.

Curses type libraries and resources will get you into the textual user interfaces, and provide very nice, relatively easy to use windows, menus, editors, etc.
Then you'll want to look into code highlighting modules for python.
It's a fun process dealing with the limitations of textual interfaces, and you can learn a lot by going down this road. Good luck!
-Adam

I would recommend the excellent urwid toolkit (http://excess.org/article/2009/03/urwid-0984-released) - it's much easier to use than straight curses.

Well, what do you mean by a GUI? If you just want to create something that can be used on a console, look into the curses module in the Python standard library, which allows you to simulate a primitive GUI of sorts on a console.

Not quite a reference to a Python library, but The Craft of Text Editing by Craig A. Finseth might be of interest you.

A not very serious suggestions: a line editor can be implemented without curses.
These things are pretty primitive, of course, and not a lot of fun to work in. But they can be implemented with very little code, and would give you a chance to fool around with various schemes for maintaining the file state in memory pretty quickly.
And they would put you in touch with the programmers of the early seventies (when they had teletypes and the first glass teletypes, but after punched cards were a bit passe...).

Another option without curses is Python Slang
Newt is a library written on top of Slang.

Related

How are complex applications made with Python in an IDE with raw code?

The question may not have been clear as there was not much of a better way for me to word it but I will do my best here: Most scripting is done raw in the IDE. When coding with Python it simply runs everything through the "console." I was wondering how full-on apps with different screens and all that cool stuff are made with Python as well, other than just being a console with many different commands?
Basically if you want to design cool stuff with colours and nice picture, you can use turtle. Turtle is a cool library which allows you to draw turtles and other cool animations. Useful for beginners like you :)
If you're talking about gui (graphical user interface) applications tkinter module is what you're looking for allowing you to create simple gui applications.
Here is a link to the documentation
https://docs.python.org/3/library/tkinter.html
should be included in python by default.

How to Run Python without a Terminal

I want to teach some students basic Python programming without having to teach them how to use the terminal. Recently, I was teaching a 2 hour intro session, and not only did teaching terminal stuff take a long time, but it also intimidated a lot of the students. I would like a solution where students wouldn't have to leave graphical user interfaces that they were comfortable with.
I also want the solution to let them execute a particular Python file (eg, not just using the interactive Python interpreter) and see the output from printing things.
Thanks!
Surely that's what IDLE is for? It's not much good as an IDE, but it does work well for exactly what you describe - opening modules and executing them, and running commands in an interactive shell.
How about this?
https://www.wakari.io/
All in the browser.
Bonus points, you introduce them to IPython as well.
Take a look at the gallery too, https://www.wakari.io/gallery
You might want to try something like this: http://repl.it/languages/python
Perhaps you want something like this:
http://learn.adafruit.com/webide/overview
When I'm not near my own PC, I use ideone.com. I like that it is a universal IDE, which for me means both C++ and Python.

What's the closest thing to SLIME for python ? / What's the best way to use python from EMACS?

I'm trying to write some python, and I'm used to the lispy way of doing things, a REPL in EMACS and the ability to send arbitrary code snippets to the REPL. I like this way of developing code, and python's built-in IDLE seems to do it pretty well. However I do like EMACS as an editor.
What's the best thing analogous to SLIME for Python?
So far:
It seems that the trick is to open a python file, and then to use 'Start Interpreter' from the Python menu, after which you get an Inferior Python buffer. You can then use C-c C-c to load the whole buffer you're editing into the 'REPL', and use normal copy and paste to put snippets into the REPL.
This works as far as it goes. Is there any way to say 'reevaluate the big thing that the cursor is in now and display the answer', or 'reevaluate the thing the cursor is just at the end of and display the answer', like M-C-x and C-x-e in SLIME?
And it all seems to work better if you use the python-mode.el from Bozhidar's answer
There is ipython.el which you can use for some extended functionality over the vanilla python-mode. Ropemacs provides a few extra completion and refactoring related options that might help you. This is discussed here. However, I don't expect you're going to get anything close to SLIME.
I think the new python.el is a much better idea. It's under active development, it can spawn a python shell and send function definitions, buffers and files to it. It also has better than average re-indent support. It's rumoured that in Emacs 24 it might become the default python mode in Emacs.
python-mode is the default mode for editing Python in emacs. It includes commands for evaluating the buffer and running an inferior interpreter instance.

Lightweight console/IDE for Python?

I often use ipython (or the regular python shell) to test python code snippets while coding, and it's been very useful. One shortcoming of this, though, is that if I want to test a multi-line segment of code, or want to write multiple lines of code before running, it isn't very convenient to have to do it "line by line". And even going back to change some of the lines is cumbersome because you have to re-type all the code that comes after it.
I'm playing with Groovy right now and I find that it has an excellent solution to this problem: the Groovy Console. You just write all the code you want, it's just like a regular editor; and then you hit run Ctrl+R (Cmd+R actually since I'm on a Mac) and it runs everything at once. If you want to change something (e.g. if there are errors), then that's easy too -- just change it and Ctrl+R again.
Is there an equivalent of this available for python? Or do you have any recommendations on a way to achieve similar behavior? I could just create a new file, save it, and then python <filename>.py from the shell. But that's just too many steps and would be cumbersome. Eclipse may be an option, but it's too heavyweight. I'm really looking for something lightweight that I can just spin up when I want to test something and then get rid of it just as quickly.
I'd be interested to hear any ideas/suggestions!
Thanks
You might give DreamPie a try. As far as I can tell from a quick read of the groovyConsole page you linked to, DreamPie features a similar input area/output area division (they call it "code box" and "history box"). The code you execute is by default cleared from the code box - which groovyConsole apparently doesn't do - but you can easily retrieve it (Ctrl+Up), or change a preference setting to "Leave code in the code box after execution".
Have you tried using IDLE, the standard Python IDE? You'd have to save the code as <filename>.py within IDLE, but after that you can run it using F5.
The Python docs link to this intro to IDLE, which may be helpful even if it's a little outdated.
I am using emacs and its python-mode.
C-c C-c: evals the current buffer
but you can also eval region (ie selection), functions etc ...
You can even make python-mode use ipython (like I do).
See http://ipython.scipy.org/dist/ipython.el . It works nicely
Did you try PyCrust? It has excellent multi-line editing, copy/paste support.
PyCrust can be found in wxPython Docs and Demos.

What are some useful TextMate features? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I noticed that many people here use TextMate for coding on OS X. I've recently started using it, and although I like its minimalistic interface, it makes it harder to stumble upon cool features if you don't know what you're looking for.
So, what feature have you found most helpful for coding (mainly in Python)? Are there any third-party bundles I should know about, besides what's included?
Don't neglect the 'mate' command line tool. You can use it to pipe output into TextMate, so if you do the following...
diff file1.py file2.py | mate
...it will not only open in TextMate, but it is smart enough to know that you're looking at a diff and highlight lines on screen.
TextMate's SVN integration is great; it also seems to have bundles for some other version control systems as well.
Add GetBundle to browse the bundle repository. I found the jQuery bundle through it and it's very handy.
As others have mentioned, rolling your own bundle for frequently used snippets is very helpful. If you have some snippets that are specific to a project or framework, you might want to prefix all of them with a common letter to keep the namespace tidy.
Holding down option while dragging allows you to highlight a block of text. If you type while the highlight is active, your keystrokes appear on multiple lines.
Being able to write simple commands in any scripting language and bind them to a context-specific hotkey.
The Navigation menu commands Go to File (Command + T) and Go to Symbol (Command + Shift + T) are both extremely helpful.
Go to File, which works when you have a project open, lets you type any part of the file name to see only files that match what you've typed.
Go to Symbol has the same type-to-filter interface, but operates on what I'd call the basic block elements of your document. For example, if you're editing a class, Go to Symbol works on the method names, but in a CSS document, you'll be searching on your selectors. It's pretty awesome.
I mention some in a review on Boagworld, I find the snippets, project manager, columnar editing (hold down option while selecting stuff or push it after having selected stuff) and CSS scopes for syntax.
I like the integrated HTML/XML Tidy. Cmd-shift-H is your friend.
Also, nice integration with a variety of scp/sftp clients.
My favourite two features are auto-completion (bound to ⎋ [esc]), and column editing (bound to ⌥ [alt]) both of these things save me quite a lot of time, and are definitely 'robot ninjas'.
The book linked above is also a really useful into to the power of TextMate, although it doesn't specifically mention python.
Don't forget "Drag commands".
They give you the ability to drag, say, an image into a blog.html document and will then upload it to the proper folder and insert the markup for you.
Here is another example of how you can expand further on drag commands if you pair TM up with QuickSilver.
(Disclaimer: I wrote the blog post I linked to there. I still think it's cool though.)
It is worth noting here that there is a Windows alternative to TextMate called E Text Editor. It does pretty much everything TextMate does (apart from macros, but the author is working on this, I think), and even - shock, horror - does some things better, such as the superb bundles editor, the bundles manager, and the branching undo history. Update: and now there's Snippet Pipes.
So, not exactly a useful feature of TextMate as such, but very useful to know if you're a fan of TextMate and you have to use Windows for whatever reason.
The ease of snippet creation.
It's trivial to create new snippets that can accomplish a lot using replacements, tabbing order, and regex substitutions. Quickly assigning these to the tab key for specific languages makes me more productive. And makes me worry about code bloat. :-)
For me the best features are:
Projects - I know every IDE under
the sun has this but TextMate makes
this useful for all sorts of editing
and text processing tasks, and
moreover makes navigating around
these projects easy without ever
lifting your hands from the
keyboard. This is huge for Rails or
Grails projects or large programming
projects with many modules.
The excellent syntax highlighting
and 'snippets' for myriad languages
and tools
The excellent scripting language
support (Being able to evaluate
chunks of Ruby and the like with a
single key chord)
The built in Blogging bundle is
superb. I now use TextMate
exclusively for all my blog posts.
Columnar editing
The ability to use just about any
language or tool to extend TextMate,
Ruby, Perl, shell, name your poison.
An excellent mix of great Aqua GUI
support and excellent command line
support through the
mate and
commands, for
instance making it easy and pleasant
to use TextMate as your default
editor for your SCM.
Using snippets to expand into large, repetitive blocks of code and then using the tab key to move through and only edit the pieces I need to without having to use the mouse or arrow keys.
It's nice and lightweight and has all of the macros built-in for Ruby and let's you run Ruby code, or any other code for that matter just with a keystroke.
Check out ProjectPlus, it gives some useful options for the sidebar, it has SCM status badges for svn and git (though I find the git thing a bit buggy).
I like the fact that it can change the sidebar to an embedded panel on left or right (as opposed to the drawer that's default).
If, like me, you're borderline OCD when it comes to making code look neat, then Option+Cmd+] to line up all the assignments around the current line is awesome!
The mate command line tool is great, you can open an individual file or my favourite use of it is to open a directory of files as a project (e.g. mate .)
Checkout Zen Coding bundle . It gives you an awesome productivity boost to developing both HTML and CSS.

Categories