Vim For Mac Python



The improved version of vi editor is Vim that can be used for creating or editing source codes of different types of programming or scripting languages. It is a configurable text editor and works faster than other command-based text editors. It can also work with various plugins and vimscript. This editor can be configured for creating a development environment for python programming. Python is a very popular programming language now and used for developing different types of applications. The coder can write python code on vim editor very easily and fast if the editor is configured properly for writing python programming. How you can add setting and install vim plugins for creating python IDE is shown in this tutorial.

The official home of the Python Programming Language. There are example customisation files for Vim available in the Misc/Vim directory in the Python source tree.

  • Vim is a popular code editor on Unix/Linux systems, and it is often compared with Emacs, another great code editor among programmers. The debate about which one is the best editor never cease.Vim is powerful, but it has its own shortcomings as an old editor 1.Over the years, Vim’s code base has grown increasing larger, which makes it hard for maintenance and for adding new features quickly.
  • Python (=2.6 or =3.1) with the curses module and (optionally) wide-unicode support; A pager (less by default) Optional dependencies. For general usage: file for determining file types; chardet (Python package) for improved encoding detection of text files; sudo to use the 'run as root' feature.
  • 先安装 brew install vim -with-python. 0) You will need a vim compiled with python support. Check it with vim -version grep +python. Also, your.vim folder should be empty.
  • FAQ What is the project status? The current stable release version is 0.4.4.See the roadmap for progress and plans. Is Neovim trying to turn Vim into an IDE? With 30% less source-code than Vim, the vision of Neovim is to enable new applications without compromising Vim's traditional roles. Will Neovim deprecate Vimscript?

Before starting this tutorial you have to check vim editor and python are installed and working properly in your Ubuntu operating system. Run the following commands to confirm that both packages are installed.

Run the following command if vim is not installed.

Run the following command if python3 is not installed.

***Note: if the old version of python is installed before then make it confirm that python3 is working as default.

Configuring vimrc file for python

.vimrc file contains all configuration settings of vim editor. If you are running vim editor for the first time, then this file will be empty. If you used vim editor before and added any setting, then the file will contain some text. I have added some syntax highlighting settings before. So the file contains some settings. Run the following command to open .vimrc file in vim editor to add setting for a python script.

Include the following settings at the end of the file for creating a python development environment. Save and close the file by pressing ‘😡’.

' Enable syntax highlighting for python codes
let python_highlight_all = 1
'
Display line numbers in each line
set number
' Display an underline where the cursor is located
set cursorline
'
Add 4 spaces for each tab
set ts=4
' Highlight the matching part of the brackets, (), {} and []
set showmatch
'
It is used to control the number of tabs that will be used by vim when tab
key will pressed
set softtabstop=4
'It is used to control the number of columns when left or right shift is pressed
set shiftwidth=8
'
It is used for automatic text wrapping
set textwidth=79
'It is used to convert all new tab character into space
set expandtab
'
It is used for adding automatic indention in vim
set autoindent
'It is used to inform vim about the file format and how to read the file
set fileformat=unix

Check the effect of the settings

Open any python script in vim editor and check the effect of the new settings. Here, average.py file is opened on vim editor. The content of the file is given below.

Vim For Mac Python

average.py

n1 =input('Enter first numbern')
n2 =input('Enter second numbern')
n3 =input('Enter third numbern')
average =(int(n1)+int(n2)+int(n3))/3
print('The average value is ',round(average,2))

The effects of the settings will be shown after opening the file in vim editor. The line number is added in each line of the file for number setting. Underline is added where the cursor is located for cursorline setting.

When the tab key is pressed then 4 spaces will be added for ts setting like the following image.

When the cursor is set at the position of any starting bracket then it will highlight both starting and ending brackets for showmatch setting.

Using auto-complete feature

vim editor has built-in auto-complete feature. This feature works after entering in INSERT mode. Ctrl+n is used to open the drop-down list and the user can select any word from the list to enter as the next word. Ctrl+p is used to select the last word from the list. Some uses of the auto-complete feature of vim are shown in the next part of the tutorial.

Vim For Mac Python Programming

Mac

Open any new or existing python file in vim editor to test the auto-complete feature. Here, average.py file is opened in vim editor. Press i to enter in the insert mode and move the cursor where you want to add the code. Press Ctrl+n to open auto-complete list. It will display the list of all declared variable and function names.

Vim

To select any particular variable or function, type the first character of the variable or function name and press Ctrl+n to open the auto-complete list. If Ctrl+n is pressed after typing ‘p’ then ‘print‘ function will be auto-selected like the following image.

The following image shows the use of Ctrl+p command of vim . Enable the insert mode, type the character ‘i’ and press Ctrl+n to display the auto-complete list. Press Ctrl+p to select the last item from the list. Here, int is selected.

Vim For Mac Python Commands

The main limitation of the built-in auto-complete feature of vim is that it has limited options for doing auto-complete tasks. If you want to do auto-complete tasks more efficiently then it is better to use an auto-complete plugin of vim editor. The names of some auto-complete plugins are jedi-vim, youcompleteme, pythoncomplete etc.

Conclusion

The coder needs less time and effort if any good editor is used for writing the code. Vim editor is one of the popular editors for writing python script. This editor has many useful features that help the coder to write or edit python code more efficiently. If you want to use vim editor for python programming or want to create a python development environment in vim then this tutorial will help you to do that task.

I've been using python and vim for the past few years as my sole development environment since I hate GUIs, and I love how simple and powerful vim really is. I started off with a simple tutorial, and then expanded from there to develop my own custom IDE. I recently discovered that vim7 has support for running python commands inline. This only further's my love of vim.
I also recently discovered a nice little checker for vim called pyflakes. This program allows you to check python scripts for simple errors that are detected at compile time in languages that support that sort of thing. It includes being able to check for un-imported modules, typos, and even things that you imported that you didn't use within that module.
The problem is that I'm using Mac OSX, which comes with vim that isn't compiled with '--enable-pythoninterp', so the pyflakes vim plugin doesn't work. A simple workaround is on the tutorial page I found, by simply installing MacVim though you only get it compiled against python2.5, not 2.6 which is standard now in Snow Leopard. Additionally, the syntax highlighting in MacVim is horrible for python compared to the built in one that ships with OSX, and I still prefer to run vim in the console.
Solving this issue involves a few steps:
1) Build MacVim from source. Of course you'll need the 10.6 XCode to do this, but after that it's pretty straight forward. The only thing to watch is that you have to run './configure --enable-pythoninterp --with-macsdk=10.6' instead of just './configure'. Other then that you can follow the instructions from the MacVim site for compiling your own version.

Vim For Mac Python Installer

2) Copy the MacVim.app folder into your /Applications folder

Vim For Mac Python Editor

3) add [ -x '/Applications/MacVim.app/Contents/MacOS/Vim' ] && alias vim=/Applications/MacVim.app/Contents/MacOS/Vim To your shell rc (for me it's .zshrc but for most people it's probably .tcshrc). I noticed that simply copying the Vim binary to your PATH, or sym linking it doesn't work, not quite sure why but this works well enough for me.
4) Copy the python syntax file from /usr/share/vim/vim72/syntax/python.vim to ~/.vim/syntax/python.vim
5) Download and install the pyflakes.vim plugin
6) Open up a new terminal and edit an already created python source file.
After you've done all of that you should see that anything that would normally show up in an error while running pyflakes now automatically get's highlighted as an error.
I've also been using the pyflakes binary included with the pyflakes.vim plugin to check everything in my modules before building and uploading them to our servers. The only issue I've noticed is that sometimes you need to import a module even though you don't need to use it (for example with boto this is used in the sdb.db module to enable reverse-references). I'm still not quite sure how to ignore those or get around that.
One other thing that doesn't work is doing conditional imports, for example to get JSON support the manuals tell you to do:

try:
import json
except ImportError:
import simplejson as json



But in pyflakes, this will result in an error.