Showing posts with label tutorials. Show all posts
Showing posts with label tutorials. 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



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

Thursday, October 9, 2014

The Linux Foundation's new Linux Certification program

By Vasudev Ram


Saw this recently via the newsletter I get from The Linux Foundation

The Linux Foundation is introducing a new Linux certification program. It will be available anywhere, online.

Jim Zemlin, the executive director of the Linux Foundation, has details about it in this blog post:

Linux Growth Demands Bigger Talent Pool

There are two certifications:

Linux Foundation Certified System Administrator (LFCS)

Linux Foundation Certified Engineer (LFCE)

These Linux certifications are likely to be a good value addition to anyone seeking to start or grow a career involving Linux, since they are from the official foundation that is behind Linux - the Linux Foundation, which does a lot of work related to sponsoring Linux development (*), conducting conferences like LinuxCon, etc.

In fact, the Linux Foundation sponsors the work of Linux Torvalds, the founder of Linux - Linus is a Linux Foundation Fellow. See this page about the Linux Fellow Program - Linus's name is at the top of the list of Linux Fellows.
On a related note, if you are into Linux and would like to learn how to write Linux command-line utilities in C, check out this blog post by me on the topic of Developing a Linux command-line utility in C, an article I wrote for IBM developerWorks a while ago. It got many views and a 4-star rating, and some people have told me they used the article (which is a tutorial) as a guide to developing command-line utilities on Linux for production use.


- Vasudev Ram - Python and Linux training and consulting - Dancing Bison Enterprises

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

Contact Page

Monday, January 7, 2013

Introduction to Go: Dr. Dobbs: a five-part series

Introduction to Go | Dr Dobb's

It is by Mark Summerfield, a software developer and trainer who has also written books on Qt and Python.

- vr
dancingbison.com

Thursday, December 20, 2012

IPython tutorials

Resource: The essential IPython tutorials

Sounds interesting based on the use cases mentioned for IPython Notebooks.

Thursday, August 23, 2012

Shortcutfoo is cool and useful - learn shortcuts for different tools - in the browser

By Vasudev Ram


Shortcutfoo lets you learn / practice shortcut commands for different tools like vim, emacs, Sublime text (all three are text editors), the command line, Visual Studio, Photoshop, Xcode, Eclipse. More areas are coming, including Gmail.

Try Shortcutfoo here

About Shortcutfoo


- Vasudev Ram - Dancing Bison Enterprises


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