On 21/11/11 14:19, porphyry5 wrote:
On Sun, Nov 20, 2011 at 5:10 PM, Tony Mechelynck [via VIM]
<[hidden email] </user/SendEmail.jtp?type=node&node=5010492&i=0>> wrote:
> Yes, and why not use bar-separated commands? You _are_ running in
> 'nocompatible' mode aren't you? The following (untested) assumes that
> this mapping definition is part of a script (of your vimrc, maybe):
Essentially I wrote scripts by trying each command individually in the
buffer, then writing the script as a string that exactly mimics what I
did with the individual commands, so normal-mode commands have no
termination, ex mode commands are terminated by a carriage return.
Now I understand that is not reliable. If the resulting script fits
on a single line I put it directly in .vimrc, otherwise I make it a
.vim script and map to that script in .vimrc. And yes, I use "set
nocompatible" in .vimrc
>
> map p$
> \ :1d <Bar>
> \ while @" != "" <Bar>
> \ b # <Bar>
> \ 1call search(@") <Bar>
> \ s/^/$ / <Bar>
> \ b # <Bar>
> \ d <Bar>
> \ endwhile<CR>
>
What I find particularly confusing is the lack of correspondence
between command formats in scripts vs their counterparts in the buffer
command line, as above. You are constrained to use :1d in both
places, because 1d does something different in both places, but you
use b# here to switch buffers, which requires :b# at the command line
or you get a completely different result. Analogous to :1d, one would
expect you to use :1/@" to go to line 1 and search for the content of
the register, but you use the far lengthier '1call search(@")'
instead. As far as I can determine the two forms work the same as far
as / goes, but something must have prompted you to use search()
instead?
In scripts, I use ex-commands as much as possible, because _every_ line
of a script must be an ex-command, which could be typed on the
bottom-line of your Vim screen after hitting : — the whole mapping
definition above is _one_ command-line, which I recommend to write as
above in a script, on several lines with continuation lines, for
legibility. It stores the {rhs} as the following command line:
:1d | while @" != "" | b # | 1call search(@") | s/^/$ / | b # | d |
endwhile^M
where the ^M at the end means that the mapping will "hit the return key"
to execute the ex-command immediately, and the bars at various places in
the middle allow (with some restrictions) concatenating.several
ex-commands on one line. In a function, the exact same set of operations
would be written as
function MarkHeaderLinesInAltFile()
1d
while @" != ""
b #
1call search(@")
s/^/$ /
b #
d
endwhile
endfunction
where the initial colons at the start of each line are not written
simply because every script line is implicitly an ex-command so you
don't need to repeatedly put Vim into command-line mode. You could then
define your mapping as
map p$ :call MarkHeaderLinesInAltFile()<CR>
The difference between :1/@ and :1call search(@") is that the first
searches for the first at-sign in the file (or the second if the first
one is the very first character in the file) while the second searches
for the first match with the pattern in the default register. Adding a
double quote after :1/@ would change nothing: it would be taken as the
start of a comment. To use the contents of the default register and
write it as a "naked range" in a script, you would have to write
:execute '1/' . @"
which to my mind is much less elegant than
:1call search(@")
Since _every_ line of a script is an ex-command, / and ? searches can
only be used in scripts as part of ex-command ranges.
Ex-commands are explained in detail in the helpfile |cmdline.txt| but
I'm not sure in which sequence you should read its various sections.
Now please go back, _read_ all the help topics I listed (quoted below),
try your best to understand them, and I hope it will bring you some
enlightenment.
> See
:help cmdline.txt
:help [range]
> :help 'nocompatible'
> :help :bar
> :help map_bar
> :help line-continuation
> :help :d
> :help :call
> :help search()
> :help :s
> :help :map-<buffer>
> :help 'runtimepath'
> :help :autocmd
> :help FileType
Best regards,
Tony.
--
hundred-and-one symptoms of being an internet addict:
223. You set up a web-cam as your home's security system.
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php