Monthly Archives: December 2009 - Page 3

meisterluk’s advent calendar: settings 1

We have learned several things to configure vim. Especially the set command is aimed to configure some vim features. All those settings can be written in a single file and will be executed before vim startup. This file is called .vimrc and should be placed in your home directory (on unix systems). On Windows systems the file is called _vimrc. Look “:help vimrc” for more information. In .vimrc all commands are read as an ex command. So you don’t need to write the prepended colons.
Note: When starting vim on a console, using the -c option (execute command), the command will be executed after .vimrc is read. Using the –cmd option will change this behaviour (before .vimrc is read).

set ignorecase

We already talked about it in the section “find, search, grep”. This makes searches incase-sensitive. It’s abbreviation is “set ic”. Prepending “no” will (of course) lead to the opposite behaviour.

set nocompatible

This makes Vim starting in compatibility mode. For example everything is interpreted as an command and no cursor keys are available (as it is in Vi) and every Vim reference tells you to turn it off, because using newer Vim plugins might result in an error, because those plugin are not compatible with old versions of Vim.Using a file named “.vimrc” disables the compatibility flag in most cases automatically.

set nodigraph

Digraph is a feature by Vim to create special characters. For example typing 1<BS>2 will result in ½. I really make typing mistakes often and as far as I don’t use commands to delete sequences in an text, the backspace key is very important for me. So I really recommend to turn it off. I only use Vim for programming and special characters in source code is also bad.

So using these settings will result in an .vimrc like (comments are written with an introducing double-quote “)

" My vimrc

set ignorecase " case-insensitive searches
set nocompatible " no compatibility mode
set nodigraph " no digraph

meisterluk’s advent calendar: using buffers and marks

When deleting lines or sentences, you might want to insert that text somewhere else. Whenever you do a delete action, the content will be put into the main buffer. You can output the content buffer by pressing “p” (put). So this is why we already used these methods. For example write a three-lines-example-textfile. Now put the cursor at row 2, press “dd” (delete line) and “p” (put). Lines 2 and 3 will be transposed now. To support the possibility to use several buffers, they have to be named. Use a lowercase letter for this job:

“dyy copy current line in buffer d
“Dyy append current line to buffer d
“dp dpaste content of buffer d after cursorposition

Markings are a nice way to refind positions in a huge text file. The main disadvantage: You can set a mark, but it will only be active for one session.

m mark position (ma -> mark position as “a”)
‘a (single-quote) goto beginning of line with mark “a”
`a (tick) goto mark “a”

meisterluk’s advent calendar: extended insert 2

In part 2 of “extended insert” I want to show you intelligent usage of the a (“append”), i (“insert”), c (“change”), d (“delete”) and other insert commands.

EOL = End of Line, EOW = End of Word
To append something to EOL, you can either type $a in command mode or just use the short version (big letter) A. Not less meaningful is I. This is the short version of 0i (“jump to column zero and insert”). Here is the next summary:

A append to EOL ($a)
I insert at begin of line (0i)
cc delete current line and insert
C change line until EOL (c$)
D delete characters until EOL (d$)
de delete word without deleting whitespace at end of word (cursor to EOW)
dE delete word without deleting whitespace but with punctuation marks (cursor to EOW)
P paste, but before cursor position
Y Copy the current line
S Substitute – delete all characters of line (cc)
R Replace-Mode (replace all characters with typing ones)
O Open line – insert empty line beyond current line

Exercise: What does those commands result in?
vim file<CR>iHome<Esc>0<Right><Right>rl<Esc>:wq
vim file<CR>aHoem<Esc>$ar<Esc><Left><Left>xp<Esc>:wq
vim file<CR>iHome<CR>r<CR>r<Esc>:2<CR>dd<Esc>:wq

Note: Programmers will think that the first line is indexed with zero, but it is 1. Type “:set nu” in command mode to add line numbers to each line.

Write a small text file and give it a try. Undo your actions to recover the old version. Use all these commands in command mode and learn-by-doing. In the next session we will talk about buffers and after that we will use this stuff to use mappings and macros.

Fängt schonmal gut an…

Versuche den neuen Standrechner zum Laufen zu bringen. Die Setups der versch. OS sind nicht so berauschend:

Note: Unter allen Linux’ lande ich nach der Fehlermeldung in der BusyBox. Was ich dort machen soll (möchte fdisk verwenden), weiß ich aber nicht.
CD = CD/DVD

Windows (CD) “An unexpected error occured at line XXX. Will exit setup”
Knoppix 3.4 (CD) “Cannot find Knoppix filesys”
openSUSE (CD) Lädt ganz normal. Vor Bereitschaft rebootet der Rechner plötzlich
grml 1.0 (CD) “Cannot access tty”
grml 1.1 (CD) “Cannot access tty”
grml 2009.10 (USB-Stick) “0AA8 Loading …………” (nach halber Stunde keine Veränderung)

Boah… zum Glück war der “Error” “unexpected”. Wenn er “expected” gewesen wäre, hätte ich mir wirklich Sorgen machen müssen.

Probiere jetzt mal direkt mit Debian was zu machen.

meisterluk’s advent calendar: extended insert 1

When switching to insert mode, you start at the current cursor position. These commands are useful to provide further insertion commands.

“a” is a shortcut for “append”. Type “a” in command mode and you will append text to the current cursor position. There are several of those commands:

a append – append text after cursor position
i insert – insert text before cursor position
c change – change text
d delete – delete text
and copy it to buffer
p put/paste – paste text
from buffer
y yank – copy text to buffer
s substitute – delete one character after cursor position
r replace – replace character at cursor position with second argument (“home” + rc -> “come”)
o open line – Insert empty line below current one
x delete character at cursor position (nothing new)
X delete character after cursor position

Those commands are not useful as a standalone. But using it combined with ranges, it rocks. For example type this:
<CR> = <Enter>

vim file<CR>ifoo<CR>bar<CR>test<Esc>:%d

The percentage symbol defines a range; actually it refers to the content of the current file. “d” will delete it. So %d will delete the file’s content. Ranges can also be given by line numbers:

1,2d

This will delete line numbers 1 to 2 (both included). A missing linenumber argument is replaced by the current line.

,7d

This will delete all lines from the current line to the seventh. Other ex ranges are:

n a simple line number
. the current line
$ the last line in file
* entire file, visual area
‘t position of mark t
/p/ the next line where p matches
?p? previous line where p matches
+n -n +n, -n to the preceding line number

In the next session we will practice combined commands and learn new shortcuts for useful combinations.

meisterluk’s advent calendar: vimtutor

luki @ unix ~ % vimtutor

vimtutor is a nice tool, provided by Vim. It consists of a simple HowTo and is started within the shell. In 7 lessons you will learn the basics, we have learned the last days. As the file is not opened read-only, you can practice the commands directly in the file. Special exercises are given in the lessons. In the next days we are going to talk about actions and ranges. After that we will have finished all elements of vimtutor. In the mid of december we will try to configure Vim for our purposes.

Try out vimtutor and check what you have learned from our previous sessions ;-)

Note: The so-called “command mode” is also named “normal mode”. Basically it’s the official name and used by vimtutor.
Update: For all Debian-(Derivat)-Users with no pre-configured vimtutor: Use this command to install vimtutor, which is a single file started with vim:
sudo apt-get install vim-runtime

meisterluk’s advent calendar: vim repetition redo and undo

Redo and undo are important features to automate repeated commands and save a lot of time. For example open a new file and type “vim text editor” ended by a newline character. This insertion of text shall be repeated. Use the dot (.) to redo insert actions. Each time when pressing the key, a new line with “vim text editor” shall be appended.

The “u” key does the opposite: It undoes actions. Each time pressing the key, a line with the text should disappear.

u undo
<Ctrl>r redo
; Redo search command
, Redo search command in other direction
U undo all operations of current line (don’t leave the line!)
J Put 2 lines together
~ Current character: change upper/lowercase
x delete character at cursor position
X delete character after cursor position
xp transpose – “Windows” + xp = “iWndows”
. repeat command
Ctrl+@ repeat command, but no deletion

meisterluk’s advent calendar: find, search, grep

Searches can be performed with the slash. Press slash in the command mode and add the term. For example:

/term

… searches for the next occurence of “term”. Per default searches are performed case-sensitive. You can use the first set-command we learn to change this behaviour (make searches case-insensitive):

:set noignorecase

It’s abbreviation is (you can use this command also)…

:set noic

In the next HowTos we learn how to save this behaviour globally for all vim sessions (keyword .vimrc). To jump to the next match, use n. Here are further key bindings:

/pattern search for next occurence of “pattern”
?pattern search for last occurence of “pattern”
n get next pattern match
N get last pattern match
(2 acute accents) Goto last edit-/searchtarget
(2 single-quotes) Goto last edit-/searchtarget at line beginning

Obfuscated Python

Obfuscated Python

Ich liebe ja Python in erster Linie für seine wunderschöne Syntax. Aber dieses Beispiel zeigt, dass auch Python obfuscated sein kann. Der Autor (Ulf Bartelt) nutzt hier lambda-Syntax sehr stark. Die Ausgabe finde ich atemberaubend.

#!/usr/bin/env python
# 94-Aug-28, Ulf Bartelt
print (lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+y,map(lambda y,
Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,
Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,
i=i,Sx=Sx,F=lambda xc,yc,x,y,k,f=lambda xc,yc,x,y,k,f:(k<=0)or (x*x+y*y
>=4.0) or 1+f(xc,yc,x*x-y*y+xc,2.0*x*y+yc,k-1,f):f(xc,yc,x,y,k,f):chr(
64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy
))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24)
#    ___ ___/  ___ ___/  |   |   |__ lines on screen
#        V          V      |   |______ columns on screen
#        |          |      |__________ maximum of "iterations"
#        |          |_________________ range on y axis
#        |____________________________ range on x axis

# Don't try this at home, kids!

Gefunden in der python FAQ (originaler Eintrag des Autors)

Notiz: Vorsicht, manche Beispiele in den verlinkten Beiträge werden mit py3 nicht mehr gehen!

meisterluk’s advent calendar: vim – navigation in a file

When upgrading my ubuntu I had troubles using my keyboard. I could not use my cursor, because the keys were connected to different keycodes (Up was Enter). To repair this, I needed to edit my .Xmodmap. I could not navigate through the lines, because of the keyboard, but I knew that vim has another option. For example “k” also means “one line up”. Open a long file and navigate through the lines using those letters:

       k
   h       l
       j

Of course you have to type these keys in command mode. Because these are no ex commands, they do not appear in the bottom line. So to change your cursor position one line less, press k. Press h to jump one position left. To make a greater jump, press 0. This will change the cursor position to the beginning of the line. Use this table for other stuff.

Some commands must be finished by an action. Those commands are followed by <Enter> in the table. Bold commands are IMHO important and useful.

EOL = End of Line
EOF = End of File
EOW = End of Word.

0 Goto to the begin of the line
n| Goto column n of current line
$ Goto to EOL
w Go forward word by word
W Go forward word by word, don’t mind symbols or punctuation marks
b Go backwards word by word
B Go backwards word by word, don’t mind symbols or punctuation marks
fx Goto next “x” in current line
Fx Goto last “x” in current line
tx Goto character before next “x” in current line
Tx Goto character after last “x” in current line
200z<Enter> Goto line 200
200G Goto line 200
G Goto EOF
<Enter> Goto first character of next line (ignore whitespaces)
+ see above
^ Goto first character of current line (ignore whitespaces)
- Goto first character of last line (ignore whitespaces)
( Goto beginning of sentence
e Goto EOW
E Goto EOW (ignore punctucation symbols)
) Goto end of sentence
{ Goto beginning of paragraph
} Goto end of paragraph