1

I tend to edit particular text files much more often than others. There might be 30 or 40 or so different files I tend to edit most often. Plus there are others that kind of rotate in and out as things I am temporarily working on. Even though I might infrequently edit them, during periods of work involving that file I might frequently access the file many times over a short time period, say a week.

The problem is how to open these files quickly in Vim.

Currently I generally just type :e then manually type in the full path in the command line which is pretty tedious. Also, if I don't remember the path, then I end up either leaving Vim altogether and going to Caja to find the file, or using :Sex.

The only shortcuts I currently use is that I have about 10 aliases for going to particular directories. So, for example, if I am working on a project where several needed files will be in a commonly used directory, then I use the alias to change to there, then open Vim. After that, I don't need to type the full path. I just have to enter the file name. However, this method only works for directories where I have big projects going, which is only a few of them. Most of the files I edit are in one off places. Also, I don't want to try to remember 50 different aliases. I have a hard enough time just remembering the 10 I already have.

What I would really like would be a hotlist of my most commonly used files with a numeric or alphabetic key. So, I could, for example, press a hotkey and a multi-column list comes up of my favorite files. Then I would press, say, 'C' to open the file keyed as 'C' or whatever. That would allow me to open any of my favorite files in 2 keystrokes, which would be the ideal.

6
  • There are a lot of plugins that do fuzzy file find on your filesystem (+ added things like most recently used files, buffers, etc) -- maybe those would suit you? For example github.com/habamax/vim-select Otherwise you can implement your own "commonly used files" popup window...
    – Maxim Kim
    Commented Jan 15, 2022 at 9:47
  • 1
    or maybe :browse old would help you a bit too
    – Maxim Kim
    Commented Jan 15, 2022 at 9:55
  • If you're open to install fzf, then I explain in this answer how to configure it to search a particular subset of directories on your system. This effectively means I can always find my most commonly edited files in about 5 keystrokes.
    – mattb
    Commented Jan 15, 2022 at 11:12
  • @MaximKim That is useful, but there seems to be no easy way to either sort or search that list. Commented Oct 28, 2022 at 12:14
  • @mattb That is a useful tool, however, I have a big directory system so searching through the whole thing can be time consuming. Commented Oct 28, 2022 at 12:15

2 Answers 2

1

There are countless possibilities using plugins and VimScript'ing. But let's focus on "built-in stuff" only.

  • cdpath instead of 10 aliases for going to directories

This is an option that works much like one in /bin/sh. That is, if you have set cdpath=/some/where,/else then instead of typing :cd /some/where/dirname you can type just :cd dirname.

  • marks for remembering up to 26 files plus last file from previous Vim run

To open the last file being edited press '0. This should work out-of-the-box provided that :h viminfo file was loaded properly. The bookmarks "0"-"9" are auto written into it.

Press mA - mZ to set global marks. Press 'A to go to mark "A" as usual. The global marks keep both line/column number and file name. And they persist across Vim runs, as they are also stored into viminfo file automatically. Use :marks A-Z to see their values.

  • oldfiles builtin MRU file list

This is also restored from viminfo. :oldfiles to browse through the list. :e#<42 to open file #42 from it. (BTW. Any command could be typed right from :h hit-enter-prompt, i.e. while still having that list before your eyes).

  • find to search for file through the directory list

The option :h 'path' is often used as buffer-local to support gf command and such. But there's no rule against using it globally. Do :set path=/dir1/dir2 and then you may :find foobar.txt instead of usual and longer :edit /dir1/dir2/foobar.txt.

You can also force searching through the whole subtree by :set path=/dir1/**. Though it could result in a great deal of disk I/O.

BTW. Plain :edit also supports :h starstar, so this could even work :e /dir1/**/foobar.txt. Unless E77: Too many file names happens when there is more than one foobar.txt sitting under /dir1.

2
  • Generally I recommend against using :find as a fuzzy finder because it requires setting path, which is used for things like gf and many many other commands
    – D. Ben Knoble
    Commented Jan 15, 2022 at 13:46
  • @D.BenKnoble Yes, and I mentioned this too. But personal preferences may vary much.
    – Matt
    Commented Jan 15, 2022 at 14:36
1

I personally use a combination of:

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.