views:

77

answers:

3

Hello. This is really noob question. There is set of vim commands

: command1 : command2

etc., which I would neet to type in in a raw quite often. How to I make it automatic? It is simple regexp replace command set, however I cannot script those in sed, since it involves non-latin locales and for some reason vim handles non-latin regexps correctly, while sed not.

+2  A: 

Why not put them in a function, then call the function ?

ldigas
I am not familiar with vim functions. Need to read a bit about it.
@temujin.ya.ru - apart from vim's help, try vim.wikia.org - has some nice examples.
ldigas
+2  A: 

If the set of commands is not always the same, the commands can be recorded in a macro:

  • press qq to start recording the macro (the second q is the "name" of the macro)
  • execute the set of commands (commands ans cursor moves are recorded)
  • press q to stop the recording
  • position the cursor for the next set (or better record that in the macro itself) and use @q to replay the macro

As any other command, the macro can be executed several time in a row e.g. 5@q.

Macros cann be saved from one session to another as register if :noremap is set.

philippe
Thank you! I think what I need is a macro. Nice explanation.
+1  A: 

You could

  • make them a command

    :command! DoStuff thing1 | thing2
    

    then execute it with

    :DoStuff
    
  • or a macro

    qq:thing1<CR>:thing2<CR>q
    

    (<CR> means "press ENTER") that you access with

    @q
    
  • or a function

    :funct! DoStuff()
    :  thing1
    :  thing2
    :endfunct
    

    that you

    • call with

      :call DoStuff()
      
    • or access through a command

      :command! DoStuff call DoStuff()
      

      that you execute with

      :DoStuff
      
  • or just run

    :thing1 | thing2
    

    in a single command line and then hit :thi (ie the first few letters of the command) and then use the up-arrow to recall the most recent lines of command history that started with that command.

Using the "bar", ie |, command separator will not work with all commands, but will work with most. If thing1 or thing2 is one of the commands that can't be sequenced with a bar, you'll have to either

  • do it with a macro or a function
  • or you can do

    :exec 'thing1' | exec 'thing2'
    

    on the command line or in a :command, but you'll have to be careful if thing1 or thing2 themselves contain single quotes.

The choice of which of these options is the most appropriate is usually based on the complexity of what you want to do.

Functions make it easy to parameterize your commands, ie to use variables in them, and are useful for combining a long series of commands, but are somewhat long-winded for simple cases. See :help user-functions for details.

Macros are convenient but you can only have 26 of them and they are usually pretty poorly named (ie with some letter of the alphabet). Also note that if you store something in the same register via y, c, etc, it will overwrite your macro. See :help recording for details.

Commands let you give your routines a meaningful name, can be parameterized in a different way than functions, and can do other fancy things like tab completion. Commands have to be written on a single line, which is one reason why it's common for vim scripts to implement commands that do nothing but call functions. See :help user-commands for more info.

If this series of commands ends up being something that you use on a regular basis, you can add your macro/command/function definition to your vimrc file, which will cause it to be ready to go whenever you use vim. Under linux or OS/X your vimrc file is the file ~/.vimrc. Getting macros set up in your vimrc is sort of tricky though, and if you're going to go to that length you might as well make it a function or a command anyway.

intuited