Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to tweak my linux environment so that when I type a text file name on the command line and hit enter the file should be opened in Vim.

$: /tmp/file.txt

should open the file in Vim. This is similar to what happens on windows (where a text file is opened in notepad.)

share|improve this question

migrated from stackoverflow.com Apr 6 '11 at 0:29

This question came from our site for professional and enthusiast programmers.

There is a way to do this in zsh, using alias -s, but I haven't heard of bash being able to do this. I use this in bash for .txt, .pdf, e.g. Note that this works when a file has the right extension. Extensions are not necessary in Linux.

share|improve this answer
    
Perusing the zsh man page(s): this would work: alias -s txt=vim. – glenn jackman Apr 5 '11 at 19:14
    
edited, thx. Yes -g is for global alias; I use : for "| less", e.g., which I can add to commands. – Henno Apr 5 '11 at 20:07

Since Linux recognizes files by their content and not by their name there is no reliable way of doing this. You can try binfmt_misc, but that will most likely not work properly.

share|improve this answer

If you're using Gnome, you can alias gnome-open to g or something and use that to open files with the the default handler used by Gnome. Would that work for you?

The way you're suggesting sounds quite annoying. What if /tmp/file.txt was executable? Would you want it to open or run when you typed it's name and hit enter?

share|improve this answer

In cygwin, I use: alias start=cygstart
In ubuntu, I use: alias start=xdg-open

So, I can use start /some/file/name to do the default action in a Windows cmd shell or in a bash shell.

share|improve this answer

I think you can't. The first part of the line is the command it self. At least if you just what something very short to open a file using vim you can set an alias: in your .bashrc if you use bash:

alias v="vim"
share|improve this answer
    
You need to remove the spaces around the equal sign. – glenn jackman Apr 5 '11 at 19:08
    
@glenn thank you. – BenjaminB Apr 5 '11 at 19:55

Ubiquité is correct that what you want is not supported in bash. However, you can use the netrw support in vim 7 to browse directories!

Try this:

alias v="vim ."
v

A directory listing will show up. Now you can do the following:

  • Use j/k to go up & down
  • Place cursor on a dir & press enter to explore it
  • Place cursor on a file & press enter to edit it
  • Use - to go up a dir
  • Use D to delete a file or dir
  • Use R to rename a file or dir
  • Use s to change the sorting of items (name, time, size)
  • Use d to create a dir
  • Use i to cycle between listing formats (thin, long, wide, tree)
  • Use Ctrl-l to refresh a directory listing

You can do a lot more with netrw too! It can be used for remote file browsing over ssh. For more info, check the help file with F1.

share|improve this answer
    
You need to remove the spaces around the equal sign. – glenn jackman Apr 5 '11 at 19:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .