This is a followup question to Auto sorting of lists of include files, which explains the motivation.EDITI 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 commandand execute these in sequence. 
A script and keybindings to look for VIM commands in a file, and then execute them on that file
formatting
A possible solution is to add the following lines to your .vimrc mapping to execute the functionnmap <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    endwhileendfunctionNotesYou can replace <F4> by any key you want to use to start the functionThe variable l:pattern can be changed to match the pattern you'll use to indicate that the line contains a command to executeThe search command in the whileloop uses the flag W not to wrap around the file and thus executing the commands only once.EditI 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 2I 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    endforendfunctionA possible evolution would be to refactor both of the function to get only one accepting an argument describing the mode to use... 