Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

add comment

6 Answers

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
add comment

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
add comment

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
add comment

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
add comment

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. –  Ubiquité Apr 5 '11 at 19:55
add comment

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.