Re: Feature: Easily remove current command from history

2016-01-11 Thread Chet Ramey
On 1/8/16 4:01 PM, Piotr Grzybowski wrote:
> On Thu, Jan 7, 2016 at 9:32 PM, Chet Ramey  wrote:
>> On 1/5/16 8:08 AM, Greg Wooledge wrote:
>>
>>> imadev:~$ zap me
>>> bash: zap: command not found
>>> imadev:~$ history -d -1
>>> bash: history: -1: history position out of range
>>> imadev:~$ history -d end
>>> bash: history: end: history position out of range
>>> [..]
>>
>> Yes, this is a reasonable feature to consider for a future version.
> 
>  I cant believe we do not have it already.

Believe it or not, it's not a heavily-requested feature.

I'll take a look at your patch, but I am not going to be adding any more
features to bash-4.4 in favor of release engineering.  If it's right, and
I have no reason to think it's not, it will be in a future release.

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/



Re: Feature: Easily remove current command from history

2016-01-08 Thread Piotr Grzybowski
On Thu, Jan 7, 2016 at 9:32 PM, Chet Ramey  wrote:
> On 1/5/16 8:08 AM, Greg Wooledge wrote:
>
>> imadev:~$ zap me
>> bash: zap: command not found
>> imadev:~$ history -d -1
>> bash: history: -1: history position out of range
>> imadev:~$ history -d end
>> bash: history: end: history position out of range
>> [..]
>
> Yes, this is a reasonable feature to consider for a future version.

 I cant believe we do not have it already.
 Maybe I am missing something, please review the following (works for
me; devel at 6f82653c5ef09aeeeba4376a1c65ce86c3605c00, also in
attachment in case of formatting problems):

diff --git a/builtins/history.def b/builtins/history.def
index dfa4fb7..4c938af 100644
--- a/builtins/history.def
+++ b/builtins/history.def
@@ -107,6 +107,8 @@ history_builtin (list)
   int flags, opt, result, old_history_lines, obase;
   char *filename, *delete_arg;
   intmax_t delete_offset;
+  int is_legal_number = 0;
+  int index = 0;

   flags = 0;
   reset_internal_getopt ();
@@ -180,14 +182,34 @@ history_builtin (list)
 #endif
   else if (flags & DFLAG)
 {
-  if ((legal_number (delete_arg, &delete_offset) == 0)
- || (delete_offset < history_base)
+ is_legal_number = legal_number (delete_arg, &delete_offset);
+ if (is_legal_number == 0)
+   {
+ sh_erange (delete_arg, _("history position"));
+ return (EXECUTION_FAILURE);
+   }
+ if ((delete_arg[0] == '-') && (delete_offset < 0))
+   {
+ if ((index = history_length + delete_offset) >= 0)
+   {
+ opt = index + 1;
+   }
+ else
+   {
+ sh_erange (delete_arg, _("history position"));
+ return (EXECUTION_FAILURE);
+   }
+   }
+ else
+   {
+  if ((delete_offset < history_base)
  || (delete_offset > (history_base + history_length)))
{
  sh_erange (delete_arg, _("history position"));
  return (EXECUTION_FAILURE);
}
   opt = delete_offset;
+   }
   result = bash_delete_histent (opt - history_base);
   /* Since remove_history changes history_length, this can happen if
 we delete the last history entry. */

sincerely,
pg
diff --git a/builtins/history.def b/builtins/history.def
index dfa4fb7..4c938af 100644
--- a/builtins/history.def
+++ b/builtins/history.def
@@ -107,6 +107,8 @@ history_builtin (list)
   int flags, opt, result, old_history_lines, obase;
   char *filename, *delete_arg;
   intmax_t delete_offset;
+  int is_legal_number = 0;
+  int index = 0;
 
   flags = 0;
   reset_internal_getopt ();
@@ -180,14 +182,34 @@ history_builtin (list)
 #endif
   else if (flags & DFLAG)
 {
-  if ((legal_number (delete_arg, &delete_offset) == 0)
- || (delete_offset < history_base)
+ is_legal_number = legal_number (delete_arg, &delete_offset);
+ if (is_legal_number == 0)
+   {
+ sh_erange (delete_arg, _("history position"));
+ return (EXECUTION_FAILURE);
+   }
+ if ((delete_arg[0] == '-') && (delete_offset < 0))
+   {
+ if ((index = history_length + delete_offset) >= 0)
+   {
+ opt = index + 1;
+   }
+ else
+   {
+ sh_erange (delete_arg, _("history position"));
+ return (EXECUTION_FAILURE);
+   }
+   }
+ else
+   {
+  if ((delete_offset < history_base)
  || (delete_offset > (history_base + history_length)))
{
  sh_erange (delete_arg, _("history position"));
  return (EXECUTION_FAILURE);
}
   opt = delete_offset;
+   }
   result = bash_delete_histent (opt - history_base);
   /* Since remove_history changes history_length, this can happen if
 we delete the last history entry. */


Re: Feature: Easily remove current command from history

2016-01-07 Thread Chet Ramey
On 1/5/16 8:08 AM, Greg Wooledge wrote:

> imadev:~$ zap me
> bash: zap: command not found
> imadev:~$ history -d -1
> bash: history: -1: history position out of range
> imadev:~$ history -d end
> bash: history: end: history position out of range
> 
> Allowing a negative offset to refer to the last (or last minus however
> many) entry would probably satisfy the original request.  Semantically,
> there's precedent with negative array indices and negative starting
> positions in ${parameter:offset:length} notation.

Yes, this is a reasonable feature to consider for a future version.

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/



Re: Feature: Easily remove current command from history

2016-01-05 Thread Dennis Williamson
On Mon, Jan 4, 2016 at 4:35 PM, Dennis Williamson <
dennistwilliam...@gmail.com> wrote:

>
>
> On Mon, Jan 4, 2016 at 4:05 PM, Dennis Williamson <
> dennistwilliam...@gmail.com> wrote:
>
>>
>>
>> On Mon, Jan 4, 2016 at 3:07 PM, Eduardo A. Bustamante López <
>> dual...@gmail.com> wrote:
>>
>>> Take into account that many options have been provided (history -d, the
>>> space
>>> prefix, even editing .bash_history yourself).
>>>
>>> But you request a single key stroke to do this... why?
>>>
>>> If you enter a password by mistake in your shell, and it gets recorded,
>>> then
>>> you go and clean up. It's not hard to do.
>>>
>>> But since you request a simple-and-easy way of doing this, it seems like
>>> you do
>>> this a lot... which you shouldn't! :-)
>>>
>>> Now, it is up to you to convince Chet that it is so important to have a
>>> simple
>>> shortcut to do this. IMO, it isn't.
>>>
>>> --
>>> Eduardo Bustamante
>>> https://dualbus.me/
>>>
>>>
>>
>> Just bind your own keystroke to a function which uses history -d:
>>
>> histdel() {
>> local last_command histline
>>
>> last_command=$(history 1)
>>
>> histline="${last_command%  *}"
>>
>> history -d "$histline"#  I wish history -d accepted negative
>> offsets
>> }
>>
>> bind -x '"\ez": histdel'
>>
>> Then Esc-z or Alt-z will delete the most recent history entry. You could
>> choose another keystroke to bind.
>>
>>
>> --
>> Visit serverfault.com to get your system administration questions
>> answered.
>>
>
> Actually, this is better:
>
> histdel() {
>
> (#  use a subshell to make extglob setting and function variables
> local
>
> last_command=$(history 1)
>
> #  strip modified-entry marker, it doesn't matter if we delete an
> asterisk in the command since we're deleting it anyway
> last_command=${last_command/\*/ }
> shopt -s extglob
> last_command=${last_command##*( )}  # strip leading spaces
> histline="${last_command%%  *}"
>
> history -d "$histline"#  I wish history -d accepted negative
> offsets
>
> )
> }
>
> bind -x '"\ez": histdel'
>
> I'm using a subshell here. You can use the local keyword for variables and
> save and restore the extglob setting if you prefer.
>
> --
> Visit serverfault.com to get your system administration questions
> answered.
>


Petr Skočík pointed out to me in a private message that my subshell version
only affects the history within the subshell. Here is a version that
doesn't require setting extglob and is much shorter:

histdel () {
local histline commandline
IFS=' *' read -r histline commandline <<< "$(history 1)"
history -d "$histline"
}

-- 
Visit serverfault.com to get your system administration questions answered.


Re: Feature: Easily remove current command from history

2016-01-05 Thread Greg Wooledge
On Mon, Jan 04, 2016 at 04:35:17PM -0600, Dennis Williamson wrote:
> > Just bind your own keystroke to a function which uses history -d:
> >
> > histdel() {
> > local last_command histline
> >
> > last_command=$(history 1)

etc.

Hmm.  Why isn't this easier...?

imadev:~$ help history
...
Options:
...
  -d offset delete the history entry at offset OFFSET.

What's an OFFSET?

imadev:~$ zap me
bash: zap: command not found
imadev:~$ history -d -1
bash: history: -1: history position out of range
imadev:~$ history -d end
bash: history: end: history position out of range

Allowing a negative offset to refer to the last (or last minus however
many) entry would probably satisfy the original request.  Semantically,
there's precedent with negative array indices and negative starting
positions in ${parameter:offset:length} notation.



Re: Feature: Easily remove current command from history

2016-01-04 Thread Dennis Williamson
On Mon, Jan 4, 2016 at 4:05 PM, Dennis Williamson <
dennistwilliam...@gmail.com> wrote:

>
>
> On Mon, Jan 4, 2016 at 3:07 PM, Eduardo A. Bustamante López <
> dual...@gmail.com> wrote:
>
>> Take into account that many options have been provided (history -d, the
>> space
>> prefix, even editing .bash_history yourself).
>>
>> But you request a single key stroke to do this... why?
>>
>> If you enter a password by mistake in your shell, and it gets recorded,
>> then
>> you go and clean up. It's not hard to do.
>>
>> But since you request a simple-and-easy way of doing this, it seems like
>> you do
>> this a lot... which you shouldn't! :-)
>>
>> Now, it is up to you to convince Chet that it is so important to have a
>> simple
>> shortcut to do this. IMO, it isn't.
>>
>> --
>> Eduardo Bustamante
>> https://dualbus.me/
>>
>>
>
> Just bind your own keystroke to a function which uses history -d:
>
> histdel() {
> local last_command histline
>
> last_command=$(history 1)
>
> histline="${last_command%  *}"
>
> history -d "$histline"#  I wish history -d accepted negative
> offsets
> }
>
> bind -x '"\ez": histdel'
>
> Then Esc-z or Alt-z will delete the most recent history entry. You could
> choose another keystroke to bind.
>
>
> --
> Visit serverfault.com to get your system administration questions
> answered.
>

Actually, this is better:

histdel() {

(#  use a subshell to make extglob setting and function variables
local

last_command=$(history 1)

#  strip modified-entry marker, it doesn't matter if we delete an
asterisk in the command since we're deleting it anyway
last_command=${last_command/\*/ }
shopt -s extglob
last_command=${last_command##*( )}  # strip leading spaces
histline="${last_command%%  *}"

history -d "$histline"#  I wish history -d accepted negative offsets

)
}

bind -x '"\ez": histdel'

I'm using a subshell here. You can use the local keyword for variables and
save and restore the extglob setting if you prefer.

-- 
Visit serverfault.com to get your system administration questions answered.


Re: Feature: Easily remove current command from history

2016-01-04 Thread Dennis Williamson
On Mon, Jan 4, 2016 at 3:07 PM, Eduardo A. Bustamante López <
dual...@gmail.com> wrote:

> Take into account that many options have been provided (history -d, the
> space
> prefix, even editing .bash_history yourself).
>
> But you request a single key stroke to do this... why?
>
> If you enter a password by mistake in your shell, and it gets recorded,
> then
> you go and clean up. It's not hard to do.
>
> But since you request a simple-and-easy way of doing this, it seems like
> you do
> this a lot... which you shouldn't! :-)
>
> Now, it is up to you to convince Chet that it is so important to have a
> simple
> shortcut to do this. IMO, it isn't.
>
> --
> Eduardo Bustamante
> https://dualbus.me/
>
>

Just bind your own keystroke to a function which uses history -d:

histdel() {
local last_command histline

last_command=$(history 1)

histline="${last_command%  *}"

history -d "$histline"#  I wish history -d accepted negative offsets
}

bind -x '"\ez": histdel'

Then Esc-z or Alt-z will delete the most recent history entry. You could
choose another keystroke to bind.


-- 
Visit serverfault.com to get your system administration questions answered.


Re: Feature: Easily remove current command from history

2016-01-04 Thread Eduardo A . Bustamante López
Take into account that many options have been provided (history -d, the space
prefix, even editing .bash_history yourself).

But you request a single key stroke to do this... why?

If you enter a password by mistake in your shell, and it gets recorded, then
you go and clean up. It's not hard to do.

But since you request a simple-and-easy way of doing this, it seems like you do
this a lot... which you shouldn't! :-)

Now, it is up to you to convince Chet that it is so important to have a simple
shortcut to do this. IMO, it isn't.

-- 
Eduardo Bustamante
https://dualbus.me/



Re: Feature: Easily remove current command from history

2016-01-04 Thread Victor Porton
On Mon, 2016-01-04 at 15:37 -0500, Greg Wooledge wrote:
> On Mon, Jan 04, 2016 at 09:30:37PM +0100, Piotr Grzybowski wrote:
> > On Mon, Jan 4, 2016 at 9:19 PM, Victor Porton 
> > wrote:
> > > It is important for security:
> > > 
> > > It should be easy to remove lines with passwords and potentially
> > > harmful
> > > commands (like sudo with rm).
> > 
> > never put your passwords in plain text in commands, thats important
> > for security. If, for some reasons, you have to type sensitive data
> > inline, take any precautions available to not to put the command in
> > the history.
> 
> Such measures may include prefixing the command with a space when you
> enter it, if you have ignorespace or ignoreboth in the HISTCONTROL
> variable.
I know. But I sometimes forget to put a space. Also some server
accounts may be not configured for ignorespace.

-- 
Victor Porton - http://portonvictor.org



Re: Feature: Easily remove current command from history

2016-01-04 Thread Greg Wooledge
On Mon, Jan 04, 2016 at 09:30:37PM +0100, Piotr Grzybowski wrote:
> On Mon, Jan 4, 2016 at 9:19 PM, Victor Porton  wrote:
> > It is important for security:
> >
> > It should be easy to remove lines with passwords and potentially harmful
> > commands (like sudo with rm).
> 
> never put your passwords in plain text in commands, thats important
> for security. If, for some reasons, you have to type sensitive data
> inline, take any precautions available to not to put the command in
> the history.

Such measures may include prefixing the command with a space when you
enter it, if you have ignorespace or ignoreboth in the HISTCONTROL
variable.



Re: Feature: Easily remove current command from history

2016-01-04 Thread Andreas Schwab
Victor Porton  writes:

> It should be easy to remove lines with passwords and potentially
> harmful commands (like sudo with rm).

C-a C-k

Andreas.

-- 
Andreas Schwab, sch...@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."



Re: Feature: Easily remove current command from history

2016-01-04 Thread Piotr Grzybowski
On Mon, Jan 4, 2016 at 9:19 PM, Victor Porton  wrote:
> It is important for security:
>
> It should be easy to remove lines with passwords and potentially harmful
> commands (like sudo with rm).

never put your passwords in plain text in commands, thats important
for security. If, for some reasons, you have to type sensitive data
inline, take any precautions available to not to put the command in
the history.

sincerely,
pg



Re: Feature: Easily remove current command from history

2016-01-04 Thread Victor Porton
On Mon, 2016-01-04 at 15:14 -0500, Chet Ramey wrote:
> On 1/4/16 3:09 PM, Victor Porton wrote:
> > On Mon, 2016-01-04 at 14:59 -0500, Chet Ramey wrote:
> > > On 1/4/16 1:30 PM, por...@narod.ru por...@narod.ru>
> > > wrote:
> > > 
> > > > Bash Version: 4.3 Patch Level: 42 Release Status: release
> > > > Description:
> > > > There should be an easy way to remove the currently selected
> > > > command
> > > > from the history. 
> > > 
> > > 
> > > Easier than `history -d'?
> > 
> > Look into my proposal carefully: I propose to do an equivalent of
> > `history
> > -d' with user pressing a single key.
> 
> I did; is that significantly easier, and enough in demand, to make
> the
> implementation effort worthwhile?
It is important for security:
It should be easy to remove lines with passwords and potentially
harmful commands (like sudo with rm).


-- 
Victor Porton - http://portonvictor.org



Re: Feature: Easily remove current command from history

2016-01-04 Thread Chet Ramey
On 1/4/16 3:09 PM, Victor Porton wrote:
> On Mon, 2016-01-04 at 14:59 -0500, Chet Ramey wrote:
>> On 1/4/16 1:30 PM, por...@narod.ru  wrote:
>>
>>> Bash Version: 4.3 Patch Level: 42 Release Status: release Description:
>>> There should be an easy way to remove the currently selected command
>>> from the history. 
>>
>>
>> Easier than `history -d'?
> 
> Look into my proposal carefully: I propose to do an equivalent of `history
> -d' with user pressing a single key.

I did; is that significantly easier, and enough in demand, to make the
implementation effort worthwhile?


-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/



Re: Feature: Easily remove current command from history

2016-01-04 Thread Victor Porton
On Mon, 2016-01-04 at 14:59 -0500, Chet Ramey wrote:
> On 1/4/16 1:30 PM, por...@narod.ru wrote:
> 
> > Bash Version: 4.3
> > Patch Level: 42
> > Release Status: release
> > 
> > Description:
> > 
> > There should be an easy way to remove the currently selected
> > command
> > from the history.
> 
> Easier than `history -d'?
Look into my proposal carefully: I propose to do an equivalent of
`history -d' with user pressing a single key.


-- 
Victor Porton - http://portonvictor.org



Re: Feature: Easily remove current command from history

2016-01-04 Thread Chet Ramey
On 1/4/16 1:30 PM, por...@narod.ru wrote:

> Bash Version: 4.3
> Patch Level: 42
> Release Status: release
> 
> Description:
> 
> There should be an easy way to remove the currently selected command
> from the history.

Easier than `history -d'?

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/



Feature: Easily remove current command from history

2016-01-04 Thread porton
Configuration Information [Automatically generated, do not change]:
Machine: i586
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='i586' 
-DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='i586-pc-linux-gnu' 
-DCONF_VENDOR='pc' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL 
-DHAVE_CONFIG_H   -I.  -I../. -I.././include -I.././lib  -D_FORTIFY_SOURCE=2 -g 
-O2 -fstack-protector-strong -Wformat -Werror=format-security -Wall
uname output: Linux victor.local 4.1.0-2-686-pae #1 SMP Debian 4.1.6-1 
(2015-08-23) i686 GNU/Linux
Machine Type: i586-pc-linux-gnu

Bash Version: 4.3
Patch Level: 42
Release Status: release

Description:

There should be an easy way to remove the currently selected command
from the history.

Fix:

I propose to add new readline command which when it is invoked removed the
current shell command from the history and moves to either the next or the
previous command (not sure whether next of previous).