I have a file foo.txt with just a one line ab. I want to launch Vim in such a way that it will open this file, swap characters using xp, and then save it as bar.txt.

Something like this:

gvim foo.txt xp :wq > bar.txt

I'm not sure it is possible. Any suggestions?


You can use Ex commands (linux version using pipes):

printf '%s\n' 'normal! xp' 'saveas bar.txt' 'q!' | ex foo.txt

vim -e and vim -E can usually be used as a substitute for ex.

Or use a custom script:

gvim -S mycmds.vim foo.txt

Where the file contains arbitrary Ex commands:

normal! xp
saveas bar.txt
| improve this answer | |
  • Hello, thanks. The former solution doesn't work for some reason; Command Prompt gives me an error 'ex' is not recognized as an internal or external command, operable program or batch file.. The second version doesn't really swap characters; bar.txt contains only a. – john c. j. Sep 5 at 14:49
  • 2
    The first assumes you have ex (comes with vim, maybe not with gvim?). As for the second, I find that odd.. it could be a cursor issue? You might want 1 then normal! 0xp or some other sequence to position the cursor appropriately. – D. Ben Knoble Sep 5 at 15:25
  • 2
    ex is required on posix systems; again, not sure about the windows install of vim. The issue with a non-default vimrc is likely some autocommand restoring a file position or some other thing. You can use -u and other options to force the default vimrc, none, or some other combination. @johnc.j. – D. Ben Knoble Sep 5 at 15:56
  • 2
    If Ex is not found, you can try to substitute ex by vim -e. In GNU/Linux systems, Ex is just Vim in Ex mode. Also, there are other tools to do this job, for example, Sed. – Quasímodo Sep 5 at 16:52
  • 2
    @Quasímodo agreed; this is one of those cases where sed really is right (i.e., we're not trying to edit in place with a stream editor 🙄—excuse the rant) – D. Ben Knoble Sep 5 at 16:57

Here's a couple more techniques you can use.

Using -c or +

You can pass ex commands into Vim by using the -c command line option or its shortened version, +:

vim foo.txt +'normal! xp' +':wq! bar.txt'

(Note that the :wq! command can take a filename argument: it's slightly more concise to use this rather than :saveas and :q! separately.)

Using a script with -s

Similar to the -S option described by @D. Ben Knoble, you can use the -s option to run a script interpreting its contents as keystrokes.

Create a file named e.g. my_script with the following contents, being sure to press Return at the end of the line so that the file contains two lines in total (the second being a blank line):

xp:wq! bar.txt

Then run this script:

vim foo.txt -s my_script

See also the -w option, which you can use to record a script that can be executed with the -s option.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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