1

I do

find $HOME -type f -name "*.tex" -exec grep -il "$1" {} + | vim -

I cannot exit vim with q only because it gives the error which I do not understand why

E37: No write since last change (add ! to override)

So you have to do q! instead. However, I would like to let Vim know that there is nothing to override and use instead q. So probably, it could be reached by altering the find command.


How can you find such that Vim knows there is no need of overriding?

2 Answers 2

6

You can change vim - to vim -R -. From the manual:

Read-only mode. The 'readonly' option will be set. You can still edit the buffer, but will be prevented from accidently overwriting a file. If you do want to overwrite a file, add an exclamation mark to the Ex command, as in ":w!". The -R option also implies the -n option (see below). The 'readonly' option can be reset with ":set noro". See ":help 'readonly'".

[The implied -n option disables the swap file.]

It doesn't explicitly state it, but read-only mode also implies "+nomodified". That is, you won't be prompted to save upon exiting if you don't make any changes. If you attempt to make changes, it will first warn you that you are modifying a readonly file, and then prompt you to save the file upon exiting.

Depending on your system, view may also be a symlink to vim that is equivalent to vim -R.

2
  • Great! Is there any other differences between view and vim -R? I cannot understand why to otherwise create a new command. Commented May 19, 2016 at 5:00
  • 1
    I don't think there is any other difference. It is fairly common in Unix / Unix-like command line tools to have separate commands as aliases for commonly used options. For another example zcat, gunzip -c, and gzip -cd all do the same thing. If you look in /usr/bin, you'll likely see a lot of commands are just symlinks to other commands. These commands change their behavior based on the name they were called as.
    – Drew
    Commented Jun 8, 2016 at 22:00
5

You are piping content into Vim, so it's starting a new unsaved buffer. If you don't need to save the content, you can add +'set buftype=nofile' to your command:

find $HOME -type f -name "*.tex" -exec grep -il "$1" {} + | vim +'set buftype=nofile' -

Vim will treat the buffer as if it has no associated file. Which would be good if you plan to use Vim as a pager. When you make changes, it will not prompt you to save it, and when you write to a file with :w test.txt the buffer will not be associated with the file.

If you want to be prompted to save if there are changes, or you want the buffer to be associated with a file once written, you could use: +'set nomodified' instead, which sets the initial state of the buffer as not modified.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.