Skip to content

Commit

Permalink
sam: remove s (substitute) command use x (extract) and c (change) ins…
Browse files Browse the repository at this point in the history
…tead

They are roughly equivalent, instead of

 s/pattern/replacement/

to replace the first occurrence of pattern you can specify an address
to the change command:

 /pattern/ c/replacement/

the only difference being that the first command is restricted to the
current line.

 -+x/pattern/ c/replacement/

also restrictes matches to the current line, but performs the substitution
on the whole line not only the first match. Currently it is not possible
to only replace the n-th match as `s2/pattern/replacement/` would do
in sam(1).

A possible alternative syntax generalizing this concepts and applying
it to the `x` and `g` commands will be investigated in the future.

Global substitution as in

 %s/pattern/replacement/g

can be performed using

 x/pattern/ c/replacement/
  • Loading branch information
martanne committed Jan 13, 2017
1 parent 53638e3 commit a1dc3da
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 14 deletions.
6 changes: 0 additions & 6 deletions man/vis.1
Original file line number Diff line number Diff line change
Expand Up @@ -452,12 +452,6 @@ the range.
.It Sy d
Delete the text in range.
.\" Set dot.
.It Sy s/regexp/text/
Substitute
.Sy text
for the first match of the regular expression in the range.
Currently implemented in terms of
.Xr sed 1 "."
.El
.
.Ss Display commands
Expand Down
11 changes: 3 additions & 8 deletions sam.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ static const CommandDef cmds[] = {
"p", "Create selection covering range",
CMD_NONE, NULL, cmd_print
}, {
"s", "Substitute text for regexp in range",
"s", "Substitute: use x/pattern/ c/replacement/ instead",
CMD_SHELL|CMD_ADDRESS_LINE, NULL, cmd_substitute
}, {
"v", "If range does not contain regexp, run command",
Expand Down Expand Up @@ -1391,13 +1391,8 @@ static bool cmd_files(Vis *vis, Win *win, Command *cmd, const char *argv[], Curs
}

static bool cmd_substitute(Vis *vis, Win *win, Command *cmd, const char *argv[], Cursor *cur, Filerange *range) {
Buffer buf;
buffer_init(&buf);
bool ret = false;
if (buffer_put0(&buf, "s") && buffer_append0(&buf, argv[1]))
ret = cmd_filter(vis, win, cmd, (const char*[]){ argv[0], "sed", buffer_move(&buf), NULL }, cur, range);
buffer_release(&buf);
return ret;
vis_info_show(vis, "Use :x/pattern/ c/replacement/ instead");
return false;
}

static bool cmd_write(Vis *vis, Win *win, Command *cmd, const char *argv[], Cursor *cur, Filerange *r) {
Expand Down

0 comments on commit a1dc3da

Please sign in to comment.