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

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.

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.

1 I didn't know about | or about %t. Thank you! This seems like a really good solution, regardless of whether it's a single command.

– ClosureCowboy Feb 27 at 8:33.

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().

Thank you! I hate to be a pest, but you could briefly explain how this works? – ClosureCowboy Feb 27 at 4:04.

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 /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 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.

Thank you for those links. Do your solutions allow me to avoid typing the name of the file each time? – ClosureCowboy Feb 26 at 23:16 the original one didn't, but see my edit – Wipqozn Feb 27 at 2:04.

Why not just try: :w disaster-stories/apple-pie.txt.

I could, and perhaps I should. But I think it would be convenient to be able to jump to a corresponding file without having to specify its name each time. – ClosureCowboy Feb 26 at 22:48 @ClosureCowboy: If the two files are supposed to be equivalent anyway then why not just use a symlink or a hardlink?

– Robert Massaioli Feb 26 at 22:50 1 they're not equivalent, just related. – Karl Bielefeldt Feb 26 at 22:53.

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 Of course, if you're editing 10 Makefiles simultaneously, this doesn't scale well.

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. Tells vim to run a command such as "cd" or "ls".

-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". -Note: the first line, #!

/bin/bash tells Unix/Linux that this is a script.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions