3

I have FCEDIT set to vim. If I call fc command it will open vim with temporary file and contents of that file will be the last executed command from history.

The problem is that even if I exit without saving, the command still gets executed. For instance:

$ printf "\nHelloAskubuntu\n\n"

HelloAskubuntu

$ fc # opens vim, I do :q!
printf "\nHelloAskubuntu\n\n"

HelloAskubuntu

How do I avoid that ?

3
  • 1
    In the editor, enter dd:x. That will delete the line and save an empty file. That way there is not command to execute.
    – John1024
    Commented Jul 19, 2016 at 1:10
  • Is there any better approach than manually deleting a line ? Commented Jul 19, 2016 at 1:15
  • 1
    @Serg It’s the standard approach in Subversion and Git commit message editor, for example. However, you need to delete the whole file. Do it using dG. A one-line file is a special case.
    – Melebius
    Commented Oct 3, 2016 at 10:32

2 Answers 2

3

fc will not execute the command if the editor command did not exit successfully. In Vim, there's a command for forcing this: :cq:

                                                        :cq :cquit
:cq[uit][!]             Quit Vim with an error code, so that the compiler
                        will not compile the same file again.
                        WARNING: All changes in files are lost!  Also when the
                        [!] is not used.  It works like ":qall!" :qall,
                        except that Vim returns a non-zero exit code.

So, when you don't feel like running the command, use :cq to quit instead of :q!. It might be possible to make Vim do this if you didn't make any changes, but that will probably be more annoying to get to work reliably than just using :cq.

0
0

This happen because the contents are already stored in the temporary file /tmp/bash-fc-*. Closing it directly, fc will run that shell anyway.

A working approach that I could test it in Ubuntu 16.04.

  1. Create a proxy command (new editor)

    sudo vim /usr/local/bin/vim2
    

    Make it open that file in vim then empty it on disc. So the contents are only in vim if you don't save it, fc will run nothing

    #!/bin/sh
    
    sh -c "sleep 1; echo -n ''>$1" &
    vim $1
    

    Fix permission

    sudo chmod +x /usr/local/bin/vim2
    
  2. Set FCEDIT to vim2 or temporarily to test export FCEDIT=vim2

The only hope is that you are not quicker than me, to close vim before 1 sec passed.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .