Showing posts with label text-editors. Show all posts
Showing posts with label text-editors. Show all posts

Monday, August 20, 2018

Nice Vim trick for Python code

By Vasudev Ram

Here's a Vim editing trick which can be useful for indenting Python code (in some situations):

Let's say I have this program, p1bad.py:
$ type p1bad.py

def foo(args):
print "in foo"
def bar():
print "in bar"
Running it gives:

$ python p1bad.py
File "p1bad.py", line 3
print "in foo"
^
IndentationError: expected an indented block
The indentation is incorrect; the first print statement should be indented one level under the "def foo" line, and the second one should be similarly indented under the "def bar" line.

Imagine that the incorrect indentation was due to fast typing, or was done by a beginner. We can fix this by opening the file in vim and typing this vim command (with the cursor being anywhere in the file):

    gg=G

How does it work?

The gg moves the cursor to the first line of the file.

Then, the = means indent some text. What text? The text that would have been moved over by the following cursor movement. And the next movement command is G (which is short for $G). That means move to the last line of the file, since $ means the last line, and G means move to a specified line (in this context).

So the net result is that the part of the file that would have been moved over (in the absence of the = part of the command), gets indented instead. In this case, it is the whole file, since we were at the top and said to move to the bottom.

Then I save the file as p1good.py. The result is:
$ type p1good.py

def foo(args):
    print "in foo"
def bar():
    print "in bar"
We can see that the code is now correctly indented.

Another example, p2bad.py:
$ type p2bad.py

def foo(args):
print "in foo"
    def bar():
    print "in bar"
Here, the user has again not indented the two print statements correctly. Note that in this case, function bar is nested under function foo (which is okay, since nested functions are allowed in Python).

Running p2bad.py gives:
$ python p2bad.py
File "p2bad.py", line 3
print "in foo"
^
IndentationError: expected an indented block
We get the same error.

If we now type the same command, gg=G, and save the file as p2good.py, we get:
$ type t2good.py

def foo(args):
    print "in foo"
    def bar():
        print "in bar"
Again, the indentation has been corrected.

If we were already at the start of the file, we could just type:

    =G

Note that this technique may not work in all cases. For example, in the case of p2bad.py above, the user may not have meant to nest function bar under function foo. They may have meant it to be a non-nested function, indented at the same level as foo. And vim may not be able to determine the intention of the user, in this and some other cases.

So use the technique with care, and preferably make a backup copy of your file before changing it with this technique.


If you're new to vi/vim, and want to learn its basics fast, check out my vi quickstart tutorial on Gumroad. It's short, so you can read it and get going with vi/vim in half an hour or less.

- Enjoy.

- Vasudev Ram - Online Python training and consulting

Get updates (via Gumroad) on my forthcoming apps and content.

Jump to posts: Python * DLang * xtopdf

Subscribe to my blog by email

My ActiveState Code recipes

Follow me on: LinkedIn * Twitter

Are you a blogger with some traffic? Get Convertkit:

Email marketing for professional bloggers



Sunday, April 26, 2015

pyvim, an experimental pure Python vim clone

By Vasudev Ram




Saw this via Twitter:

pyvim, a pure Python vim clone, by Jonathan Slenders from Belgium.

It's new and experimental, so will not have a lot of features, and may have some bugs.

I tried it out briefly after installing it with:
pip install pyvim
(That's all there is to it. It's location even got added to my PATH.)

Here is a screenshot of me editing a file in pyvim:


And here is a screenshot of running pyvim without specifying a filename, i.e. editing a new file (same image as the one at the top of this post, except that the one at the top links to the pyvim site on Github):


The really interesting thing about the editor being written in Python, is that, as they say, it may later be possible to script the editor in Python:

"Further, when the project develops, it should also become possible to write extensions in Python, and use Python as a scripting language. (Instead of vimscript, for instance.)"

Speaking of vim, if you are new to vim or vi (vim's predecessor), you may like to check out my vi quickstart tutorial, first published in Linux For You magazine. I first wrote the tutorial at the request of some Windows sysadmin friends who were transitioning to Linux, and they told me that it helped them to come up to speed with vi quickly, for basic text editing tasks. I then sent it to Linux For You as an article proposal and they published it.

- Vasudev Ram - Online Python training and programming

Dancing Bison Enterprises

Signup to hear about new products or services that I create.

Posts about Python  Posts about xtopdf

Contact Page

Monday, October 20, 2014

Published my first presentation on SpeakerDeck - using Python

By Vasudev Ram



SpeakerDeck is an online presentation service roughly like SlideShare. SpeakerDeck seems to have been created by Github Inc.

I just published my first presentation on SpeakerDeck. It is a quickstart tutorial for the vi editor. Note: vi, not vim. I had written the tutorial some years ago, when vim was not so widely used, and vi was the most common text editor on Unix systems.

About the tutorial:

I first wrote this vi quickstart tutorial for some friends at a company where I worked. They were Windows and network system administrators without prior Unix experience, and had been tasked with managing some Unix servers that the company had bought for client work. Since I had a Unix background, they asked me to create a quick tutorial on vi for them, which I did.

Later on, after learning the basics of vi from it, and spending some days using vi to edit Unix configuration files, write small shell scripts, etc., they told me that they had found the tutorial useful in getting up to speed on vi quickly.

So, some time later, I thought of publishing it, and sent an article proposal to Linux For You magazine (an Indian print magazine about Linux and open source software). The proposal was accepted and the article was published.

About generating the tutorial as PDF and uploading it to SpeakerDeck:

The original vi quickstart tutorial was in text format. Last year I wrote XMLtoPDFBook (as an application of xtopdf, my Python toolkit for PDF creation), which allows the user to create simple PDF e-books from XML files. So I converted the vi tutorial to XML format (*) and used it to test XMLtoPDFBook. I therefore had the tutorial available in PDF format.

(*) All you have to do for that - i.e. to convert a text file to the XML format supported by XMLtoPDFBook - is to insert each chapter's text as a <chapter> element in the XML file. Then give the XML file as the input to XMLtoPDFBook, and you're done.

SpeakerDeck requires that presentations be uploaded in PDF format. It then converts them to slides. So I thought it would be a good test of SpeakerDeck and/or xtopdf, to upload this PDF generated by xtopdf to SpeakerDeck, and see how the result turned out. I did that today. Then I viewed the resulting SpeakerDeck presentation. It was good to see that the conversion turned out well, AFAICT. All pages seem to have got converted correctly into slides.

The presentation can be viewed here:

A vi quickstart tutorial

If you prefer plain text to presentations, you can read the vi quickstart tutorial here.

- Vasudev Ram - Dancing Bison Enterprises

Click here to signup for email notifications about new products and services from Vasudev Ram.

Contact Page

Saturday, January 18, 2014

Dark Room for Windows, a text editor like WriteRoom for Mac

By Vasudev Ram

Dark Room is a text editor for Windows that is like the text editor called WriteRoom for Mac computers.

I had come across either one of them a while ago, and saw Dark Room today. Trying it out.

Here is a screenshot of a test file I am editing with Dark Room:


Though I'm editing the file without the editor menu showing, you can use the menu option keys, and you can also toggle the display of the menu by pressing the Escape key repeatedly.

One of the main advantages of both is what they call a "distraction-free writing environment", There are very few or no controls on the sides or top or bottom of the writing area, which makes for less distractions. Also, Dark Room has a default of green text on a black background, which is like the old fashioned Unix dumb terminals or IBM mainframe terminals. It can be a good color combination for both writing and reading (YMMV, of course).

- Vasudev Ram - Dancing Bison Enterprises

Contact Page


O'Reilly 50% Ebook Deal of the Day


Wednesday, March 27, 2013

Metapad is my text editor of choice for quick edits in Windows

By Vasudev Ram



Metapad is a great lightweight replacement for Windows Notepad.

Metapad is my preferred editor for light and quick editing tasks when on Windows. I even use it to read and edit programs sometimes.

There are, of course, more powerful editors for Windows, but Metapad is great for quick work, without having to set up a lot of options in the editor. When I am on a new Windows machine, I typically just download it, and only change the font to Courier New of a reasonable size, depending on the monitor resolution, and set tabs to 4 spaces, and I'm ready to go :)

I've been using Metapad for many years now.

Update: I should mention that Metapad does have many useful editing options that you can set, it's just that I don't use them most of the time.

But overall, it's a nice Windows software tool.

For bigger editing tasks, I use Vim / GVim, on Linux and Windows; it is one of the most powerful editors around, and I've been using it for years, and vi (the predecessor of vim), earlier.

Check out my vi quickstart tutorial if you are new to vi / Vim. It was published in Linux For You magazine, in 2003 or so. I first wrote it to help some colleagues who were familiar with Windows but not Unix, and who needed to be able to edit files on Unix. They told me that it helped them to quickly start using vi for basic editing tasks.

Interestingly, I just checked the Metapad side now, and came across these two items of interest:

Metapad turns 10

Metapad is now open sourced

- Vasudev Ram - Dancing Bison Enterprises

Saturday, October 6, 2012

Pallavi, extensible plugin-based text editor written in Python and wxPython

By Vasudev Ram


Pallavi, extensible plugin-based text editor written in Python and wxPython.

- Vasudev Ram - Dancing Bison Enterprises


7 editing tips from vim creator Bram Moolenaar

Vim: Seven habits of effective text editing

Useful general editing tips in the post.

If you are new to vi/vim, read my vi quickstart tutorial first.

- Vasudev Ram

Wednesday, July 25, 2012

Vim's 20th anniversary

By Vasudev Ram


Nice one about the vim text editor, seen on Ars Technica:

Two decades of productivity: Vim's 20th anniversary

If you are a beginner to vim, you may find this vi quickstart tutorial useful:

A vi quickstart tutorial

I wrote it some time ago for Linux For You magazine.

(vi is the predecessor to vim, but all the tutorial content applies equally to vim). Actually, I first wrote it for some colleagues of mine who were coming from Windows to Unix, and later thought of publishing it, because they told me that it helped them to come up to speed on the basics of vi in a short time.

Enjoy.

- Vasudev Ram - Dancing Bison Enterprises