Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move File within Vim

Tags:

Is there a way to move a file within Vim? E.g. I opened a file foo/bar.txt in Vim. I know 2 ways to move this file:

First solution:

  1. Delete the buffer with :bd bar.txt
  2. Perform the move on the shell with mv foo/bar.txt foo/bar2.txt
  3. Load the file in vim with :e foo/bar2.txt

Second solution:

  1. Close Vim, so all buffer where closed.
  2. Perform the move on the shell...
  3. Start Vim and load the file.

But these two solutions are embarrassing. I know, there is a plugin for renaming files vim-enuch, but isn't there a Vim way for performing such basic functionality?

like image 903
SvenK Avatar asked Jun 04 '12 16:06

SvenK


People also ask

How do I change the location of a file in Vim?

You can enter :pwd to display the current working directory. This is where your file will be saved if simply enter :w filename . You can change the working directory with :cd path/to/new/directory .

How do I move the top of a file in Vim?

Type "gg" in command mode. This brings the cursor to the first line.

How do I move files in Netrw?

Then change directory, select file(s) (see netrw-mf), and press "mc". The copy is done from the current window (where one does the mf) to the target. If one does not have a target directory set with netrw-mt, then netrw will query you for a directory to copy to.

How do I move a line in Vim?

In normal mode or in insert mode, press Alt-j to move the current line down, or press Alt-k to move the current line up. After visually selecting a block of lines (for example, by pressing V then moving the cursor down), press Alt-j to move the whole block down, or press Alt-k to move the block up.


2 Answers

You could also use netrw (the default file explorer) rename functionality.

  1. Open netrw with :E
  2. Move your cursor to the line with the file you intend to rename, in this case bar.txt . You move to the file in question using h,j,k,l or you can search for it with / (e.g. /bar.txt)
  3. Hit R. You will then be prompted for a new filepath. When done entering the filepath hit <CR>
  4. Move your cursor to the new file and open it with <CR>

While this solution may not be as quick as using vim-eunch, it does allow you to see the project's structure as you rename the file. This will also allow you to move multiple files at once.

For further reading run :help netrw-move

like image 131
Douglas Anderson Avatar answered Sep 21 '22 14:09

Douglas Anderson


There is no atomic way to move a file like that, but this should be close:

function! MoveFile(newspec)      let old = expand('%')      " could be improved:      if (old == a:newspec)          return 0      endif      exe 'sav' fnameescape(a:newspec)      call delete(old) endfunction  command! -nargs=1 -complete=file -bar MoveFile call MoveFile('<args>') 

Now you could say:

:MoveFile file2.txt 

To rename to file2.txt

:MoveFile %.0 

to move file2.txt to file2.txt.0

like image 29
sehe Avatar answered Sep 18 '22 14:09

sehe