Running Python code in Vim

Running Python code in Vim
We are writing Python code using Vim, and every time we want to run my code, we type this inside Vim:

:w !python

Solution:

I have this on  .vimrc:

nano /home/userlocal/.vimrc
map  :w:!python %

which saves the current buffer and execute the code by pressing only Esc + F9

Basic Mapping

If there’s one feature of Vimscript that will let you bend Vim to your will more than any other, it is the ability to map keys. Mapping keys let you tell Vim:

When I press this key, I want you to do this stuff instead of whatever you would normally do.

We are going to start off by mapping keys in normal mode. We will talk about how to map keys in an insert and other modes in the next chapter.

Type a few lines of text into a file, then run:

:map - x

Put your cursor somewhere in the text and press -. Notice how Vim deleted the character under the cursor, just like if you had pressed x.

We already have a key for “delete the character under the cursor,” so let’s change that mapping to something slightly more useful. Run this command:

:map - dd

Now put your cursor on a line somewhere and press – again. This time Vim deletes the entire line because that is what dd does.
Special Characters

You can use to tell Vim about special keys. Try running this command:

:map  viw

Put your cursor on a word in your text and press the spacebar. Vim will visually select the word.

You can also map modifier keys like Ctrl and Alt. Run this:

:map  dd

Now pressing Ctrl+d on your keyboard will run dd.

Rate this post