How to use my vim configuration in less?

4

1

Quite some programs have commands that output with less(e.g. git blame filename). I would like to use my vim configuration(e.g. <C-j> instead of using Pg Down to go down a page) whenever less is used. How do I set this?

According to this thread and the less man page putting export EDITOR=vim and export VISUAL=vim in my ~/.baschrc should use vim for less but I don't see any change after reloading a terminal session and launching less (I can't use my page down hotkey for example).

Bentley4

Posted 2013-03-30T20:22:46.470

Reputation: 1 588

Your question is ambiguous. Do you want to use Vim in place of less, or do you somehow want your Vim configuration files to affect less's behavior? The former should be possible, but the latter isn't. – Heptite – 2013-03-30T21:08:56.563

I wasn't sure if commands like git blame filename hardcoded the use of less or that they just passed output to the pager you are using. I just wanted to use my vim configuration when using a pager, wether its with less or not with less. – Bentley4 – 2013-03-30T22:27:04.547

Answers

3

The $EDITOR and $VISUAL variables define the editor to use (e.g. when you use the "edit" command inside less).

When programs run less, they run it as a pager, not as an editor – they're looking at the value of $PAGER.

Install vimpager, then:

export PAGER=vimpager

user1686

Posted 2013-03-30T20:22:46.470

Reputation: 283 655

Awesome! Every time I launch vimpager though I get E21: Cannot make changes, 'modifiable' is off: retab Press ENTER or type command to continue. Any idea how I can set 'modifiable' to on or just simply stop displaying this message? – Bentley4 – 2013-03-30T22:22:37.823

I've put :set modifiable in my vimrc and it keeps outputting the same message. – Bentley4 – 2013-03-30T22:31:15.127

Nevermind, it worked after I commented out retab in my .vimrc. Don't know why though. – Bentley4 – 2013-03-30T22:36:12.540

1

If you don't want to install vimpager as the accepted answer recommends, there's two hackish sort of workarounds that I've found to just send stdout straight to vim.

1) As a non-global workaround, you can pipe everything that would go to less to
<command_outputting_to_less> | vi -.

2) More globally, you can set
export PAGER='vi -'.
A problem I ran into with this is that the cmd that's sending output to the pager may put color codes into it because it thinks the pager should be able to handle color codes. To correct this, you'll need to strip out the color codes either by a different program or the output program may have the option to remove color codes. For the one I was using (git), I changed this to get it to output correctly git config --global color.ui false.

horta

Posted 2013-03-30T20:22:46.470

Reputation: 359

0

I am not sure what you are exactly after but there are a lot of vi-like key bindings to use with less in its original installation. At least it is so on my Ubuntu Jaunty box. But if you are missing some particular functionality, you can use lesskey program to reassign your key bindings. Running this program will create a file named .less under your home directory. It will be a text file that you can cat or vi or even less to see the contents.

Check the man pages of less (look for KEY BINDINGS) and/or lesskey command to accomplish what you are trying to accomplish.

MelBurslan

Posted 2013-03-30T20:22:46.470

Reputation: 835