Vim Tips Wiki

Quick peak at files

Redirected from VimTip1218

1,624pages on
this wiki
Add New Page
Talk0 Share
Tip 1218 Printable Monobook Previous Next

created 2006 · complexity intermediate · author Suresh Govindachar · version 6.0

In an operating system's command-line terminal, one can get a quick peak at a file using commands such as more, cat, head etc. In Vim, one way to peak at a file would be to open it in a new buffer -- but there is a way to peak at a file from Vim without having to open it in a buffer, browse it and close the buffer -- one can just view it in Vim's command line! This mode of viewing is facilitated by Vim version 7's support for scrolling (see :help new-scroll-back).

So to peak at a file, we just echo its contents (:help :echo).

Simple implementation:

One way to implement the idea would be:

:new|r <file_name>|1d|exec 'normal "ayG'|q!|echo @a

One could also do :echo system('cat foo.bat'), but we are trying to avoid explicit system calls. In version 7, Vim supports readfile(). But the result of readfile() is an array of lines -- and these lines would need to be joined to enable viewing; so we have:

:echo join(readfile('foo.bat'), "\n")


Here are two applications that build on the idea presented here.

Yasuhiro Matsumoto's calendar utility script#52 is written to display the calendar in a buffer. For a quick peak at the calendar, one can modify the plugin to support echoing the calendar in Vim's command line, and make a simple map (such as of a RightMouse click) to trigger the display on the command line.

I have the following in my vimfiles\after\ftplugin\index.vim to speed up previewing emails using my mail user agent utility script#1052.

if(v:version < 700)
  nnoremap <buffer> <Space> :exec "let @a='r '.expand('%:p:h').'/'.substitute(
        \silent exec 'normal\ d}\"ayG'\\|q!\\|echo\ @a"<CR>
  nnoremap <buffer> <Space> :exec "let alist=readfile(expand('%:p:h').'/'.
        \while(remove(alist, 0) != '')
        \\\|echo join(alist,\"\n\")"<CR>


Most normal mode commands have a command line counterpart. For example,

exec 'normal "ayG'

could be replaced by

%yank a

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Also on Fandom

Random Wiki