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

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

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

Tuesday, September 18, 2012

How to ROT13 a line of text in vim


By Vasudev Ram


ROT13 (which stands for "rotate by 13 places") is a simple substitution cipher. It encodes letters of the English alphabet by replacing each letter by the one 13 letters after it, wrapping around to A and beyond for letters after M.

I came across this capability of vim while checking out ROT13 for a program I'm writing. You can ROT13 a line of text in the vim text editor with this command:
ggVGg?
See the Wikipedia ROT13 page for more on what ROT13 is, and for another vim example.

An interesting property of ROT13 is that it is its own inverse; i.e. ROT13(ROT13(x)) == x. The XOR (exclusive OR) operator also has the same property in some cases. E.g. if you XOR (*) an ASCII character, say x, with some other constant character c, you get, say, the character y (c, x and y are meant as placeholders, not the actual letters c, x and y). Then if you XOR y with the same fixed character c, you get x again. I had used this long ago to write a simple encryption program in C that would encrypt text files using this algorithm.

UPDATE: Forgot to mention that, due to this self-inverse property of XOR, the decryption program code was exactly the same as the encryption program, except for different prompts or error messages (e.g. "enter name of file to encrypt" vs. "enter name of file to decrypt"). Of course, the encryption would be trivial to break, for anyone with some basic knowledge of cryptography. I just wrote it as an exercise for learning.

(*) Here, by XOR ASCII characters, I mean treat the characters as eight-bit bytes and do the XOR operations on them. This process is transparent in C because characters are really stored as ints anyway.

- 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

Thursday, April 26, 2012

So Unix and VIM are famous programming languages ...


See section "Are there any famous
Mountain View Googlers?" at this page:

http://www.google.com/about/jobs/locations/mountain-view/

... Wow, I never knew that, and I've been using both UNIX and Vim all my life. But if the big G sez so, it must be right. Right? rite? ..... Helllooo .....

- Vasudev Ram
www.dancingbison.com

Thursday, September 15, 2011

Python stdlib docs as Vim help files

By Vasudev Ram - dancingbison.com | @vasudevram | jugad2.blogspot.com

Saw this recently.

The Python standard library documentation as Vim help files:

http://jeetworks.org/node/92

It seems to be a work in progress but may be of use. The potential benefit is that you can look up Python library documentation from within Vim by typing ":help py2stdlib". Neat idea.

It is by Jeet Sukumaran ( http://jeetworks.org/about ) , currently a PhD student at the University of Kansas, who uses Python for his work.

Posted via email

- Vasudev Ram @ Dancing Bison