Showing posts with label vi. Show all posts
Showing posts with label vi. Show all posts

Wednesday, February 4, 2015

Text editor (command line)

Raspberry pi text editor (command line)

Note: This is a translation of the Spanish version of my blog article I posted some years back

Before even mastering the command line, you will need to learn a way to edit text files from the command line. Be it to make configuration changes, or write a script (sequence of commands), you'll be a lot more productive if you learn how to use one.

Raspbian comes equipped with nano (easy to use when starting out) and vi (the standard - found on all *nix servers and desktops), but, there is something even better: vim (VI iMproved)

If you've never used vi before, might want to start with nano instead, or read a vi tutorial or even better, an interactive vim tutorial (highly recommended!!), and cheat sheet)

Normally, on Linux and modern Unix, vi is really vim, but on Raspbian, vi is vi. Let's change this:
fdion@raspberrypi ~ $ sudo apt-get install vim
There are a few things to add to .vimrc to make it better (in /home/user):

syntax on
filetype indent plugin on
set modeline

fdion@raspberrypi ~ $ pwd
/home/fdion
fdion@raspberrypi ~ $ vi .vimrc
(type i for insert, type the above 3 lines, hit Escape and :wq)
fdion@raspberrypi ~ $ ls .vimrc
.vimrc
Excellent, now scripts and code (like Python) will look much better, easier to read in color:


fdion@raspberrypi ~ $ vi file.py 




There are a few lines you will want to use specifically for Python scripts:


The first line is always the "shebang" line:

#!/usr/bin/env python

Here, we tell the operating system shell executor to pass the script to the python interpreter. For other interpreters, just replace python with whichever interpreter will handle the script.

Another thing for a python script that I like to use, a docstring (description) for the script, between """ and """. 

The seventh line above (in the code example) is for vim itself for proper tabs as 4 spaces (you want that for aligning your Python code, and is a nice to have in general for shell scripting):

# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4


If you are dealing with utf-8 code (for example accents or special characters) then you need to define the following:

# vim: set fileencoding=utf-8


Francois
@f_dion

Thursday, September 27, 2012

Getting wiringPi on the Pi

wiringPi is one of the various libraries available for the raspberry pi to interface with hardware. It is similar to the wiring library for the arduino.

One barrier to adoption seems to be the complexity of the installation.

In reality, it is not that hard, but you need to have setuptools for python installed, the development package for python and git.

Then you get the code from git and do a setup install, but there is one hiccup: there is a c source file that needs to be edited. But you are a Python person, so dont panic, I provide a 1 line sed command to do the edit for you.

Altogether, here is what you need to do:


sudo apt-get install python-setuptools python-dev git-core
git clone https://github.com/WiringPi/WiringPi-Python.git
cd WiringPi-Python
git submodule update --init
sed -i 's/<wiringPi.h>/"wiringPi.h"/g' WiringPi/wiringPi/piNes.c
sudo python setup.py install
Either you run these commands one by one, or using your favorite editor, you save the whole block in a file (let's call it installwpi) and then:

pi@raspberrypi ~ $ sh installwpi

Tuesday, September 25, 2012

Editores de Codigo (X)

Hablamos la semana pasada de vim como editor de código , y lo que hacer para ver el código Python con colores.

Ahora hablamos de editores en modo gráfico (con X)

Mi favorito es Scribes, pero vamos a ver 5 en total, hoy.

spe

Stani's Python Editor (http://pythonide.stani.be/)


 pi@raspberrypi ~ $ sudo apt-get install spe






gedit

Editor de Gnome (http://projects.gnome.org/gedit/)


 pi@raspberrypi ~ $ sudo apt-get install gedit

geany

Editor basado en GTK2 (http://www.geany.org/ )

Es necesario  hacer la instalación de xterm también.


 pi@raspberrypi ~ $ sudo apt-get install geany xterm

bluefish

Editor de código y HTML5 (http://bluefish.openoffice.nl/)


 pi@raspberrypi ~ $ sudo apt-get install bluefish

scribes

Editor ultra minimalista (https://launchpad.net/scribes)


 pi@raspberrypi ~ $ sudo apt-get install scribes



Hay una versión mas reciente también en launchpad. Como cambia a cada día, es posible que no opera bien esta versión, pero es la versión que estoy utilizando.

La instalación de esta versión es así:


 pi@raspberrypi ~ $ sudo apt-get install python-xdg bzr autoconf automake intltool gnome-doc-utils
 pi@raspberrypi ~ $ bzr branch lp:scribes

 pi@raspberrypi ~ $ sudo su

 # cd scribes

 # ./configure

 # make
 # make install



Friday, September 21, 2012

Editor de codigo Python (texto)

Muchas veces, debo hacer cambios, o escribir por completo un script (secuencia de comandos) Python a través de una conexión ssh. Sin X forwarding tampoco, y por eso no puedo utilizar un editor grafico.

Hay nano y vi en la distribución Raspbian Wheezy, pero, hay algo mejor: vim. En la mayoría de las distribuciones Linux y en OpenIndiana, vi es en realidad vim, pero en Raspbian, vi es vi. Vamos a ajustar eso:

fdion@raspberrypi ~ $ sudo apt-get install vim
Y tambien hay que anadir a .vimrc (en /home/user):

syntax on
filetype indent plugin on
set modeline

fdion@raspberrypi ~ $ pwd
/home/fdion
fdion@raspberrypi ~ $ ls .vimrc
.vimrc
Muy bien, ahora es mas facil a leer el codigo Python en color:


vi file.py




Hay tambien que anadir una linea con instrucciones para vim:


La primera linea es siempre la "shebang":

#!/usr/bin/env python


Despues, una docstring (descripcion) del fichero entre """ y """. La septima linea (en nuestro ejemplo) es para vim:

# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4


Voy a anadir otra linea tambien, en espanol, frances y portugués, a causa de los acentos debemos poner el codigo:

# vim: set fileencoding=utf-8

Francois
@f_dion