开发者

In Vim, open a file with the same name as the current file, but in a different directory

I know this is a beginners' question!! Let's assume we have two directories:

recipes/
  apple-pie.txt
  blueberry-muffin.txt
disaster-stories/
  apple-pie.txt
  blueberry-muffin.txt

Let's also assume that Vim's current directory开发者_开发技巧 is set to the parent directory of both recipes/ and disaster-stories/.

Question:

If I am editing recipes/apple-pie.txt, I want to be able to enter a custom command such as :dsstr, causing the current file to be saved and disaster-stories/apple-pie.txt to be opened. How could I do this?


You can chain commands by separating them in the vim commandline with a pipe character.

:w | e disaster-stories/apple-pie.txt

Vim will expand % to the currently edited filename in commandline commands. You can add some modifiers to manipulate the filename in different ways (see filename modifiers in the manual). For example, %:t is the tail (last component) of the filename; if % gives recipes/apple-pie.txt then %:t gives apple-pie.txt.

:w | e disaster-stories/%:t

Usually when I want to do this kind of thing I don't bother with custom key-bindings. If you've used a command like this recently, you can type a prefix of it (:w |) and use the up-arrow key to recall it from history conveniently.


Put this on your vimrc:

command! -bar Dsstr w|exec 'edit '.expand('%:p:h:h').'/disaster-stories/'.expand('%:t')

Then you can use :Dsstr to change to that file.

This is not flexible at all, but I'm not sure what you need, so I can't go any further.

EDIT Now a short description with pointers to Vim's help for more details:

  • command! : Create a command and overwrite it if it exists. :help :command
  • -bar : Allow | to concatenate command. :h :command-bar
  • Dsstr : User commands must start with a capital.
  • w| : Write, then execute the next command. :h :bar
  • exec : Execute the arguments as an ex command. :h :execute
  • expand() : See :h expand()


EDIT: Ok, I have a solution for you. I'm not sure how to write a function in vim, but you can just write a shell script to do the work for you. I tried writing a script that would save your current file, quit, than switch to the new file, but ran into some errors with that. But this will allow you just type:

 :w | ! scriptName

! tells vim to run a command such as "cd" or "ls".

Steps to make the shell script:
-cd
-mkdir bin (if bin does not already exist in ~)
-cd bin <- You want to make the script in ~/bin so linux can find it -vim nameOfScript (this is what you'll type to run the command. For example, if you named it "vimScript" it would run you typed "vimScript". Ideally you'll want to use something short like "vc" (vim change)

-Enter the following text:
#!/bin/bash
vim -c e {)

-Note: the first line, #!/bin/bash tells Unix/Linux that this is a script. vim -c e pathToFile makes it run the command "vim -c e pathToFile" -save and quit
-chmod +x nameOfscript <-This makes the script executable
-Enjoy! You can now you can now save your file and move to the next by typing:

 :w | ! nameOfScript  

A few things of note: This only works one way. You'll need to either write two separate commands, one for switching to one file, and one for switching to the other, or add more on to the script. Adding more on would be the best way, since it would allow you to more easily add more files into it later.

Also, I would suggest making backups of both of the files, just in case you make a mistake creating the script.

If you have any questions, just ask! Some more links below:

Here are some links on writing shell scripts:
http://linuxcommand.org/wss0010.php
http://linuxcommand.org/writing_shell_scripts.php
http://www.freeos.com/guides/lsst/

Also, these two links could prove helpful in the future:

http://vimhelp.appspot.com/vim_faq.txt.html
http://vim.wikia.com/wiki/Vim_Tips_Wiki


I think you want to go learn about buffer management in vim. Once both files are loaded as buffers, you can probably toggle between them with :b appl<tab> Of course, if you're editing 10 Makefiles simultaneously, this doesn't scale well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜