1

I am switching to Linux and used to have NoteTab Pro, which allowed me to do scripted search and replaces... as I need multiple replace sequences to achieve a certain outcome. I'd like to put these sequences into a file or macro, to apply it to multiple times, while I improve the script, or apply it to multiple files.

Does VIM support this?

Why not try? Well, I research before installing things. :)

3 Answers 3

3

There's hardly any editor better suited to scripting than Vim, so the answer is a resounding yes.

Several approaches are possible:

  1. As @Angelo already answered, you can simply put the Vim commands (Ex commands to be precise; i.e. anything you'd type after : and conclude with <Enter> in Vim) in a scriptname.vim file and then :source path/to/scriptname.vim to load and execute them. The nice thing here is that you can just keep the script open, make edits and persist, and then can immediately rerun it. On the other hand, the invocation isn't short and nice, and this doesn't scale well. (You could :source another script from one, but that's really ugly.)
  2. As an alternative, you can skip the external file and just put the commands into a register (:help quote_alpha). You can paste the register into a scratch buffer, edit, and then yank (copy) back into the register, and execute with :@. Named registers are usually persisted automatically across sessions, but you could also put them into your .vimrc (see next) to prevent the macros from overwriting.
  3. The most common approach is to make a new custom mapping or command. Mappings are invoked by short keystrokes, and align with Vim's modal editing (so the same key(s) can do different stuff depending on the mode); these are used for stuff that's done frequently. Commands are Ex commands, so longer to type, but you'll get completion on Vim's command-line. See :help usr_40.txt for detailed instructions on how to write mappings and commands. You usually put those into your central configuration file, ~/.vimrc, and then they are available immediately after starting Vim. Compared with previous approaches, mappings and commands fit seamlessly into Vim and can be easily recalled, so these are great for stuff that you will need again and again in the future. The downside is that editing and reloading is more complex (restarting Vim takes time, reloading into the current session is a bit more effort).
  4. If your extensions are useful in general, you can turn them into a plugin (:help write-plugin), which can then easily be shared across Vim installations, published (on GitHub or vim.org). There's a plethora of plugins available; so search before reinventing the wheel!
2

Sure, you can do any vim commands through an external file including search and replace, and load them with

:source ~/path/to/file

Documentation:

                                        *:so* *:source* *load-vim-script*
:so[urce] {file}        Read Ex commands from {file}.  These are commands that
                        start with a ":".
                        Triggers the |SourcePre| autocommand.
1
  • 2
    One of the more important (VIM) excommands to learn is :help, and in the questioner's case more specifically :help script.
    – JdeBP
    Commented Feb 16, 2020 at 9:06
0

You can also build vim-scripts to use outside vim.

Example:

$ cat trim-spaces

#!/usr/bin/vim -s
:%s/[ \t\r]*$//
:%s/[ \t\r\n]*\%$/\n/
G
:r ! echo "\#last changed by $USER in :" `date`
:x
  • line2: remove spaces at the end of the line
  • line3: remove empty lines at the end of the file
  • line4,5: add a "last changed" line

Next, we can

$ trim-spaces file1

in order to clean file1

You must log in to answer this question.

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