Wednesday, February 11, 2009

Fixing PageUp and PageDown

For a long time it had annoyed me that pressing PageUp or PageDown would not only go to previous/next screen portion of file, but would also screw cursor position. Essentially, unlike good ol' DOS editors, in VIM pressing PageDown followed by PageUp (or vice versa) would send cursor to the start of first visible line - not back to the position where it was before. Especially as I now work more on my notebook's cramped keyboard (which btw has full sized keyboard, yet no keypad and no dedicated PageUp/PageDown keys) hitting by accident any of the keys requires some seconds to recover cursor position.

It annoyed me to the point of trying to make a VIM script to PageUp/PageDown properly.

But thankfully I have sobered up and my always questioning mind aksed: "Do you idiot think that you are the first annoyed by that? Don't you think VIM folks already have a feature for that??"

So finally I read as resourceful as ever :help - and my Eureka! awaited me right there, few lines above ":help <PageUp>". Now my ~/.vimrc has this:

map <PageUp> <C-U>
map <PageDown> <C-D>
imap <PageUp> <C-O><C-U>
imap <PageDown> <C-O><C-D>
set nostartofline


Essentially, PageUp and PageDown are now mapped to ^U and ^D respectively. The difference is that ^U/^D do preserve cursor's relative line number (relative to first visible line). ^U/^D work in normal mode, thus the ^O trick is needed to access it from insert mode.

The final nail - ":set nostartofline" - tells VIM during motion commands to try to preserve column where cursor is positioned.

You cannot possibly imagine how happy I am ;)

Update1. I felt something was wrong - but couldn't tell it immediately. Apparently, unlike PageUp/PageDown, ^U/^D move cursor only by half of screen. In other words, to emulate PageUp/PageDown, they have to be triggered twice:

map <PageUp> <C-U><C-U>
map <PageDown> <C-D><C-D>
imap <PageUp> <C-O><C-U><C-O><C-U>
imap <PageDown> <C-O><C-D><C-O><C-D>


Now that's much better.

Update2. Here goes improved version:

map <silent> <PageUp> 1000<C-U>
map <silent> <PageDown> 1000<C-D>
imap <silent> <PageUp> <C-O>1000<C-U>
imap <silent> <PageDown> <C-O>1000<C-D>


It turned out, ^U and ^D look at the counter for how many lines to scroll. Yet, they would scroll up to screen size. Thus specifying large value would make the shortcuts to scroll full screen at once. And you have to use counter all the time. Read more in :help CTRL-U and :help scroll. I'm not sure what led to the messy design (try :set scroll=NN) yet the mappings as above work fine.

With version from "Update1" especially on large files when triggering e.g. ^U^U it would be still visible that the scrolling happens in half screen steps. With the version that doesn't happen anymore as VIM now is properly told how many lines to scroll. As there is now counter involved, adding silent option is required.

VIM's keywordprg for Perl and ... VIM itself

This is logical follow-up to my previous post about K shortcut.

The two (actually three lines) added to ~/.vimrc allow when editing Perl and VIM scripts to transparently call with K proper help system.

au BufReadPost *.pl   set keywordprg=perldoc\ -f
au BufReadPost *.vim map K :exe ":help ".expand("<cword>")<CR>
au BufReadPost .vimrc map K :exe ":help ".expand("<cword>")<CR>


Perl one is obvious. VIM one actually required some trickery: we do not call external program, but call VIM's internal command instead.

P.S. BTW, :help :help is pretty useful reading. Due to my error, 'K' in VIM mode used to constantly show it, instead of expected 'expand("<cword>")'.

Thursday, November 20, 2008

how to not loose vim stdin buffer after opening a file

I often use VIM instead of "less" as pager. Usually in a way like "diff -u ... | vim -R -c 'set syntax=diff' -" (for what I actually have a shell alias).

The one thing was annoying me for quite some time: when vim reads input from stdin, it does create unnamed buffer where to the content is loaded. Unnamed means that as soon as I open in the same vim another file, the stdin input is lost and I do not know of a way to recover it: it doesn't show up in ":ls" anymore.

I have tried to do ":w" yet that created a new file - but the buffer with stdin still remained unnamed and was still replaced by first open file.

After some search I have found a nice command - ":sav" (:help :saveas).

Unlike originally intended for ranges ":w" command, the ":sav" operates on files and thus has enough authority to change buffer name, making stdin buffer persistent.

P.S. "vim -" is described under ":help read-stdin" and ":help --" (two minuses). Though the description is quite shallow.

Monday, November 10, 2008

getting colder?

Another gem (from pov of those of us who grep a lot) found in VIM documentation:

:help :colder

At first I thought that is another wit command but then realized that the name fits to :cnext and :cprev commands.

Snip from VIM documentation:

So far has been assumed that there is only one list of errors. Actually the
ten last used lists are remembered. When starting a new list, the previous
ones are automatically kept.


And the two commands are :colder (to go to previous results) and :cnewer (to go back to newer results).

P.S. "List of errors" is used in VIM originally for :make. Results of :grep are also interpreted as list of errors, what promotes grep into rank of compiler (:

Thursday, November 06, 2008

VIM tips exchange on Slashdot

Link.

Flamewars Alert. This is Slashdot after all.

Friday, October 03, 2008

[link] vim scripts

From Sander Marechal: My list of must-have vim scripts.

Sunday, September 14, 2008

Defining a user command: easy make invocation

This little thing:

command MM :exec ":make ".substitute( substitute(expand('%:p'), '\.[^\.]*$', '', ''), '^'.getcwd().'/', '', '')

defines a new VIM command ":MM" which does invoke ":make" with name of current file without extension. E.g. if you edit file "helloworld.c" and then type command ":MM" that would expand to command ":make helloworld"

Just in case I have also added stripping of $PWD (getcwd() in VIM universe) so that if you edit file from another directory, make would be called with relative path and Makefile from current directory would be used. That will not improve much make behavior, but if by chance you are using "cons", then that would work very well.

Edit1. Forgot to mention. VIM mandates that all user commands start with capital letter (all standard commands start with lower case letters). That's why I named the custom command "MM"

Tuesday, August 26, 2008

[link] 21 of the Best Free Linux Text Editors

21 of the Best Free Linux Text Editors

Unfortunately, VI and Emacs clones are not presented. Emacs itself is too bloated to be used by people who like to remain in control of their text editor - but there are number of decent Emacs clones.

From Wikipedia: Emacs clones and VI clones. VIM is listed in VI clones.

P.S. Most important Emacs shortcuts: Ctrl-g - end minibuffer editing, if any; Ctrl-x Ctrl-c - exit Emacs. Ctrl-g you need because often, after entering shortcut from another text editor, you might end-up in Emacs minibuffer (internal command line). Ctrl-x Ctrl-c will not work in minibuffer - so you need to press Ctrl-g. After exiting Emacs, type in shell "vim" and press Enter to start real text editor ^_^

Monday, August 25, 2008

[link] 100 Vim commands

100 Vim commands every programmer should know

Nice summary of vim commands.

P.S. Though apparently the people are messing up "vim" and "vi". As things stand right now, "vim" has absolutely nothing to do with "vi". Hey I worked in "vi" (on both Solaris 10 and HP-UX 11.x) - that was most frustrating 5 minutes in my life. The *NIX vendors do not even bother adding keyboard support. Solaris' "vi" proudly says that it wasn't updated since 1996.

Wednesday, July 09, 2008

little useful bit of info showcmd

How to see how long selection (visual) is?

How to see whether you have accidentally entered any numbers which would act as repeat counter for the command?

The little thing I always forget about:

:set showcmd

In right bottom corner (before ruler) little useful bits about actual state of keyboard input in normal are displayed.

Most useful is of course to now how long visual selection is, which I use often to e.g. count number of characters.

P.S. Documentation says the showcmd setting will be changed if you changed compatible mode. Shouldn't be a problem since no sane person uses compatible mode anyway.