Friday, June 30, 2006

highligting tabs and trailing spaces

Add the following to .vimrc:

highlight SpecialKey ctermfg=DarkGray
set listchars=tab:»-,trail:~
set list

That makes something like that:

map <silent> <F7> :exec ":e ".(expand("%") =~ ".h$"
\ ? glob(substitute(expand("%"), ".h$", ".cpp", ""))
\ : substitute(expand("%"), "\\.cpp$", ".h", ""));<CR>

to be displayed that way:
map <silent> <F7> :exec ":e ".(expand("%") =~ ".h$"~
»-------\»------? glob(substitute(expand("%"), ".h$", ".cpp", ""))
»-------\»------: substitute(expand("%"), "\\.cpp$", ".h", ""))<CR>

Tabs are now plainly visible and also public enemy trailing space on first line became apparent.

P.S. Nice character '»' requires set encoding=utf-8. That looks great with "Andale Mono" and "Black Bold" color set in PuTTY to (39,39,39).




It's nice to know that there are reasonable people left. Read on.

Attack of clowns^Wclones^Wamateurs of WYSIWYG/WYSIAYG at times is plain depressing.

Wednesday, June 28, 2006

vim :: windows and buffers

VIM can open and edit simultaneously many files. For that VIM uses two elements:

  1. Window - part of VIM's screen space. Screen of every VIM instance can be divided into several windows. When GUI available, one can of course launch several instances of VIM. But in terminal in common case user is limited to single 80x25. When you need to edit two files side by side - VIM windows come to help.
  2. Buffer - place in VIM's memory. That's where file content resides while you are editing it. Buffers is what you see in output of ":ls" command. Every file you open automatically put into new buffer. Buffers are numbered starting from 1. Single buffer can be displayed in more than one window at the same time.
Consult ":help windows" (":help buffers") for indepth info on buffers and windows.

[link] basics of search and replace has few word to say about VIM's search/replace facility. Read on.

Monday, June 26, 2006

vim modes

Modes are probably the most obscure part of vim. I do not like them. But I do not like even more Emacs and PC's editors approach with terribly long and complicated shortcuts.

VIM mode defines how pressed keys are interpreted. One can tell currect VIM mode by content of left bottom corner of screen. Command (normal) mode has no special marker. When in insert mode one would see there '--INSERT--'. Replace & visual modes have similar markers: '--REPLACE--' & '--VISUAL--'.

  1. Command (normal) mode. The default mode used after invokation of vim. All keys are mapped into some command. Sleeping on keyboard in that mode can be dangerous: many key combos can be lethal to your document (or if you are root to your system). That's the main mode where one manipulates the content of file.

    Press <Esc> two times to get into command mode from any other mode/situation. Typing :q<Enter> would quit VIM.

    VIM's help for the shortcuts of command mode uses prefix "c_", e.g. ":help c_CTRL-D<Enter>" would bring the help for ^D shortcut of command mode. Type ":help c_" and press Tab/^D instead of Enter to see all standard shortcuts of command mode.

  2. Insert mode. The mode activated by pressing 'i' or <Insert> in command mode. Typing characters would insert the characters into the document. That's the mode one uses to input text. Shortcuts to manipulate text in insert mode are bound to non-printable characters and functional keys (e.g. keys with modifiers - Alt, Meta, Cmd, Shift, etc).

    VIM's help for the shortcuts of insert mode uses prefix "i_". Type ":help i_<Tab>" to see all standard shortcuts of insert mode. Insertion of text heavily covered in ":help insert.txt"

    N.B. Insert mode has a submode called "Replace mode". It's the same as insert mode, but typed letters instead of being inserted into the position under cursor - moving existing letters on the line right of cursor - would replace them. The submode is usually activated by pressing <Insert> in insert mode. I normally use 'R' (Shift-r) in command mode, since I do not like going into replace mode accidently hitting <Insert> twice (or hitting it once again in insert mode)

  3. Visual mode. The mode I use a lot, since I come from PCs/DOS and standard DOS editors depend heavily on visial text selection. Visual mode allows one to mark continuos part of text. Any invoked command allowing text range would use visual selection as the range. Commands are the same as in vim's command mode. The are three submodes:

    • Visual mode - by character. Activated by pressing 'v' in command mode. Use motions keys to select range character-wise. Selection starts on character under cursor when activating visual mode. End of selection is on characted under cursor. When finished selecting use command you like e.g. 'd' to delete selected text.

    • Visual mode - by line. Activated by pressing 'V' (Shift-v) in command mode. Allows to select lines of file. For example you can select several lines and send them to external filter application - e.g. ":r!sort" to sort them.

    • Visual mode - by block. Activated by pressing ^V (c_CTRL-V) in command mode. Most commonly refered as vertical selection. Allows you to select rectange-like part of text, spanning several lines. E.g. you can delete N characters in the beginning of several adjacent lines. Or every Nth character of several adjacent lines. Try it. It's hard to explain, but easy to understand by experimenting.

    Help for the shortcuts uses prefix 'v_', thou you would find that most of the shortcuts are the same as in command mode. ":help visual.txt" for more info.

Sode note. Officially, there are more to vim modes. The three aforementioned are the modes I use. All other modes find little application in my day to day life. For more indepth info on vim modes try ":help vim-modes". Every mode serves its purpose. Probably you would find some other modes useful too.

Tuesday, June 13, 2006

little things

Bunch of little options normally found in my vimrc:

  • set nowrap
    disable long ling wrapping. since most of the time I do coding, I do not need wrapping. My coding style doesn't "allow" long lines anyway.
  • set ruler
    enables display of current line/column number at bottom of screen.
  • set title
    enables changing terminal title. Works with xterm and alike by printing specially crafted escape sequence understood by the terminal emulators.
  • set bs=2
    make Backspace work as expected. Be default, new VIMs allow backspacing only just typed text.
  • set cindent
    enables indentation for C-like languages. Pressing Enter will position cursor indented according currect context nesting.
  • set cinkeys-=:,0#
    disable indentation processing when typing colon and hash symbols.
  • set wildignore=.o:.lib:.dld
    when completing file names, ignore files with extensions on the (colon separated) list.
  • set wildmode=list:longest
    when completing, first complete to the longest common matching string and then list possible matches.
  • set bg=dark
    tell syntax highlighting to expect dark (black) backgroung.
  • syntax on
    turn on syntax highlighting.
For more help on the options use e.g. :help 'ruler' - single quotes telling help command where to look for the given keyword. For help on syntax highlighting use :help syntax.