2

I inserted the 'datetime' snippet from vim-snippets/all.snippet right at the beginning of my document.

The problem is that the time keeps on updating whenever I scroll, as if vim is redrawing the screen and executing the snippet at the same time. What this leads to is, I end up with the time at which the file is closed. Please help me fix the issue.

snippet datetime "YYYY-MM-DD hh:mm" w
`!v strftime("Date: %d %B, %Y | Time: %X")`
endsnippet

UPDATE 1: A gif of the same

UPDATE 2:

base_file

snippet base_file "Competitive Programming Template File" b
/*
**    Date: `!v strftime("%A, %d %B, %Y")`
**    Time: `!v strftime("%X")`
*/

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

#define sz(x) (int)x.size()
#define rep(a, b, i) for (int i = a; i < b; i++)
#define repi(a, b, i) for (int i = a; i > b; i--)
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define __                                                                     \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define CPU                                                                    \
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s." << endl;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

const int MOD = 1e9 + 7;
const ll inf = 1e18 + 5;
const ld pi = 3.141592653589793;
const int maxn = 1e5 + 5;

int32_t main() {
  __;

  int t;
  cin >> t;
  while (t--) {
    ${1}
  }
  return 0;
}
endsnippet

outputs:

:autocmd CursorHold and :autocmd SafeState are empty

:autocmd CursorMoved

--- Autocommands ---
matchparen  CursorMoved
    *         call s:Highlight_Matching_Pair()
ycmcompletemecursormove  CursorMoved
    *         call s:OnCursorMovedNormalMode()
UltiSnips  CursorMoved
    *         call UltiSnips#CursorMoved()

I randomly tried entering this command: :call UltiSnips#CursorMoved() The same thing happened, i.e. the time updated. I think there is some problem with this command.

7
  • Wait, so you went to the first line, typed datetime hit your trigger key, current date and time string appeared, then you scrolled away and the time continuously advances while you're moving? That's wild. Never heard of such a thing. Interference from another plugin? Do you use, errr, YouCompleteMe? Try removing all other plugins except UltiSnips and see if the problem still occurs.
    – B Layer
    Apr 27, 2020 at 14:12
  • @ManasSingh Please update your question to clarify the exact behavior you're seeing. Can you maybe include screenshot or perhaps a video demonstrating it? If you disable other plug-ins, most of what's in your vimrc, do you stop seeing the issue?
    – filbranden
    Apr 28, 2020 at 2:02
  • 1
    @filbranden I have added the gif Apr 28, 2020 at 10:19
  • 1
    @filbranden Thank you for replying. I have added all the outputs. Talking of removing other plugins, UltiSnips triggers were not working when I tried to use my expand trigger. Apr 28, 2020 at 14:23
  • 1
    Yeah I can reproduce this behavior exactly! Looks like this is expected behavior. Well we got an answer. Nice!
    – filbranden
    Apr 28, 2020 at 17:25

1 Answer 1

2

From this video:

UltiSnips evaluates each text object that might have a dependency on some types of text multiple times, to make sure that all dependencies are properly updated.

To prevent the date from being updated several times, you could switch to a python interpolation, and use the guard if not snip.c. For example, you could replace this:

**    Time: `!v strftime("%X")`

With this:

**    Time: `!p
if not snip.c:
    from datetime import datetime
    snip.rv = datetime.now().strftime('%X')`

Alternatively, try to move your cursor outside the snippet to end its expansion; if you can't, run this:

:sil! py3 UltiSnips_Manager._current_snippet_is_done()

For more info about snip.c, read :h UltiSnips-python. See here for a similar issue.

1
  • 1
    This works pretty well. Thanks for answering my question. Apr 28, 2020 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.