2

This is a followup question to Auto sorting of lists of include files, which explains the motivation.

EDIT I am looking for a script and/or keybindings to look for VIM commands in a file, and then execute them on that file. As mentioned in the referenced question, the idea is that the script and the key bound to it, will do the following: Look for lines that obey a certain patter in a file. E.g.,

'// vim: some vim command

and execute these in sequence.

0

1 Answer 1

2
+50

A possible solution is to add the following lines to your .vimrc

" mapping to execute the function
nmap <F4> :call ExecuteCommandsFromFile()<CR>

function! ExecuteCommandsFromFile()
    " The pattern which will indicate the commands
    let l:pattern="'// vim: "
    let l:res=1

    " Go to the first line of the file
    normal gg

    " While some commands are found in the file
    while search(l:pattern, "We") != 0

        " Get the position of the text representing the command
        let l:start=col('.')
        let l:end=col('$')

        " Get the command
        let l:line=strpart(getline('.'), l:start, l:end)

        " Execute the command
        execute l:line
    endwhile

endfunction

Notes

  • You can replace <F4> by any key you want to use to start the function
  • The variable l:pattern can be changed to match the pattern you'll use to indicate that the line contains a command to execute
  • The search command in the whileloop uses the flag W not to wrap around the file and thus executing the commands only once.

Edit

I made this according to what I understood from the question but if the solution doesn't really fit your needs don't hesitate to give more details so that I can adapt the solution.

Edit 2

I thought it could be interesting to execute the commands after parsing all the file instead of executing it as soon as the command is read. So here is a second version where the function firstly get all the commands and then execute them:

function! ExecuteCommandsFromFile2()
    " The pattern which will indicate the commands
    let l:pattern="'// vim: "
    let l:res=1

    " Go to the first line of the file
    normal gg

    " The list will contain the commands to execute
    let l:commands = []

    " While some commands are found in the file
    while search(l:pattern, "We") != 0

        " Get the position of the text representing the command
        let l:start=col('.')
        let l:end=col('$')

        " Get the command
        let l:line=strpart(getline('.'), l:start, l:end)

        " Add the command to the list
        call add(l:commands, l:line)
    endwhile

    " Execute each command
    for l:command in l:commands
        execute l:command
    endfor
endfunction

A possible evolution would be to refactor both of the function to get only one accepting an argument describing the mode to use...

Your Answer

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

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