kev
kev

Reputation: 161884

The `:bar` in vim doesn't work as expected

After reading the vim doc about *:bar* *:\bar*:

'|' can be used to separate commands, so you can give multiple commands in one line.


I tried to clear history via this command:

:set history=0 | set history=20

I expect that, these two commands should be executed one by one, and the history should be cleared.
But it didn't. The history still there, and a new history was added.


Then I tried:

:set history=0
:set history=20

It works.


Why?

Upvotes: 1

Views: 114

Answers (1)

ZyX
ZyX

Reputation: 53654

When you execute a command line that is what happens:

  1. History is initialized (truncation happens here) (init_hist() function call in getcmdline()).
  2. String to be executed is obtained (in getcmdline()).
  3. It is added to history (add_to_history() call in getcmdline()).
  4. It is executed (do_one_cmd() in do_cmdline(), happens after getcmdline() call (called by getexline() passed as fgetline argument to do_cmdline())).

Setting the option itself does nothing, it only changes p_hi variable. Actual modification of history is done at step 1.

Now let’s see what happens in both cases (assuming that you typed second or third additional command):

  1. History is first initialized when p_hi has old value, then it is changed two times and then when you do next command it is already set to 20.
  2. History is first initialized when p_hi has old value, then it is changed once and initialized for the second time, being effectively truncated at this step and only then the value changes again. By the time you start typing third command it is already wiped out.

Thus your assumption (commands are executed one by one) is true, but you have mistaken at which point history modification happens. I would rather suggest using histdel() as @Michael advised because, at first, it clears history by itself and, at second, it does not have hardcoded 'history' value. I can’t say how you live with just 20 lines long history (I’ve set it to 65535 and am always sure I won’t lose anything small, but useful), but hardcoding this value will make changing history size to something else more painful.

Upvotes: 7

Related Questions