How to open some files/directories read-write, others read-only - vim

This is similar to "Split vim window with one file read/write and one file read-only".
From the command line, I want to open files in one directory as read-write, and files in another directory as read-only. How do I do this?
For example:
vim read-write-dir/**/* read-only-dir/**/* # zsh
Listing the files individually isn't an option, there's a large number of files under each directory (I use bufExplorer).
My motivation is, I'm editing two programs in different OSX Terminal tabs, and Vim gets unhappy about a file being edited twice.

If you want to open a read-only file and read-write file while starting Vim, you can use the -c option in your terminal:
vim -c ":view file_read_only" file_read_write
Then you can switch between them using :e #

Related

How can I save a write protected file in vi?

I'm trying to save my modified "menu.lst" file in vi. When I save the file, vi says: 'menu.lst' is read only.
How can I fix this?
The file you are trying to save is read-only, meaning you cannot modify its contents. It needs to be marked as writable. The process varies depending on your OS. Here are some helpful resources on how to change permissions of files:
For Windows 10: Nibbleguru: How to remove read-only attribute in Windows 10
For Linux (using chmod): TLDP: File Permissions
For macOS: Chron: How to Change File Permission From Read-Only to Read-Write on a Mac
EDIT:
As filbranden pointed out, for Grub's files, you should be opening vi using the sudo command. Grub's files are meant to be modified by root only. You should be opening your files using sudo vi menu.lst instead.
I have this on my vimrc
cnoremap w!! execute 'silent! write !sudo tee % >/dev/null' <bar> edit!
command! SaveAsRoot w !sudo tee %
cnoreabbrev sudow SaveAsRoot
For instant use, just copy to the clipboard and run:
:#+
:SaveAsRoot
The :#+ loads your clipboard into vim memory which allows you to run the given commands while not saved on your vimrc.
People suggest using sudo vi(m) on unix, but this could have unwanted consequences: all commands executed in this window are done by root and so you could accidentally do unwanted things like deleting file or just creating files owned by root.
Instead you should think about using sudoedit instead. It will copy the file to /tmp and open it in $EDITOR (if you are using vim, you should set it in your ~/.profile / ~/.bash_profile).
But beware: Something I stumbled across: the original file is only replaced when you close vim - no matter how often you save! (This is, because you are editing the file in /tmp and not the original).
It is not that I don't use sudo vim but if I do, I am extra cautious about what I do ;) I do it for example if I know that I will need to edit multiple files as root, or that I want to execute other commands from within vim as root (e.g. git)
And something even more important to me: sudo vim is using roots vimrc instead of mine, but with sudoedit I have my own config...

How can I change to a different directory on an FTP server in Vim?

Vim has a fancy NetRW plugin that lets me type
vim ftp://ftp.example.com/
And get a nice directory listing, which I can edit files from by simply pressing CR while on top of them.
However, I can't seem to do this with directories. In the help file that appears when I press F1, it says I should be able to:
BROWSING netrw-cr {{{2
Browsing is simple: move the cursor onto a file or directory of interest.
Hitting the <cr> (the return key) will select the file or directory.
Directories will themselves be listed, and files will be opened using the
protocol given in the original read request.
However, when I use j/k to navigate to a directory and press <cr> to try to open it, I just get:
somedirectory: Not a regular file
Vim tries to access ftp://ftp.example.com/somedirectory. Notice that it doesn't add the slash signifying that it's a directory.
I'm at a loss for why this occurs. How can I simply enter that directory within Vim, without having to exit Vim and retype vim ftp://ftp.example.com/somedirectory/?
I'm running Vim 7.4 on Ubuntu 14.04.
Vim didn't seem to know that my directory really was a directory, as it didn't show a / sign after it in the directory listing.
Luckily, I found a way to force NetRW to act like a file is a directory: gd.
FORCING TREATMENT AS A FILE OR DIRECTORY netrw-gd netrw-gf {{{2
Remote symbolic links (ie. those listed via ssh or ftp) are problematic
in that it is difficult to tell whether they link to a file or to a
directory.
To force treatment as a file: use
gf
To force treatment as a directory: use
gd
So, simply press gd instead of <cr> to change to the directory under the cursor.
I'm still not sure why NetRW thought my directory was a file. The help message only said "remote symbolic links," which they're not (I think). It may have something to do with my web host (iPage), which might be using symbolic links everywhere instead of actual directories. Or maybe something just borked on my end.

how to make a file editable in linux without becoming root

i am working with files placed in directory /etc/asterisk using vim editor, every time i have to do some editing i have to go to terminal to become root and opens file in vim editor and performs edition,but if i do it by directly opening the file from such directory(/etc/asterisk) in gedit and perform edition in the file but it doesn't show the save option if save as option is selected message becomes "you need to become root for such operation." is there any way to open these files(placed in /etc/asterisk) directly from gedit and save the changes without going into terminal
You can always do sudo gedit from the terminal to launch gedit as root to edit the files.

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.

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

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.

Resources