How can I open file with modifiable on - vim

I want to open file with modifiable buffer set noma ON by default for this specific file.
for example something like this:
vi file1.txt noma
vi file2.txt ma , # default
OR at least inside vim:
:tabnew file1.txt noma
Thanks

You can add that to your .vimrc, which will set the flag if the file has that name:
augroup ReadOnly
autocmd!
autocmd BufReadPost file1.txt set noma
augroup END
That will set it both if the file exists (BufReadPost).
Edit: The view command allows you to open a file in readonly mode.
For reference: :help autocmd-groups and :help autocmd-define

You can use the -R command-line option to open a file in 'readonly' mode (which includes 'nomodifiable' and also other options that are useful when viewing files.)
$ vim -R file1.txt
The view command is usually available as a shortcut to vim -R, so this is often also possible:
$ view file1.txt
If you already have a Vim running and want to use it to open a file in read-only mode in a new tab, you can use this sequence:
:tab sview file1.txt
The :view command opens a file in read-only mode, the :sview command does so in a split and the :tab modifier has Vim do so in a new tab instead.

Related

Vifm. Edit file in opened gvim

I use Gvim to write code. And use vifm to make file management ( copy, move files, extract tar's ). Sometimes, when navigating in vifm i need to open some file to edit him. Using :e it opened vim in same window. Is there any way to open file to edit in already opened gvim program?
You can use Vim's client-server feature to send the :edit to the existing GVIM instance. In the terminal Vim / vifm, execute:
:!gvim --remote path/to/file
See :help remote.txt for details. You can also open the file in a new tab page with --remote-tab etc.
Partial solution/workaround (I'm using a mac fwiw):
In vfimrc, define
" yank current file path into the clipboard
nnoremap Cf :!echo -n %c:p | pbcopy %i<cr>
To copy filename and dir into system clipboard
Then in vifm, cursor over file and type
Cf
:!gvim "
<cmd-v>
to paste clipboard,
and finish expression with...
"
<enter>
and now that file should open in gvim. It worked for me

Show the list of the files inside the folder of the current file

Imagine I'm editing file, and I want to show the list of the files inside the folder who belongs the file I'm editing, to edit one of them.
How can I do that? Is there any way using FuzzyFinder?
Did you even read FuzzyFinder's documentation (:help fuzzyfinder)? Quickly opening nearby files is one of that plugin's main features.
Without installing anything, you can do:
:Ex[plore]
to open the netrw file tree. See :help netrw.
You can also do:
:e <Tab>
Add these lines to your ~/.vimrc to make command line completion even better:
set wildmenu
set wildmode=list:full
and read :help wildmenu and :help commandline-completion.
set autochdir is a useful option to add to your ~/.vimrc, by the way.
change vim current directory to current file's:
:cd %:h
then
FuzzyFinder can do what you want (pick and edit). (:FufFile) I have mapping :
nmap <Leader>ff :FufFile<cr>
NERDTree can do that as well.
Depends on what you mean by showing the file.
To include the list of files in the currently edited files, you can do something like:
:read !ls /path/to/file
(it can be shortened to :cd %:h | read !ls if you don't mind if vim changes it's current directory...)
If you want to pick another file to edit, I'd suggest to take a look at NerdTree plugin (here is a little intro). Or you can simply issue:
:cd %:h | e .

Open several files in new tabs with VIM

How can I open several files using wildcards in a new tab for each file with VIM?
Similar to How can I open several files at once in Vim? but in new tabs instead of buffers.
Use the -p flag:
vim -p *.c
The above will open all files matching *.c.
You can also create an alias for this (I have alias vip='vim -p' and I only type vip *.c for the above example)
If you are in Vim, this command will open each html file in the current directory in its own tab and restore syntax support (disabled by :argdo):
:args *.html | argdo tabe | tabdo syntax on
If you are in your shell, go for the other answers.
If you want to open only .md.
vim -p *.md
Or after opening vim, use :args to specify md files.
:args *.md
:tab all
with the p option: vim -p file1.cc file2.cc file3.cc
To open files in new tabs without replacing the arguments or tabs that are already open:
:argadd *.c | tab all
Also, to search for files in subdirectories:
:argadd code/**/*.c | tab all

Vim autocmd (save file, run code formatter, reload file)

I wish to integrate the source code formatter Uncrustify with Vim. Any of the below two options will suffice.
Format the code that I am currently editing (i.e. when gq is pressed).
Format the code when I save the file and then reload the formatted file into current Vim window.
Option 1 is preferable. I tried
set formatprg=uncrustify\ -c ~/misc/uncrustify.cfg --no-backup
i.e. I call Uncrustify with command line options.
This does not work. Vi gives the E518: Unknown option: ~/misc/uncrustify.cfg error.
For option 2, I tried the following in the vimrc file
autocmd bufwritepost *.cpp ! ~/bin/uncrustify -c ~/misc/uncrustify.cfg --no-backup <afile>
The file is formatted after the save, but I have to manually reload the file into Vim.
Have you tried escaping whitespaces:
:set formatprg=uncrustify\ -c\ ~/misc/uncrustify.cfg\ --no-backup
UPDATE
uncrustify prints "Parsing: 170 bytes ..." message to stderr so we need to redirect it to /dev/null:
:set formatprg=uncrustify\ -c\ ~/misc/uncrustify.cfg\ -l\ CPP\ --no-backup\ 2>/dev/null
gq operates on lines, so you can select necessary lines in visual mode and execute gq. For example, if you want to reformat whole file execute ggVGgq.
More info at :help gq

How do I make vim open all files matching a pattern in different tabs?

In a given working directory, if I do
:tabe **/test*.py
vim complains with E77: Too many file names. What if I want it to open every matching file in a separate tab? There must be a way to do it, but I can't find it.
You could use the args list and argdo like so:
:args **/test*.py
:argdo tabe %
However, the syntax event is turned off by argdo (to speed up the normal use case), so the files will be loaded without syntax at first. You could follow it up with a :syntax on to force the syntax event on all loaded buffers. Compressed into one line (need to wrap argdo in execute so it doesn't absorb the following |):
:args **/test*.py | execute 'argdo tabe %' | syntax on
Alternately, you can open vim from the command line via:
vim -p **/test*.py
But that will max out at 10 tabs.
You can use the following:
:next **/test*.py
It opens all the files.
To map it
nmap <c-d> :args **/*.tpl<bar>execute 'argdo tabe %'<bar>syntax on<cr>
But still it displays list of files, you have to press enter few times (depending of number of files).
This functionality can be included as a command in your .vimrc file:
"open all files in seperate tabs
command -nargs=1 OpenAll call <SID>openAll(<f-args>)
function! s:openAll(dir)
execute 'args ' . a:dir
silent argdo tabe %
syntax on
endfunction
With this function running :OpenAll **/*.py from vim will quickly open all files into new tabs
None of the other answers works for me, but this is fine:
find <path> -iname <pattrn> | xargs -o vim -p
all files are visible in different tabs
file lookup is recursive
Note, vim can limit tabs - to be changed by set tabpagemax=42.
Also, if you wonder how to close all tabs at once, use :qa

Resources