Tuesday, July 18, 2006

vim :: tags keyboard shortcuts

Keyboard shortcuts used to work with tags. There are really few of them. All of them work in control/normal mode.

  • :set tags? needless to remind that VIM must be told which tag files to use. It can be done automatically as for example shown here.
  • :tag word is main one. Opens file and goes to line with first tag entry matching word.
  • ^] same as above, but uses word under cursor.
  • :tag /word is similar to :tag word. Opens file and goes to line with first tag entry matching regular expression in word.
  • :tselect word is what I use most often. The function - tag select - displays list of matching tags allowing you to select one to go to. Without word, it will display list of matching tags for word used last time.
  • g^] same as above, but uses word under cursor. Also, omits displaying list if only single match is found.
  • :tnext goes to next matching tag entry.
  • :tprev goes to previous matching tag entry.
  • :tlast goes to last matching tag entry.
  • :tfirst goes to first matching tag entry.
  • ^T Every time you do :tag word, the word with current file position is put on stack and vim goes to new file/position as it is told by tag file. To restore the previous file position you use ^T. The shortcut removes tag stack top element and moves to the file position saved there. So if you wandered off far away with ^] - to return back where you started you need to press ^T until vim would complain that it is at bottom of tag stack.
That's about it. Both :tag word and :tselect word support wildcard key (Tab/^D) to show list of tags matching partially the word. That will work with regilar expressions too. But be warned that often tag files can host millions of entries: displaying list of thousands items can take some time. Use wildcard key carefully.

Friday, July 14, 2006

syntax highlight for non-standard file types

Following line once put into vimrc will tell vim which syntax highlighting to use:

au BufReadPost SCons* set syntax=python
au BufReadPost Cons* set syntax=perl
au BufReadPost *.mke set syntax=make
au BufReadPost make*.inc set syntax=make
au BufReadPost *.fcc set syntax=cpp
au BufReadPost *.fhh set syntax=cpp


This is selfexplanatory. e.g. SCons files are in fact python scripts - so tell vim to use python syntax highlighting on everything what matches 'SCons*'. Same goes for Construct - but done in perl.

:au BufReadPost <mask> tells vim to execute something right after file read into buffer. In that particular case we use that to set buffer local variable 'syntax' to name of one of standard syntax highlightings (perl, make, cpp, etc). :help :au for more info.

P.S. One can check what syntax module vim has loaded by looking at 'syntax' variable value: do :set syntax? after opening file recognized by vim.

P.P.S. Of course, :syntax on has to be put somewhere in the vimrc for syntax highlighting to work automatically. VIM 6.x doesn't have problem of previous versions (I experienced that in VIM 5.x), where loading of syntax from vimrc was slowing launch time, even if file had no syntax highlighting assigned. Hm.. or probably my PC now fast enough for me not to notice that launch time. Who cares. It works.

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).

Edit1:

to

tabs!!!

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

http://derkarl.org/why_to_tabs.html

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

Linux.com has few word to say about VIM's search/replace facility. Read on.

http://www.linux.com/article.pl?sid=06/06/26/1525255

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.

Wednesday, May 31, 2006

purpose of the blog

If somebody comes across the blog, I want to make sure what people see here.

Another hard drive crashed, another non-backed-up $HOME died - and again I have to ressurect my .vimrc from ashes. Piece by piece. This becomes a habit.

With the blog I hope to record as much knowledge as possible, so next time creation of my .vimrc - full fledged, ready for production environment - wouldn't take as long as it takes now. There would be single place with all things I need. And it will be smaller than vim's online ":help"

P.S. Some people argued that I just need to save my up-to-date .vimrc somewhere on the net. But this is a way of Emacs users. I do not like NNN kilobytes of black magic everybody copied from Joe The Emacs Pro and nobody understand meaning of. I like to have things I (1) need and (2) understand. I do not like black magic.

Monday, May 29, 2006

VIM's buffers

Buffer is what VIM uses for holding all information related to a file open during editing session. One can think of buffer as file. The only case when buffer is not file - is when we started editing file and not yet saved it.

VIM keeps inactive files closed. Only file(s) currently displayed is(are) open. (You can have of course more than one file displayed with multiple windows).

":ls" lists all buffers. (Conveniently, common mistype ":sl" is call to function sleep with default to sleep for 1 second.)

":b1" - switch to buffer number 1 - will reopen first file open for editing in the vim session. If file in current buffer was modified, the command would fail.

":bd" deletes current buffer. Makes vim forget that it edited the file in the buffer. Buffer numbers are not reused. Trying later to switch to that buffer number will reopen the file. In other words, ":bd" only removes the buffer from output of command ":ls".

":b#" switches to previously open buffer. Something I use a lot. Similar to ":b1" command, but instead of literal buffer number it uses vim's shortcut '#' for previous active buffer. '%' is shortcut to currently active buffer and consequently ":b%" is nop, since there is nothing to be done to activate already active buffer.

So all of my .vimrc have the line:

map <F6> :b#<CR>

what allows me to work with two files very quickly: I just need to press F6 to go to second file.

Type ":help windows.txt" in VIM for more info.

P.S. The buffers is where VIM stores info about files to open, if more than single file was specified. If vim is called that way:
$ vim *.c
you would find all of .c files vim is to open by ":ls" command. Usual :next/:prev are used to browse thru all the files.