Vim: How can I create a file after "$ vim file.tgz"? - vim

I want to create a file to tarball, without explicitly opening/extracting it but directly by using Vim. Is that possible?
$ vim file.tgz
:e someNewfile
:w! # how can I create here a file?

Vim handles tar and derived files (including .tgz) using a vimscript called tar.vim. You can see tar.vim's documentation by typing :help tar<CR> inside vim. According to that documentation:
When one edits a *.tar file, this plugin will handle displaying a
contents page. Select a file to edit by moving the cursor atop the
desired file, then hit the <return> key. After editing, one may also
write to the file.
Currently, one may not make a new file in tar archives via the plugin.
So you can edit a file which is already in the tar, but you cannot currently add new files using a vanilla setup of vim.

Related

Modify some text of a file in a zip file

On linux, I would like to modify a file that is inside a zip without having to extract it. The file is in any possible extension.
Here's an exemple.
test.zip
|---hello.someextension
|---bye.someextension
The file hello.someextension contains following text: Hello, this is a test.
What I would like to do
Modify the word test in the hello.someextension file to be "gift" instead, for instance.
Modifying the text is not really a big deal, but the issue I'm facing is that I cannot edit a file that is inside a .zip. I tried via VIM and here's an exemple output:
ÅÍ.PE¥&ö$kpì`w_OËŽ=“XÖ¸m† 86=šoÔRw«Õºxÿ¯Ûiö²X
Vim supports editing zipped files out-of-the-box. If it doesn't work for you then you have a local problem of some sort.
Check if it helps to bypass your faulty vimrc (e.g. vim -u NORC -N), or to re-install the whole Vim package etc.

How do I go to a file in vim/nerdtree?

I'm trying to transition to vim, but I'm having a hard time mapping over some functionalities in pycharm over to vim.
The first being how do I directly go to a filepath. In pycharm, I believe it is cmd-shift P. You'll type the file-path and it'll take you there. I think there's auto-complete too?
Like -- I know that there's a .css file I want to access. So I'd instinctively start typing: cmd shift p .css and this would return the .css files.
How do I do that in vim?
Thanks!
:edit is the most basic command for editing an existing file.
:edit <your file name>
To get a list of all the files ending in ".css" use :edit e *.css and then press Ctrl+d. See :help c_CTRL-D in Vim for more information.
:find <file> is a more powerful version of :edit. It searches for <file> from the directories listed in your path option. For example, if your current directory is project and the value of the path contains
/path/to/project/**, then :find file.css will search all the subdirectories of project for the "file.css".
There is also a plugin called "ctrlp.vim" that should be similar to what you used in pycharm.
For more information about file navigation, I highly recommend reading "Death by a thousand files", an excellent article by Romain Lafourcade.

How to get to long directory quickly when writting code in VIM

I am writing Bash script using VIM. I need to cd to a directory and run the command tool. The command tool is deep inside the directory. How do I quickly cd to that directory instead of manually typing the directory out in VIM ? In terminal prompt, I can get to the directory quickly using tab. It does not work in VIM.
Thanks
ffl3883
You can change to the currently edited file's directory with :cd %:h; see :help filename-modifiers. Likewise, if you trigger the tool from Vim :! % can do this quickly (and repeat with :!!). Or just :set autochdir, so that the current directory within Vim always follows the currently edited file (and you can then just reference the file via ./).
When typing file paths in vim (as I often do for shell scripts), I find filename-complete invaluable. Simply type <C-X><C-F> in insert mode.
N.B. It does not work in all cases (generally vim prefers the path to be a separate WORD), but a quick edit-complete-fixup isn’t terrible.

Open a docx file as a zip in vim

I've got a docx file, which is really just a zip but since the extension isn't .zip, vim fails to recognize that, and opens the raw data.
I've tried opening the file and setting the filetype to zip, :set filetype=zip, I've tried opening the file as a directory, :e path/to/file.docx/ and I've tried opening the file
Is there any way that I can force vim to open the docx as a zip?
Naturally, within seconds of asking I do a :help zip and find the pi_zip plugin which explicitly tells me:
Apparently there are a number of archivers which generate zip files that
don't use the .zip extension (.jar, .xpi, etc). To handle such files,
place a line in your <.vimrc> file: >
au BufReadCmd *.jar,*.xpi call zip#Browse(expand("<amatch>"))
One can simply extend this line to accommodate additional extensions that
should be treated as zip files.
Which I can use to add the filetype to that list, and then also showed me
au BufReadCmd *.docx call zip#Browse(expand("<amatch>"))
or alternatively
call zip#Browse(expand("/path/to/file"))
which I can use explicitly if I don't want to always open a docx like that.
If error on writing: check file permission, maybe it root:root.
I just started to use https://github.com/lbrayner/vim-rzip, which also provides support for browsing zip-format files recursively, meaning if you have a zip inside the zip it also opens and you can navigate further down the tree.
You can define any file extension in the .vimrc, like this:
echo "let g:rzipPlugin_extra_ext = '*.jar,*.war,*.ear'" >> ~/.vimrc

gvim: change the default working directory

when I open gvim using Alt+F2 it takes as its default working directory my home folder.
How can I change the working folder after or while running gvim? can i pass that folder as a parameter when open gvim?
You could use a shortcut.
The simplest way, though, would be to
:edit $MYVIMRC
append a line
cd /home/user/my/work/dir
save (optionally execute :w|source % to immediately reload)
Inside vim
use
:pwd
:cd some/other/dir
To view/change current working directory.
Use e.g.
:cd %:h
to change to the directory containing the file loaded in the active window.
If you need/want to do this often, consider just setting 'autochdir'
:se autochdir
From the docs:
When on, Vim will change the current working directory
whenever you open a file, switch buffers, delete a
buffer or open/close a window. It will change to the
directory containing the file which was opened or
selected. This option is provided for backward
compatibility with the Vim released with Sun ONE
Studio 4 Enterprise Edition.
Note: When this option is on some plugins may not work.
You can pass an a folder to gvim (when you have NERDTree then it will be a file tree) You can cd before start to begin in directory you want or use :cd <path> command to change current working directory, which can be passed to -c flag when running Vim:
$ [g]vim -c 'cd <path>'
You can also check current dir using :pwd command.
You can change the working directory with the :cd command. You can also pass this in a command-line option like this:
vim -c "cd wherever"
If you like the working directory to always be the file you're currently editing you can use the set autochdir option. Put that in your ~/.vimrc or see :help autochdir.
I know I'm late, but I started using CDargs which is a bash tool to mark certain directories as bookmarks, then use cdb and press tab to list all the bookmarked directories.
There is a vim plugin that interacts with the settingsfile of this tool: vim-cdargs.
This combo works really nice for me to switch between projects.
Or after opening gvim to go quickly to some bookmarked folder, then use Ctrl-p plugin to quickly find the file I want to edit.
extra hint: I don't even want to type :Cdb so I abbreviated c to expand to :Cdb by adding this to my vimrc:
cnoreabbrev c Cdb
after which typing :c followed by a space, will expand into :Cdb.
EDIT: I now use vim-startify which provides a start page for vim that shows the most recent used files. And with the option let g:startify_change_to_vcs_root = 1 it will change the working directory to the outermost vcs root folder of the file you opened. Which is almost always what I want.
Furthemore, I created my own 'plugin' with some key mappings that will switch to the closest or furthest directory, in the path of the current buffer, containing a .git directory or file. In order to easily switch between searching for files in the current git submodule or in the overal supermodule.
Also I switched to fzf with fzf-vim instead of Ctrl-p, which works significantly faster and is more highly configurable.

Resources