Thursday, September 11, 2008

Got a Question?

I had a reader of Daily Vim contact me yesterday about the possibility of periodically submitting questions regarding Vim best practices, etc. I told him that I'm very happy to assist in answering questions any Daily Vim readers might have but if I don't know the answer, I'll post the question here anyway, so someone smarter than me can ;-).

So, if you have a question regarding Vim, Linux, scripting or something along those lines, feel free to post them under this thread or email me directly via tinymountain at gmail dot com. I'll post the question and possibly an answer to the blog, and as usual, feedback is welcome.

To kick things off, I have a question of my own. Does anybody know of a Vim plugin or some method to spontaneously highlight every other line of a file? I frequently use Vim as a pager inside of MySQL, and this would be very handy for eyeballing all the values on a specific row.

UPDATE:

I solved this problem thanks in part to an initial suggestion to use syntax highlighting to perform the highlighting. Here's my finished solution. It seems to have issues with language files with pre-existing syntax highlighting, but for it's intended purpose (use within the mysql pager), it works beautifully. Just do "pager /usr/bin/vim -R -" inside the mysql client, add the code to your vimrc, run a query and hit F2.


hi AlternateLine ctermfg=grey ctermbg=darkblue

function! HiLine(lineno)
let tmpline = escape(getline(a:lineno), '/\[]')
exec 'syn match AlternateLine /.*' . tmpline . '.*/'
endfunction

function! DoHighlight()
global /^/if line('.')%2|call HiLine(line('.'))
normal 1G
endfunction

map <F2> :call DoHighlight()<cr>


5 comments:

Anonymous said...

For a dead simple solution to the highlight question you can do /\n.*\n.

If you want to specify the color you can do
:syn match OtherLine "\n.*\n"
:hi def OtherLine guibg=#e0e0e0

If you use it enough to map a command for it, the second solution has the advantage that it doesn't highlight the carriage returns, but it also doesn't seem to update dynamically with the text.

Hope this is useful to you. I've definitely found some good tips on here.

Travis Whitton said...

Thanks very much for the suggestion. It's very close to what I'm looking for but doesn't seem to reliably alternate. I'll use it as a start and see where I can get.

Anonymous said...

I have a suggestion for a tip you could give out. 〈C-x〉〈C-f〉 will complete a file name that you are typing in, kind of like tab completion on the command line. Very handy for avoiding typos in file names and something that I have wanted to be able to do for a long time but have not seen anybody mention.

Anonymous said...

I have an unrelated question - anyone know how to wrap vimgrep in a function e.g.

:vimgrep /foo/ **/*.pl

becomes
:call Vg(foo, pl)

Dotan Cohen said...

Travis, the feature that has me married to Kate (KDE text editor) is the file browser with bookmarks built in. Kate allows users to browse remote filesystems from within the app just like they could browse the local filesystem in a file manager. Furthermore, there is a bookmarks feature where users can bookmark specific directories / files on remote servers. Anythink like those two features in VIM?

Thanks!