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 am using vim to edit config files on newly-built linux servers. On these boxes I have a default vim setup (RHEL 6.2).

While editing these files, I'd like to avoid fat-fingering directory & file paths whenever possible.

Suppose I have a file foo.conf opened in the editor, the contents of which I've just entered:

myfoo=

The cursor is currently to the right of the = in this file. I'd like to use vim to insert the name of a directory here. Suppose it's a directory that exists: /etc/foo, and that I'm lazy and so would like to use as few keystrokes as possible.

Can get vim to do this?

I cannot add any plugins to vim, nor can I change its configuration.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Read the output from ls and format like this:

  1. ESC
  2. :r!ls -d /etc/fTabEnter
  3. <move one line up, e.g. K or >
  4. Shift+J
  5. X

Explanation:

  1. Goes to "command" mode (technically "normal" mode).
  2. :r reads input from a source and inserts it after the current line, ! interprets the following as a shell command and executes it.
  3. Go up.
  4. J joins a line with the next, and also inserts a space in between.
  5. Remove the extra inserted space.
share|improve this answer
    
Awesome, exactly what I wanted! Thanks a lot. –  John Dibling May 24 '12 at 20:15
    
Shift+x? That is not standard, is it? It doesn't work here. I'd do :r!echo /etc/f <tab completion> <Enter> kJx –  Daniel Andersson May 25 '12 at 7:27
    
Sorry about that, I fixed the answer there was a typo in the <kbd> syntax –  Bram May 25 '12 at 7:36
    
Ahh, that makes sense :-) . It is exactly the same as my version then, except the echo trick which is completely arbitrary. –  Daniel Andersson May 25 '12 at 7:40

Use omnicompletion: hit <C-x><C-f>, choose, <CR>, repeat…

share|improve this answer
    
Cool, though this appears to work only for files in the current dir? I need to look into this. There's a vim tip page on this as well. –  Bram May 25 '12 at 7:41
    
No. After / it will list the first level of directories of your filesystem. After ../ it will list all the directories contained in the directory above. –  romainl May 25 '12 at 8:39

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.