I want to map, in my ~/vimrc file, the key <F4> in vim in order to execute the indent program on the file which I'm currently editing. How can I do it?If I do this,the compatibility with vi will be broken?Thanks in advance.
Map a shell command to execute it into vim
vim;vimrc
Although it's not even implied by --help or the man pages, as it turns out indent will process things in a pipeline, reading from standard input (aka, FD 0) and writing to standard output (aka, FD 1) which is what you need to get this to work within vim.Start with the command mapped:map <F4> :!indent<CR>Go to the first line of the file and enter visual mode (Ctrl-v).  Then press Ctrl-End, which will take you to the last line of the file.  It will appear that a block the width of the last line plus one (the newline, which is at the end of all lines in vim), including every line, is highlighted.  It doesn't really matter how wide it is, because if we feed this block through an an external command, the vertical is the only determining factor: if a line is included, it will be the whole line.So now you press F4 and presto.  This was tested by me and it works.  IMO, that's simple enough -- plus it means you could indent just a section easily too, instead of the whole file, if you want. Another idea would be to remove the return <CR> from the mapping so you have to hit enter yourself, but are also left with the option of adding options to indent.  Then you can mix and match different styles, lol.If you really want to map it to select the whole file automatically, call indent, then return to command mode and/or your original position -- I'm sure this is possible using a more elaborate construct.  Unfortunately I don't now anything about programming vim, so I can't tell you how it would go.  But someone may come along that does.  Until then, you'll have to settle for 6 keystrokes (:1+return to go to the start  of the file, ctrl-v for visual, ctrl-end to go to end of file, and finally, F4).