25

Currently, ranger will only open text files with nano, and I want it to open them with vim.

As per the Arch Wiki I have tried adding

has xdg-open, flag f = xdg-open "$1"
ext txt = vim "$@"

to rifle.conf, but that didn't work.

GAD3R
  • 63,407
  • 31
  • 131
  • 192
Display name
  • 1,267
  • 2
  • 14
  • 22

4 Answers4

25

You don't have to edit .bashrc, only the ranger config. Here is how:

After startup, ranger creates a directory ~/.config/ranger.

You want to edit the rifle.conf file. Rifle is the program that chooses what to open files with. To copy the default configuration for rifle to this directory, issue the following command:

$ ranger --copy-config=rifle.conf

(Alternatively, add all of rangers config files with $ ranger --copy-config=all)

In rifle.conf, find this part. Change the $EDITOR variable on the two lines below:

#-------------------------------------------
# Misc
#-------------------------------------------
# Define the "editor" for text files as first action
mime ^text,  label editor = $EDITOR -- "$@"
mime ^text,  label pager  = "$PAGER" -- "$@"
!mime ^text, label editor, ext xml|json|csv|tex|py|pl|rb|js|sh|php = $EDITOR -- "$@"
!mime ^text, label pager,  ext xml|json|csv|tex|py|pl|rb|js|sh|php = "$PAGER" -- "$@"

Change it to whatever you want to edit text files with, like vim. I use Kakoune, so I change it to kak:

#-------------------------------------------
# Misc
#-------------------------------------------
# Define the "editor" for text files as first action
mime ^text,  label editor = kak -- "$@"
mime ^text,  label pager  = "$PAGER" -- "$@"
!mime ^text, label editor, ext xml|json|csv|tex|py|pl|rb|js|sh|php = kak -- "$@"
!mime ^text, label pager,  ext xml|json|csv|tex|py|pl|rb|js|sh|php = "$PAGER" -- "$@"

This was done on ranger version 1.8.1.

askor
  • 351
  • 3
  • 3
  • 3
    In later versions (currently I'm using 1.9.3) command to copy the config is `ranger --copy-config=rifle`. – Ava Feb 03 '21 at 11:33
22

I figured it out.

If you are using BASH, add the following to your .bashrc :

 export VISUAL=vim;
 export EDITOR=vim;

otherwise, follow these instructions

Display name
  • 1,267
  • 2
  • 14
  • 22
  • Also you can set an alias to set these env. variables and start ranger if you only want to use vim when you open a file with ranger. – Dimitar May 29 '23 at 12:12
10
  • Open ranger, press S, that will open ranger's shell
  • Then enter select-editor, it will show a numbered list of aveilable options and you have to enter the numer of the option that you want.
AdminBee
  • 21,637
  • 21
  • 47
  • 71
gogor44
  • 101
  • 1
  • 3
1

It is also worth considering the difference between pressing Enter and pressing E on the file that you want to edit

export VISUAL=vim;
export EDITOR=vim;

Though this solution might work when you press E on the file but pressing Enter may vary.

AdminBee
  • 21,637
  • 21
  • 47
  • 71