analitics

Pages

Showing posts with label vim. Show all posts
Showing posts with label vim. Show all posts

Sunday, September 23, 2012

Setting up Vim to work with Python - part 2

You can customize Vim for editing a specific file.

In this case is our python script files.

To do that you can use modelines, or auto commands, or filetype plugins.

For example , if you want to see the number on rows then just set the boolean number option to true.

set number

The keys PageUp and PageDown are mapped to Ctr+U and Ctr+D.

The next changes of the vimrc file will move cursor only by half of screen.


map <PageUp> <C-U>
map <PageDown> <C-D>
imap <PageUp> <C-O><C-U>
imap <PageDown> <C-O><C-D>

To move the cursor to the end of the screen they have to be triggered twice:

map <PageUp> <C-U><C-U>
map <PageDown> <C-D><C-D>
imap <PageUp> <C-O><C-U><C-O><C-U>
imap <PageDown> <C-O><C-D><C-O><C-D>

Then you can add next line to prevent the cursor from changing the current column when jumping to other lines within the window.

set nostartofline

To add some lines on your python file.

For example many python files start with this:

#!/usr/bin/env python 
#-*- coding: utf-8 -*-

Just add the next line in your vimrc file.

:iabbrev newpy  #!/usr/bin/env python <cr>#-*- coding: utf-8 -*-

On your first line from your python file in the insert mode , write newpy and press Enter key.

Also will be a good idea to take a look at the official .vimrc for following PEP 7 & 8 conventions.

...and the result of this changes.


Saturday, September 22, 2012

Setting up Vim to work with Python - part 1

You need to use mercurial.

Mercurial is a free, distributed source control management tool.

It is mainly implemented using the Python programming language and C.

To use it , just see the next command.

# hg clone https://vim.googlecode.com/hg/ vim 
requesting all changes
adding changesets
adding manifests
adding file changes
added 3831 changesets with 24526 changes to 2566 files (+2 heads)
updating working directory
2385 files updated, 0 files merged, 0 files removed, 0 files unresolved

Go to vim folder.

# cd vim/src/

Use configure to builds a Makefile file.

src # ./configure --enable-pythoninterp --with-features=huge --prefix=$HOME/opt/vim
configure: creating cache auto/config.cache
checking whether make sets $(MAKE)... yes
checking for gcc... gcc
checking whether the C compiler works... yes
...

The next commands make builds the program and make install to install the program.

src # make && make install
mkdir objects
...

Now let's try to make working well.

src # mkdir -p $HOME/bin
src # cd $HOME/bin
bin # ls
vim
bin # ln -s $HOME/opt/vim/bin/vim
bin # ls
vim
bin # which vim
/usr/bin/vim
bin # vim --version 
VIM - Vi IMproved 7.2 (2008 Aug 9, compiled Sep 21 2009 11:22:49)
Included patches: 1-245

If you read all the output , will see something like this:

+profile +python +quickfix 

The main goal is to use vimrc.

For example you cand do this.

" Wrapping and tabs spaces.
set tw=78 ts=4 sw=4 sta et sts=4 ai

" Update syntax highlighting.
let python_highlight_all = 1

" Create smart indenting
set smartindent cinwords=if,elif,else,for,while,try,except,finally,def,class

" Auto completion using ctrl-space
set omnifunc=pythoncomplete#Complete
inoremap <nul> <C-x><C-o>

With just few lines can change and improve your work.