Three-Strikes-Zivi

In Reply to “Ab 3 Tage Krankenstand Zivildienst verlängern”

Naja… 3 12-Stunden-Schichten krank in jedem Monat. Dann würden wir gleich viel arbeiten, wie ein durchschnittlicher beruflicher Mitarbeiter. Das Rote Kreuz ist natürlich “sehr zufrieden” mit Zivildienern, sonst könnte jemand im Zuge dessen auf die Idee kommen die Arbeitsstunden der Zivildiener zu kontrollieren.

Naja… ist halt ein Idee von einer Frau, die nicht Zivildienst ableisten musste.

Three-Strikes-Zivi

meisterluk’s advent calendar: vimrcs and amazon

Merry christmas! As the last part of this serie, I want to pulish my own vimrc. Actually this vimrc is a work in progress and I didn’t finish it yet. You might want to add stuff for yourself. I also want to show another useful vimrc: the one by mitsuhiko. Especially his fruity colorscheme is very nice (screenshot). His repository with vim-configurations contains cool stuff.

Emacs

And last but not least, this is a screenshot when I was searching for the vim books at amazon.com. Find the mistakes!

meisterluk’s advent calendar: vimrcs and amazon

meisterluk’s advent calendar: Books, References …

One of the advantages of Vim to other text editors is it’s huge and full documentation. Using “:help <keyword>” you can get help about any keyword and feature. Anyway… the internet is another place to get information too. This is a simple collection of some random stuff, helped me creating this advent calendar.

Cheatsheets, References, …

Books:

Textverarbeitung mit dem VI Editor

meisterluk’s advent calendar: Books, References …

meisterluk’s advent calendar: multiple files

Vi lacks in providing a way to edit multiple files at the same time, but Vim offers an option to do so.

If you open several files via the command line, you can switch between those files with the edit-command (:help edit):

vim file1 file2 file3
:e file2 " edit file2
:e# " edit alternate file -- file1
:x

Using :e you can read the file (given as parameter) into the current buffer. Vim will save an alternate filename with the symbol #. As it can be seen in line 3, # stands for file1 (the "alternate filename") in this case.

Ok... this will load files "between" others and you can display one after another, but is actually very stupid. In most cases, when I'm programming and need to open 2 files, in general I need to write (modified) text from one file to another. In this case I want to have both files side by side. This is done with the split command:

:sp file1

This command will split the screen into two parts: one with the current buffer and one file with file1. To split files vertically, use the "vsp"-command. To handle this frame-separation anyway, you have to use the following commands. All of them require a <C-W> before.

<C-W> Move to next frame.
k Move to frame located above the current.
Up (see above)
j Move to frame located beyond the current split.
Down (see above)
+ Increases the size of the current frame by one line (eg. 5<C-W>+ will make the split five lines bigger.)
- (Minus sign) Decreases the size of the current frame by one line.
_ (Underscore) Maximize the current frame (that is, make it take up as much room as possible.)

See :help split for more information about this issue.

meisterluk’s advent calendar: multiple files

meisterluk’s advent calendar: Highlight(ning)s

Using vim in default ubuntu is quite uninteresting. White text on a dark background or otherwise. No colours, no keyword highlighting and every letter in the same colour. There are several colorschemes in the internet, which can be used and adapted for your own wishes. Put them into $VIMRUNTIME/colors (call :echo $VIMTUNTIME to get the environment variable). Such files should always start with something like:

set background=dark " or 'light'
if exists("syntax_on")
    syntax reset
endif
let g:colors_name = "foobar"

In the next lines, Vim uses the highlight command (abbr. hi) to set colors for specific textareas. Vim has already plugins to detect areas itself. For example <!– comments in HTML –> are accessible with the keyword htmlComment. To change the color of a html comment, we will use this command:

hi htmlComment term=bold guifg=#00CC00 guibg=#FFCC33 gui=bold

Of course, gui-anything means it’s only available in gui-mode. “fg” means foreground and “bg” background. See “:help hi” to get more information. I would really recommend to find good color schemes and adapt it for your own purposes. See “:help usr_06” for general information about syntax highlightning.

meisterluk’s advent calendar: Highlight(ning)s

meisterluk’s advent calendar: Vim for programmers

In vim you have all tools to generate real programs. Vim’s imperative scripting language is called vimscript. I would like to show you some basic. Use :help usr_41 to get more information. The following content can be saved as “script.vim” and can be used by typing :source script.vim in an other vim session (of course, you shall not change the working directory). There is no need for the inital colon before ex commands (because nearly everything is executed by ex) and double-quotes introduce a comment (as I mentioned before). Basically this is my workout of the first sections of usr_41.txt

" simple vimscript
" the syntax is similar to ruby

" define a simple variable
let i = 47

" overwriting var
let i = 47
while i >= 43
    let i -= 1
endwhile
echo i "is the answer"

" a 'for' loop
for i in range(1, 4)
    echo i "... "
endfor

" numbers
" negative octal, positive hexadecimal, positive octal
echo -024 0x14 024

" valid variable names match [a-zA-Z_][a-zA-Z0-9_]*
let _VIMRC = "~/.vimrc"

" variable scopes
" s:    local
" b:    local to a buffer
" w:    local to a window
" g:    global
" v:    variable predefined by Vim

" for example this variable is defined local
let s:vim = "rocks"
unlet s:vim " delete var

" unlet with exclamation mark
" if (exists(var)) delete it; else ignore.
unlet! _VIMRC 

" exists() checks existence of variable.
if !exists("s:count_calls")
    let s:count_calls = 0
endif
let s:count_calls = s:count_calls + 1
echo "called" s:count_calls "times"

" auto-converts string to a number
" Strings and Numbers are the two basic data types.
if "true"
    " will not be executed
    echo "string 'true' is true"
endif

Another thing I would like to talk about is the modeline. Every programmer has its own vim settings. Modifying a file of another programmer possibly destroys the whole program format like whitespaces in HTML or think about the tab-or-spaces-discussion of python. In this case it would be wise to add a comment in the file, which tells you the most important vim settings of the author. And Vim automatically reads this line and executes the set commands:

vim: autoindent nosmartindent expandtab shiftwidth=4

Enable modeline (:set modeline) to auto-read such lines, you might find at the bottom or top of a source file. Vim will search for it, if modeline is enabled. The settings can also be colon-separated (for compatibility reasons). You will get more information about that feature with :help modeline. For example if you want to make settings dependent on your Vim version, use the documentation in this case.

meisterluk’s advent calendar: Vim for programmers

meisterluk’s advent calendar: additions to regex, :s and :g

Ok… writing long regex’ and using this stupid :%s///g is very annoying. Actually it’s not usable and some features allow you to optimize your work with it.

~ The tilde is a alias for the regex used in the last search. Available in normal searches (via slash) and substitution commands.
n This sequence allows you to access subclasses. For example :%s/(rat)ing/1e/g will replace all “rating”s with “rate”. Note: n is the number of the subclass of the regex
Escaping character. For example 1 is not a regex subclass; this is simply “1”.
& Will be replaced with to whole match. For example :%s/./&/g will replace each character with itself.
l u The character following l will be converted to lowercase; the character following u to uppercase.
L U Like l and u it converts characters. But this affects the whole term. :%s/Home/U&/g will replace “Home” to “HOME” everywhere.

Beside all those special characters, another problem are [brackets]. I mean, it’s very useful to define range to match different characters, but it might be frustrating: [a-z] does not match é and a simple dot also matches numbers. To match é, but ignore numbers, you have two different possibilities (the slash introduces a search):

/[^0-9]

This regex matches anything except numbers. The other possibility is to define a character class:

[:alnum:] alphanumeric characters
[:alpha:] alphabetic characters
[:blank:] whitespace characters (like space, tabulator)
[:cntrl:] control characters
[:digit:] numeric characters [0-9]
[:graph:] all characters except whitespaces
[:lower:] lowercases
[:print:] printable characters (including spaces)
[:punct:] punctuation marks
[:space:] all whitespace characters
[:upper:] uppercases
[:xdigit:] characters for hexadecimal numbers

You can use these classes to match characters, you might have not available on your keyboard. The solve the problem from above, you have to negotiate a character class (yes, you can do this): /[^[:digit:]]

meisterluk’s advent calendar: additions to regex, :s and :g

meisterluk’s advent calendar: substitute and global

Global replacements can be done with 2 ex commands in general: substitute and global.

:s/deprecated/new/

This substitute command simply replaces “deprecated” with “new” in the current line only one time.

:5,10s/deprecated/new/g

5,10 is a range and means “from lines 5 to 10”. Another range example is “0,$” meaning “from line 0 to the end of the file”. But this example can be written shorter: % means the whole content of the current file and so it can be used as an alias for “0,$”. The “g” at the end of the command means “global” and will make replacements any time the pattern matches. To approve each replacement, add an “c” (confirm) to “g”. Each line will be shown before the replacement will be done. Type “y” to accept an replacement and press Enter to get to the next match.

You have already learned a way to make replacements: Searches and macros. “/dep” will search for “dep” and “cwnew” will replace the word with “new”. Make the search again by typing “n”. Don’t forget the undo function: If anything fails, you can undo it by pressing “u” and redo it with “.”.

Sometime you only want to replace term by term in some special cases. For example “replace x with y only if line contains a”. In this case you need context-sensitive replacements. This is done with the global command:

:g/a/s/x/y/

So “:g” is the inital command. “a” is the term to match to select the line. “s” introduces a substition in this line and exactly “x” with “y”.

meisterluk’s advent calendar: substitute and global