-2

Currently all I have to do is type vim file_name and it will make that file in the current directory (usually user because that is where the command line start at if you don't use the cd command). It would annoy me tremendously to have to move to the documents folder every time I want to open a vim file. Would there be a way to do this automatically? Maybe using aliases?

My vim version info: VIM - Vi IMproved 7.4 Huge version with GTK2 GUI

1
  • // , vim directory/another_directory/yet_another_directory/file_name In the case of using the Documents folder, you could use the following: vim Documents/file_name. I like your alias idea, though. For someone new to Linux and the idea of file paths in general, this is a good question. Commented Dec 29, 2015 at 18:25

4 Answers 4

4

First of all wherever you open vim, you have access to the whole filesystem from within vim.

If you open vim without parameters or if you open a new buffer inside of vim with :new you can then save it to the drive with :w \path\to\file

Then to access to those files you have a lot of different options. For example :

  • netrw which is the built in file Explorer of vim, you can call it with :Explore. More info here

  • NERDTree an excellent file explorer for the beginners because it reminds a lot of the classical system file explorers... Ones might say that it is not "the vim way" to navigate files but it is worth a try. See here

    EDIT: I wrote that 6 years ago, when I didn't knew better. Since then I've seen dozens of questions showing how this plugin confuses newcomers (including myself), how buggy it can be and how many bad practices it pushes. Please refrain from using NERDTree especially if you are new to vim. I strongly encourage you to read this article to convince you that this is not a good idea. If netrw doesn't fit your needs you could have a look at vim-vinegar which tries to improve it a bit.

  • ctrlp a really good fuzzy finder which can be confusing for beginners. See here.

    EDIT If you want to go the fuzzy way in 2021+ I recommend fzf instead because it is faster, however it is a much larger tool which can be overwhelming for a new user.

Of course you have a lot of other options but Google should be your friend to find them.

Also I don't know your workflow and what kind of files you're editing but that sounds pretty weird to edit your files in only 2 folders (user and documents if I'm right) so your answer with an alias to change of directory before opening vim is far from being ideal: if you need to edit files in another directory will you create another alias? And what if you use 10 or 100 directories? That's why I presented different file exploring solution.

4

You could use

set autochdir

in the vimrc to make the working directory that of the current file. Apparently you need to be careful with this option and plugins.

1

All credit for this answer goes to heemayl from Ask Ubuntu. Apparently that is where this question belonged.

No, you can not do that using shell aliases. You need to use a function.

Here is a simple function to do the job :

dim() {
cd /home/jason/Documents
vim "$1"
cd -
}

The function dim will take a file name as argument. You can put this code snippet at the end of your ~/.bashrc file and then run it as:

dim file.txt

Replace file.txt with any file name you want.

To run it from the current shell session, source the ~/.bashrc file first :

. ~/.bashrc
3
  • The question belongs here just fine, but I think others have got the sense that you're not willing to work with Vim. I have asked similar questions (vi.stackexchange.com/q/258/205, vi.stackexchange.com/q/2009/205), and got nice answers.
    – muru
    Commented Jul 6, 2015 at 8:17
  • Notes: use cd - so that your working directory is restored. cd without any arguments will take you back to the home directory, cd - will take you back to wherever you were.
    – muru
    Commented Jul 6, 2015 at 8:18
  • 1
    Also consider using vim "$@" so that you can pass additional arguments or commands to Vim.
    – muru
    Commented Jul 6, 2015 at 8:19
-1

A hot-fix is to just put in the following alias:

alias dim='cd /home/usr/Documents/; vim'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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