antonio
antonio

Reputation: 11150

Piping and colon commands in Vim

I am a happy Emacs user. Anyway I find that there is a field where Vim can be better fitted to the job, this is inside the Linux terminal or Windows CLI, when you don’t want to open a separate window. There are some micro-Emacs distros for this lightweight tasks, but I still haven’t found a good one.

A typical Vim usage for me is as a more powerful alternative to less, e.g.:

 dir | vim -

Now I come to my problems/questions.

First, Vim starts from the top of the file. I’d like to position the cursor on the last screen line, but dir | vim +L - doesn’t work. I suppose because L is not an ex command. How can I overcome this?

When I finish browsing, and I want to quit I have to type: :q! five keys and with shifting (so two hands). Really a lot for a fast output check. Colon commands are very slow because they need shifting. So I wonder if there is a way to replace the colon : with a function key, say F2, or the Tab key. Also I’d like to replace the ! key with a non-shifted key, a single key like `-=[];'\,./. Is this possible? What are the drawbacks of doing so?

Edit

Note by colon replacement I mean replacing only the : symbol for every colon command, so I could type <F2>q<CR> for quit. For example in Emacs, you can assign the modifier key "Meta" (normally Alt key) to a non-standard key or remap all commands starting with the prefix Ctrl-x to a different key.

Upvotes: 1

Views: 1337

Answers (3)

244an
244an

Reputation: 1589

In the installation of Vim73 on Ubuntu there is a script for running VIM more like "less". I'm using this in situations you describe.
The script is less.sh located at (replace with your installation path) /usr/share/vim/vim73/macros/less.sh

Here is the script in case you don't have it on your server:
script /usr/share/vim/vim73/macros/less.sh:

#!/bin/sh
# Shell script to start Vim with less.vim.
# Read stdin if no arguments were given.

if test -t 1; then
 if test $# = 0; then
   vim --cmd 'let no_plugin_maps = 1' -c 'runtime! macros/less.vim' -
  else
   vim --cmd 'let no_plugin_maps = 1' -c 'runtime! macros/less.vim' "$@"
  fi
else
  # Output is not a terminal, cat arguments or stdin
  if test $# = 0; then
    cat
  else
    cat "$@"
  fi
fi

Notice that it's "turning off" all plugins.
If you want to do special things in your .vimrc when started this way you can start it using an alias with e.g.

alias lessv='use_less=1 /usr/share/vim/vim73/macros/less.sh'
# use it:
lessv some_file
# and
dir | lessv   # no need for "-"

Then you can reference it as $use_less in .vimrc, and e.g. do finish before all of the usual mappings. There will be no errors if you start vim the "normal way" (you don't have to use tests like exists("$use_less")). And if there is no other good reason to use it, it's kind of fun having a variable named use_less.

There is some help with commands when opened with less.sh, just hit h
I also found it in vim help: :h less
I tried to find the code for less.vim on Internet and use a link, but no luck. I think that the file is part of the installation though.

Edit:
Windows:
On windows in some installations there are also a less.bat to start vim more or less like less. I paste it in here if it's not part in the installation:
script C:\Program\Vim\vim73\macros\less.bat:

@echo off
rem batch file to start Vim with less.vim.
rem Read stdin if no arguments were given.
rem Written by Ken Takata.

if "%1"=="" (
  vim --cmd "let no_plugin_maps = 1" -c "runtime! macros/less.vim" -
) else (
  vim --cmd "let no_plugin_maps = 1" -c "runtime! macros/less.vim" %*
)

A final tip:
j, e, Ctrl+j, Ctrl+e, Ctrl+n are all mapped to scroll 1 line down (first placing cursor at bottom of window).
k, y, Ctrl+k, Ctrl+y, Ctrl+p are all mapped to scroll 1 line up (like less, first placing cursor at bottom of window).
To move up and down use <Up> and <Down>.

Upvotes: 3

romainl
romainl

Reputation: 196886

In Vim, L is used to place the cursor at the bottom of the window.

Use $ command | vim - +8 to place the cursor on line 8.

Use +$ to place the cursor on the last line.

You can place this line in ~/.vimrc if you don't like :q!:

nnoremap <F2> :q!<CR>

Also, what do you dislike about less?

edit

$ command | vim - +'norm L'

puts the cursor on the last visible line.

You can use any key instead of : but Vim uses more or less the whole keyboard. You'll have to choose the replacement key wisely and use something like the following in your ~/.vimrc:

nnoremap <F2> :

Upvotes: 3

Vaughn Cato
Vaughn Cato

Reputation: 64308

In Linux, you could do:

ls | vim +$ +'map <F2> :' +'map <F3> !' -

Upvotes: 0

Related Questions