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


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.

Thursday, May 29, 2008

[off topic] Darn good quote

It's so much easier to suggest solutions when you don't know too much about the problem.
    -- Malcolm Forbes

[link] Sweeten your Vim with Cream

Simple Vim intro by Linux Magazine.

Monday, May 26, 2008

My personal minimal .vimrc

Another upgrade, another OS, another update... Well, new home directory - consequently new .vimrc.

Here is N hours into coding minimalistic usable .vimrc:

filetype plugin off
syntax on
imap <Insert> <Nop>
set bs=2 ruler title comments= nowrap
set cindent cinkeys-=: cinkeys-=0#
colo torte
let loaded_matchparen = 1
au BufReadPost * if line("'\"") > 0 &&
\ line("'\"") <= line("$") | exe "normal g'\"" | endif

I hope this time, Blogger/BlogSpot hadn't eaten any characters.
The set comment= creeped in before I have realized that filetype plugin is still on.

Saturday, May 24, 2008

Normal command from Insert mode

Always forget the very useful shortcut which is apparently intended for "Vim Easy":


When in insert mode (and, by definition, in easy vim you are always in the mode) to insert command from Normal mode, you press first ^O and then start typing the command.

e.g. to delete to the end of line press ^O followed by Shift-d. Pressing : you can also access all the ex commands too - but that will leave insert mode.

Sunday, March 30, 2008

Inserting command output from a function

How would I use a vim function to insert the output of these commands at the current cursor position?

Well, that would look like a crude hack, yet here it goes:

:function InsertCmd( cmd )
: exe ':silent !'.a:cmd.' > /tmp/ 2>/dev/null'
: let l = readfile( '/tmp/', '', 1 )
: exe "normal a".l[0]
: redraw!

:imap <silent> <F5> <Esc>:call InsertCmd( 'hostname' )<CR><Insert>
:map <silent> <F5> :call InsertCmd( 'hostname' )<CR>

The function InsertCmd(cmd) runs in shell given command and redirects its stdout to a file. Then using readfile() function, first line of file is read into list l. Finally using :normal command and a (:help a) command we put the first line of output into buffer.

The two mappings for F5 key - for insert and for command mode show how to invoke the function to insert output of "hostname" into current cursor position. ":imap" probably needs extra <Right> on the end since due to switch between insert/normal modes cursor goes one position back.

Edit1. Better way to do it, from SO.