-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
727 lines (434 loc) · 72.7 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.3.0">
<link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
<link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
<link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
<link rel="mask-icon" href="/images/logo.svg" color="#222">
<link rel="stylesheet" href="/css/main.css">
<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
<script id="hexo-configurations">
var NexT = window.NexT || {};
var CONFIG = {"hostname":"yukirasawa.github.io","root":"/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
</script>
<meta name="description" content="摸鱼的地方">
<meta property="og:type" content="website">
<meta property="og:title" content="吉良泽优的小站">
<meta property="og:url" content="https://yukirasawa.github.io/index.html">
<meta property="og:site_name" content="吉良泽优的小站">
<meta property="og:description" content="摸鱼的地方">
<meta property="og:locale" content="zh_CN">
<meta property="article:author" content="吉良泽优">
<meta name="twitter:card" content="summary">
<link rel="canonical" href="https://yukirasawa.github.io/">
<script id="page-configurations">
// https://hexo.io/docs/variables.html
CONFIG.page = {
sidebar: "",
isHome : true,
isPost : false,
lang : 'zh-CN'
};
</script>
<title>吉良泽优的小站</title>
<noscript>
<style>
.use-motion .brand,
.use-motion .menu-item,
.sidebar-inner,
.use-motion .post-block,
.use-motion .pagination,
.use-motion .comments,
.use-motion .post-header,
.use-motion .post-body,
.use-motion .collection-header { opacity: initial; }
.use-motion .site-title,
.use-motion .site-subtitle {
opacity: initial;
top: initial;
}
.use-motion .logo-line-before i { left: initial; }
.use-motion .logo-line-after i { right: initial; }
</style>
</noscript>
<link rel="alternate" href="/atom.xml" title="吉良泽优的小站" type="application/atom+xml">
<link rel="alternate" href="/rss2.xml" title="吉良泽优的小站" type="application/rss+xml">
</head>
<body itemscope itemtype="http://schema.org/WebPage">
<div class="container use-motion">
<div class="headband"></div>
<header class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-brand-container">
<div class="site-nav-toggle">
<div class="toggle" aria-label="切换导航栏">
<span class="toggle-line toggle-line-first"></span>
<span class="toggle-line toggle-line-middle"></span>
<span class="toggle-line toggle-line-last"></span>
</div>
</div>
<div class="site-meta">
<a href="/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<h1 class="site-title">吉良泽优的小站</h1>
<span class="logo-line-after"><i></i></span>
</a>
</div>
<div class="site-nav-right">
<div class="toggle popup-trigger">
</div>
</div>
</div>
<nav class="site-nav">
<ul id="menu" class="main-menu menu">
<li class="menu-item menu-item-home">
<a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>
</li>
<li class="menu-item menu-item-about">
<a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>
</li>
</ul>
</nav>
</div>
</header>
<div class="back-to-top">
<i class="fa fa-arrow-up"></i>
<span>0%</span>
</div>
<main class="main">
<div class="main-inner">
<div class="content-wrap">
<div class="content index posts-expand">
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="https://yukirasawa.github.io/2023/11/23/tui%E6%9D%82%E8%AE%B0/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.jpg">
<meta itemprop="name" content="吉良泽优">
<meta itemprop="description" content="摸鱼的地方">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="吉良泽优的小站">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2023/11/23/tui%E6%9D%82%E8%AE%B0/" class="post-title-link" itemprop="url">tui杂记</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2023-11-23 09:14:44 / 修改时间:09:18:42" itemprop="dateCreated datePublished" datetime="2023-11-23T09:14:44+08:00">2023-11-23</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="isatty"><a href="#isatty" class="headerlink" title="isatty"></a>isatty</h2><p>在终端中,许多程序会将输出进行配色等的美化。但当输出被重定向到文件中时,我们肯定不希望这些控制字符被写入文件。所以一些程序会使用 <code>isatty</code> 判断当前得标准输出是不是连接到了终端上,从而自动使用合理的输出方式。</p>
<p>一个 python 的 demo 如下</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">import</span> sys</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> os.isatty(sys.stdout.fileno()):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">"tty find"</span>)</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">"No tty"</span>)</span><br></pre></td></tr></table></figure>
<p>当标准输出在终端中且没有重定向时,会输出 <code>tty find</code>,否则输出 <code>No tty</code></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">➜ python tty_test.py </span><br><span class="line">tty find</span><br><span class="line">➜ python tty_test.py | cat</span><br><span class="line">No tty</span><br></pre></td></tr></table></figure>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="https://yukirasawa.github.io/2023/10/16/windows%E8%99%9A%E6%8B%9F%E6%9C%BA%E8%B8%A9%E5%9D%91/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.jpg">
<meta itemprop="name" content="吉良泽优">
<meta itemprop="description" content="摸鱼的地方">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="吉良泽优的小站">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2023/10/16/windows%E8%99%9A%E6%8B%9F%E6%9C%BA%E8%B8%A9%E5%9D%91/" class="post-title-link" itemprop="url">windows虚拟机踩坑</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2023-10-16 09:27:33 / 修改时间:09:36:12" itemprop="dateCreated datePublished" datetime="2023-10-16T09:27:33+08:00">2023-10-16</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>在使用 virtualbox guest windows 安装 office 时,虚拟机会在安装程序启动时卡死。经过搜索找到了<a target="_blank" rel="noopener" href="https://forums.virtualbox.org/viewtopic.php?t=96533">这篇讨论</a>,其中指出 windows10 guest 需要打开虚拟机的 PAE/NX 选项。经过尝试打开该选项之后就可以正常安装 office。</p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="https://yukirasawa.github.io/2023/05/17/C%E8%AF%AD%E8%A8%80%E4%B8%AD%E5%BC%B1%E7%AC%A6%E5%8F%B7%E7%9C%9F%E7%9A%84%E5%AD%98%E5%9C%A8%E5%90%97/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.jpg">
<meta itemprop="name" content="吉良泽优">
<meta itemprop="description" content="摸鱼的地方">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="吉良泽优的小站">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2023/05/17/C%E8%AF%AD%E8%A8%80%E4%B8%AD%E5%BC%B1%E7%AC%A6%E5%8F%B7%E7%9C%9F%E7%9A%84%E5%AD%98%E5%9C%A8%E5%90%97/" class="post-title-link" itemprop="url">C语言中弱符号真的存在吗</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2023-05-17 22:05:19 / 修改时间:22:09:08" itemprop="dateCreated datePublished" datetime="2023-05-17T22:05:19+08:00">2023-05-17</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>不存在,至少在 C 语言标准中是这样。<a target="_blank" rel="noopener" href="https://www.zhihu.com/question/600255688/answer/3022351083">这里</a>的解释非常清楚。所以如果需要在多个文件中声明同一个变量,稳妥的方法就是只在一个地方定义,其他地方显式使用<code>extern</code> 修饰。</p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="https://yukirasawa.github.io/2023/04/02/%E6%AF%94%E7%89%B9%E5%B8%81%E6%8C%96%E7%9F%BF%E5%88%B0%E5%BA%95%E5%9C%A8%E7%AE%97%E4%BB%80%E4%B9%88/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.jpg">
<meta itemprop="name" content="吉良泽优">
<meta itemprop="description" content="摸鱼的地方">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="吉良泽优的小站">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2023/04/02/%E6%AF%94%E7%89%B9%E5%B8%81%E6%8C%96%E7%9F%BF%E5%88%B0%E5%BA%95%E5%9C%A8%E7%AE%97%E4%BB%80%E4%B9%88/" class="post-title-link" itemprop="url">比特币挖矿到底在算什么</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2023-04-02 10:36:39 / 修改时间:22:14:48" itemprop="dateCreated datePublished" datetime="2023-04-02T10:36:39+08:00">2023-04-02</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="PoW"><a href="#PoW" class="headerlink" title="PoW"></a>PoW</h2><p>众所周知,比特币网络以及其他使用 PoW 的区块链网络都通过挖矿保证网络的安全性。其原理大致就是通过算力的分散性实现区块产生能力的分散,从而避免网络被少数攻击者操纵。</p>
<p>进一步地,比特币网络使用的是基于双 SHA256 的碰撞的 PoW,矿工需要得到一个拥有小于网络要求的双 SHA256 值的区块,这样的区块可以被网络接受,矿工也会获得对应的奖励。由于(目前)没有高效的双 SHA256 的碰撞算法,唯一的方法就是暴力尝试。因此,比特币挖矿就是在算大量区块的双 SHA256 值,直到找到满足网络要求的区块。</p>
<h2 id="nonce-字段"><a href="#nonce-字段" class="headerlink" title="nonce 字段"></a>nonce 字段</h2><p><del>到这里,我们已经解决了题目中的问题,本文也就完结了。</del>接下来的问题是:这些允许矿工枚举的区块是怎么产生的。容易想象,作为包含了交易信息的区块,其中的大部分内容应该是固定的,那么一个比较合理的方法是设计一个独立的字段用于挖矿。比特币的区块结构中也确实设计了一个 4 字节的 nonce 字段用于矿工挖矿。</p>
<h2 id="不只是-nonce-字段"><a href="#不只是-nonce-字段" class="headerlink" title="不只是 nonce 字段"></a>不只是 nonce 字段</h2><p><del>到这里,我们又解决了前面提出的问题,本文再一次完结了。</del>用脚趾头数一下就可以发现,4 字节的 nonce 在现在根本不够产生足够挖矿的区块。现在一台比特币矿机的算力可能都有数十乃至数百 T,一秒钟就能把整个 nonce 字段枚举很多遍了。因此,仅仅使用 nonce 进行挖矿是完全不够的。因此,矿工还需要在其他的位置进行枚举。</p>
<p>在这种情况下,矿工将目光转向了 coinbase 交易,coinbase 交易是矿工构造的为自己提供挖矿奖励的交易。相比于其他交易,coinbase 交易既没有资金输入,也不需要签名。因此,coinbase 交易被规定在签名位置可以放指定前缀的任意值(<a target="_blank" rel="noopener" href="https://learnmeabitcoin.com/technical/coinbase-transaction#footnote-bip34">bip34</a>要求),而交易信息会通过一个称为 Merkle Tree 的结构打包到区块中,从而修改 coinbase 交易可以改变区块的哈希值,矿工可以修改这里的值来得到足够的可枚举范围。(同时这里往往也会放一些矿场信息之类的私货)</p>
<h2 id="参考来源"><a href="#参考来源" class="headerlink" title="参考来源"></a>参考来源</h2><p><a target="_blank" rel="noopener" href="https://learn.saylor.org/mod/book/view.php?id=36375&chapterid=19428">https://learn.saylor.org/mod/book/view.php?id=36375&chapterid=19428</a></p>
<p><a target="_blank" rel="noopener" href="https://learnmeabitcoin.com/technical/coinbase-transaction#footnote-messages">https://learnmeabitcoin.com/technical/coinbase-transaction#footnote-messages</a></p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="https://yukirasawa.github.io/2023/02/15/neovim-%E9%85%8D%E7%BD%AE%E8%BF%81%E7%A7%BB%E8%AE%B0%E5%BD%95/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.jpg">
<meta itemprop="name" content="吉良泽优">
<meta itemprop="description" content="摸鱼的地方">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="吉良泽优的小站">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2023/02/15/neovim-%E9%85%8D%E7%BD%AE%E8%BF%81%E7%A7%BB%E8%AE%B0%E5%BD%95/" class="post-title-link" itemprop="url">neovim 配置迁移记录</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2023-02-15 19:11:32" itemprop="dateCreated datePublished" datetime="2023-02-15T19:11:32+08:00">2023-02-15</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar-check"></i>
</span>
<span class="post-meta-item-text">更新于</span>
<time title="修改时间:2023-02-18 14:52:59" itemprop="dateModified" datetime="2023-02-18T14:52:59+08:00">2023-02-18</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>(上一次配 vim 还是上大学之前,转眼就快本科毕业了。)</p>
<p>这次主要是把一部分 <code>.vimrc</code> 中的配置迁移到 neovim 的 lua 风格配置上,同时把一部分插件迁移到 neovim 的生态中。</p>
<h2 id="基本配置"><a href="#基本配置" class="headerlink" title="基本配置"></a>基本配置</h2><p>配置主要存放在 <code>~/.config/nvim</code>,目录结构大致为</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">├── init.lua</span><br><span class="line">└── lua</span><br><span class="line"> ├── core</span><br><span class="line"> └── plugins</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>其中 <code>core</code> 中存放全局配置,<code>plugins</code> 中存放插件相关配置,<code>init.lua</code> 中引用各个配置,因此后面每个文件配置时都需要在 <code>init.lua</code> 中 <code>require</code>。</p>
<p>为了配置迁移的平滑性,先在 <code>init.lua</code> 中加入下面的代码,使其读取原本的 <code>.vimrc</code> 文件</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">vim.cmd <span class="string">[[</span></span><br><span class="line"><span class="string">set runtimepath^=~/.vim</span></span><br><span class="line"><span class="string">let &packpath = &runtimepath</span></span><br><span class="line"><span class="string">source ~/.vimrc</span></span><br><span class="line"><span class="string">]]</span></span><br></pre></td></tr></table></figure>
<p>在 <code>core</code> 文件夹中,创建 <code>options.lua</code> 用于配置 neovim 的选项,我的配置如下</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 基础配置</span></span><br><span class="line"><span class="keyword">local</span> opt = vim.opt</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 不创建交换文件</span></span><br><span class="line">opt.swapfile = <span class="literal">false</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 搜索</span></span><br><span class="line">opt.ignorecase = <span class="literal">true</span></span><br><span class="line">opt.smartcase = <span class="literal">true</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 真彩色</span></span><br><span class="line">opt.termguicolors = <span class="literal">true</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 缩进</span></span><br><span class="line">opt.tabstop = <span class="number">4</span></span><br><span class="line">opt.shiftwidth = <span class="number">4</span></span><br><span class="line">opt.expandtab = <span class="literal">true</span></span><br><span class="line">opt.autoindent = <span class="literal">true</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 使用鼠标</span></span><br><span class="line">opt.mouse:append(<span class="string">"a"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 使 gg 等命令把光标移动到行首的第一个非空白字符</span></span><br><span class="line">opt.startofline = <span class="literal">true</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 关闭自动换行</span></span><br><span class="line">opt.<span class="built_in">wrap</span> = <span class="literal">false</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 突出当前行</span></span><br><span class="line">opt.cursorline = <span class="literal">true</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 使用系统剪切板</span></span><br><span class="line">opt.clipboard:append(<span class="string">"unnamedplus"</span>)</span><br></pre></td></tr></table></figure>
<p>在 <code>core</code> 文件夹中,创建 <code>keymaps.lua</code> 用于配置键位映射,我的配置如下</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 按键映射</span></span><br><span class="line"><span class="keyword">local</span> keymap = vim.keymap</span><br><span class="line"></span><br><span class="line">keymap.set(<span class="string">"i"</span>, <span class="string">"jk"</span>, <span class="string">"<ESC>"</span>, {noremap = <span class="literal">true</span>, silent = <span class="literal">true</span>})</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 一键编译运行 (SingleCompile)</span></span><br><span class="line">keymap.set(<span class="string">"n"</span>, <span class="string">"<F9>"</span>, <span class="string">":SCCompile <cr>"</span>)</span><br><span class="line">keymap.set(<span class="string">"n"</span>, <span class="string">"<F10>"</span>, <span class="string">":SCCompileRun <cr>"</span>)</span><br></pre></td></tr></table></figure>
<p>上面这些基本是从 <code>.vimrc</code> 中直接翻译过来</p>
<h2 id="packer"><a href="#packer" class="headerlink" title="packer"></a>packer</h2><p>我原本使用 <code>plugged</code> 进行插件管理,这次将新的插件使用 <code>packer</code> 配置,之后会把 <code>plugged</code> 中的插件逐步替换和迁移过来。</p>
<p>在 <code>plugins</code> 文件夹中创建 <code>packer-setup.lua</code> 用于 <code>packer</code> 的自动安装。我这里使用的安装目录是 <code>~/.local/share/nvim/site/pack/packer</code>,后面通过 <code>packer</code> 安装的插件也会存放在这个目录。</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">local</span> packer_setup = {}</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 检查和安装 packer</span></span><br><span class="line">packer_setup.ensure_packer = <span class="function"><span class="keyword">function</span><span class="params">()</span></span></span><br><span class="line"> <span class="keyword">local</span> fn = vim.fn</span><br><span class="line"> <span class="keyword">local</span> install_path = fn.stdpath(<span class="string">'data'</span>)..<span class="string">'/site/pack/packer/start/packer.nvim'</span></span><br><span class="line"> <span class="keyword">if</span> fn.empty(fn.glob(install_path)) > <span class="number">0</span> <span class="keyword">then</span></span><br><span class="line"> fn.system({<span class="string">'git'</span>, <span class="string">'clone'</span>, <span class="string">'--depth'</span>, <span class="string">'1'</span>, <span class="string">'https://github.com/wbthomason/packer.nvim'</span>, install_path})</span><br><span class="line"> vim.cmd <span class="string">[[packadd packer.nvim]]</span></span><br><span class="line"> <span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line"> <span class="keyword">end</span></span><br><span class="line"> <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line"><span class="keyword">end</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> packer_setup</span><br></pre></td></tr></table></figure>
<p>在 <code>plugins</code> 文件夹中创建 <code>packer.lua</code> 用于 <code>packer</code> 的插件管理。安装插件只需要在这里添加 <code>use</code> 语句。</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 保存本文件时自动更新插件</span></span><br><span class="line">vim.cmd(<span class="string">[[</span></span><br><span class="line"><span class="string"> augroup packer_user_config</span></span><br><span class="line"><span class="string"> autocmd!</span></span><br><span class="line"><span class="string"> autocmd BufWritePost packer.lua source <afile> | PackerSync</span></span><br><span class="line"><span class="string"> augroup end</span></span><br><span class="line"><span class="string">]]</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">local</span> packer_bootstrap = <span class="built_in">require</span>(<span class="string">"plugins.packer-setup"</span>).ensure_packer()</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="built_in">require</span>(<span class="string">'packer'</span>).startup(<span class="function"><span class="keyword">function</span><span class="params">(use)</span></span></span><br><span class="line"> use <span class="string">'wbthomason/packer.nvim'</span></span><br><span class="line"> <span class="comment">-- My plugins here</span></span><br><span class="line"> use <span class="string">'neovim/nvim-lspconfig'</span> <span class="comment">-- Configurations for Nvim LSP</span></span><br><span class="line"> use <span class="string">'hrsh7th/nvim-cmp'</span> <span class="comment">-- Autocompletion plugin</span></span><br><span class="line"> </span><br><span class="line"> use <span class="string">'hrsh7th/cmp-nvim-lsp'</span> <span class="comment">-- LSP source for nvim-cmp</span></span><br><span class="line"> use <span class="string">'hrsh7th/cmp-nvim-lsp-signature-help'</span> <span class="comment">-- nvim-cmp source for displaying function signatures</span></span><br><span class="line"> </span><br><span class="line"> use <span class="string">'hrsh7th/cmp-path'</span> <span class="comment">-- path source for nvim-cmp</span></span><br><span class="line"> use <span class="string">'hrsh7th/cmp-buffer'</span> <span class="comment">-- buffer source for nvim-cmp</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">-- LuaSnip, not used now</span></span><br><span class="line"> use({<span class="string">"L3MON4D3/LuaSnip"</span>, run = <span class="string">"make install_jsregexp"</span>})</span><br><span class="line"> use <span class="string">'saadparwaiz1/cmp_luasnip'</span> <span class="comment">-- LuaSnip source for nvim-cmp</span></span><br><span class="line"> </span><br><span class="line"> use <span class="string">'sirver/ultisnips'</span></span><br><span class="line"> use <span class="string">'honza/vim-snippets'</span> <span class="comment">-- snippets collection for ultisnips</span></span><br><span class="line"> use <span class="string">'quangnguyen30192/cmp-nvim-ultisnips'</span> <span class="comment">-- ultisnips source for nvim-cmp</span></span><br><span class="line"></span><br><span class="line"> use {<span class="string">'iamcco/markdown-preview.nvim'</span>, run = <span class="string">'cd app && yarn install'</span>, cmd = <span class="string">'MarkdownPreview'</span>}</span><br><span class="line"></span><br><span class="line"> <span class="comment">-- File Explorer</span></span><br><span class="line"> use {</span><br><span class="line"> <span class="string">'nvim-tree/nvim-tree.lua'</span>,</span><br><span class="line"> requires = {</span><br><span class="line"> <span class="string">'nvim-tree/nvim-web-devicons'</span>, <span class="comment">-- for file icons</span></span><br><span class="line"> },</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">-- treesitter and its module</span></span><br><span class="line"> use {</span><br><span class="line"> <span class="string">'nvim-treesitter/nvim-treesitter'</span>,</span><br><span class="line"> run = <span class="string">':TSUpdate'</span></span><br><span class="line"> }</span><br><span class="line"> use <span class="string">'p00f/nvim-ts-rainbow'</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">-- statusline</span></span><br><span class="line"> use {</span><br><span class="line"> <span class="string">'nvim-lualine/lualine.nvim'</span>,</span><br><span class="line"> requires = { <span class="string">'kyazdani42/nvim-web-devicons'</span>, opt = <span class="literal">true</span> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">-- colorsheme</span></span><br><span class="line"> use <span class="string">'rebelot/kanagawa.nvim'</span></span><br><span class="line"> use { <span class="string">'embark-theme/vim'</span>, as = <span class="string">'embark'</span> }</span><br><span class="line"> <span class="comment">-- Automatically set up your configuration after cloning packer.nvim</span></span><br><span class="line"> <span class="comment">-- Put this at the end after all plugins</span></span><br><span class="line"> <span class="keyword">if</span> packer_bootstrap <span class="keyword">then</span></span><br><span class="line"> <span class="built_in">require</span>(<span class="string">'packer'</span>).sync()</span><br><span class="line"> <span class="keyword">end</span></span><br><span class="line"><span class="keyword">end</span>)</span><br></pre></td></tr></table></figure>
<h2 id="LSP-和补全"><a href="#LSP-和补全" class="headerlink" title="LSP 和补全"></a>LSP 和补全</h2><p>neovim 的主要特性之一就是添加了对 Language Server Protocol 的原生支持,我也把原本基于 YouCompleteMe 的补全迁移到 Nvim LSP client 上。用到的插件主要有 <code>nvim-lspconfig</code>、<code>nvim-cmp</code>、<code>cmp-nvim-lsp</code>,还有一些其他的插件提供辅助功能,具体可以看上面的插件列表。我尝试了 <code>LuaSnip</code>,但是感觉不如一直使用的 <code>ultisnips</code> 顺手,所以目前继续使用 <code>ultisnips</code>。LSP 协议支持自定义 style 的 formatting,但 clangd 对这个特性的支持并不好(来源: <a target="_blank" rel="noopener" href="https://clangd.llvm.org/features#formatting%EF%BC%89%EF%BC%8C%E5%9B%A0%E6%AD%A4%E6%88%91%E4%BF%9D%E7%95%99%E4%BA%86%E5%8E%9F%E6%9C%AC%E4%BD%BF%E7%94%A8%E7%9A%84">https://clangd.llvm.org/features#formatting),因此我保留了原本使用的</a> vim-autoformat 插件用于 formatting。</p>
<p>主要的配置文件就是配置 LSP 的 <code>lspconfig.lua</code> 和配置补全的 <code>nvim-cmp.lua</code>,其中 LSP 的键位基本是默认的,补全则根据以前使用的键位做了一些配置</p>
<p><code>lspconfig.lua</code></p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- Mappings.</span></span><br><span class="line"><span class="comment">-- See `:help vim.diagnostic.*` for documentation on any of the below functions</span></span><br><span class="line"><span class="keyword">local</span> opts = { noremap=<span class="literal">true</span>, silent=<span class="literal">true</span> }</span><br><span class="line">vim.keymap.set(<span class="string">'n'</span>, <span class="string">'<space>e'</span>, vim.diagnostic.open_float, opts)</span><br><span class="line">vim.keymap.set(<span class="string">'n'</span>, <span class="string">'[d'</span>, vim.diagnostic.goto_prev, opts)</span><br><span class="line">vim.keymap.set(<span class="string">'n'</span>, <span class="string">']d'</span>, vim.diagnostic.goto_next, opts)</span><br><span class="line">vim.keymap.set(<span class="string">'n'</span>, <span class="string">'<space>q'</span>, vim.diagnostic.setloclist, opts)</span><br><span class="line"></span><br><span class="line"><span class="comment">-- Use an on_attach function to only map the following keys</span></span><br><span class="line"><span class="comment">-- after the language server attaches to the current buffer</span></span><br><span class="line"><span class="keyword">local</span> on_attach = <span class="function"><span class="keyword">function</span><span class="params">(client, bufnr)</span></span></span><br><span class="line"> <span class="comment">-- Enable completion triggered by <c-x><c-o></span></span><br><span class="line"> vim.api.nvim_buf_set_option(bufnr, <span class="string">'omnifunc'</span>, <span class="string">'v:lua.vim.lsp.omnifunc'</span>)</span><br><span class="line"></span><br><span class="line"> <span class="comment">-- Mappings.</span></span><br><span class="line"> <span class="comment">-- See `:help vim.lsp.*` for documentation on any of the below functions</span></span><br><span class="line"> <span class="keyword">local</span> bufopts = { noremap=<span class="literal">true</span>, silent=<span class="literal">true</span>, buffer=bufnr }</span><br><span class="line"> vim.keymap.set(<span class="string">'n'</span>, <span class="string">'gD'</span>, vim.lsp.buf.declaration, bufopts)</span><br><span class="line"> vim.keymap.set(<span class="string">'n'</span>, <span class="string">'gd'</span>, vim.lsp.buf.definition, bufopts)</span><br><span class="line"> vim.keymap.set(<span class="string">'n'</span>, <span class="string">'K'</span>, vim.lsp.buf.hover, bufopts)</span><br><span class="line"> vim.keymap.set(<span class="string">'n'</span>, <span class="string">'gi'</span>, vim.lsp.buf.implementation, bufopts)</span><br><span class="line"> vim.keymap.set(<span class="string">'n'</span>, <span class="string">'<C-k>'</span>, vim.lsp.buf.signature_help, bufopts)</span><br><span class="line"> vim.keymap.set(<span class="string">'n'</span>, <span class="string">'<space>wa'</span>, vim.lsp.buf.add_workspace_folder, bufopts)</span><br><span class="line"> vim.keymap.set(<span class="string">'n'</span>, <span class="string">'<space>wr'</span>, vim.lsp.buf.remove_workspace_folder, bufopts)</span><br><span class="line"> vim.keymap.set(<span class="string">'n'</span>, <span class="string">'<space>wl'</span>, <span class="function"><span class="keyword">function</span><span class="params">()</span></span></span><br><span class="line"> <span class="built_in">print</span>(vim.inspect(vim.lsp.buf.list_workspace_folders()))</span><br><span class="line"> <span class="keyword">end</span>, bufopts)</span><br><span class="line"> vim.keymap.set(<span class="string">'n'</span>, <span class="string">'<space>D'</span>, vim.lsp.buf.type_definition, bufopts)</span><br><span class="line"> vim.keymap.set(<span class="string">'n'</span>, <span class="string">'<space>rn'</span>, vim.lsp.buf.<span class="built_in">rename</span>, bufopts)</span><br><span class="line"> vim.keymap.set(<span class="string">'n'</span>, <span class="string">'<space>ca'</span>, vim.lsp.buf.code_action, bufopts)</span><br><span class="line"> vim.keymap.set(<span class="string">'n'</span>, <span class="string">'gr'</span>, vim.lsp.buf.references, bufopts)</span><br><span class="line"> vim.keymap.set(<span class="string">'n'</span>, <span class="string">'<space>f'</span>, <span class="function"><span class="keyword">function</span><span class="params">()</span></span> vim.lsp.buf.<span class="built_in">format</span> { async = <span class="literal">true</span> } <span class="keyword">end</span>, bufopts)</span><br><span class="line"><span class="keyword">end</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">local</span> lsp_flags = {</span><br><span class="line"> <span class="comment">-- This is the default in Nvim 0.7+</span></span><br><span class="line"> debounce_text_changes = <span class="number">150</span>,</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">-- Add additional capabilities supported by nvim-cmp</span></span><br><span class="line"><span class="keyword">local</span> capabilities = <span class="built_in">require</span>(<span class="string">"cmp_nvim_lsp"</span>).default_capabilities()</span><br><span class="line"></span><br><span class="line"><span class="keyword">local</span> lspconfig = <span class="built_in">require</span>(<span class="string">'lspconfig'</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">-- Enable some language servers with the additional completion capabilities offered by nvim-cmp</span></span><br><span class="line"><span class="keyword">local</span> servers = { <span class="string">'clangd'</span>, <span class="string">'rust_analyzer'</span>, <span class="string">'pyright'</span>, <span class="string">'tsserver'</span> }</span><br><span class="line"><span class="keyword">for</span> _, lsp <span class="keyword">in</span> <span class="built_in">ipairs</span>(servers) <span class="keyword">do</span></span><br><span class="line"> lspconfig[lsp].setup {</span><br><span class="line"> on_attach = on_attach,</span><br><span class="line"> flags = lsp_flags,</span><br><span class="line"> capabilities = capabilities,</span><br><span class="line"> }</span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure>
<p><code>nvim-cmp.lua</code></p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- ultisnips setup</span></span><br><span class="line"><span class="keyword">local</span> cmp_ultisnips_mappings = <span class="built_in">require</span>(<span class="string">"cmp_nvim_ultisnips.mappings"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">-- nvim-cmp setup</span></span><br><span class="line"><span class="keyword">local</span> cmp = <span class="built_in">require</span> <span class="string">'cmp'</span></span><br><span class="line">cmp.setup {</span><br><span class="line"> snippet = {</span><br><span class="line"> expand = <span class="function"><span class="keyword">function</span><span class="params">(args)</span></span></span><br><span class="line"> <span class="comment">-- luasnip.lsp_expand(args.body)</span></span><br><span class="line"> vim.fn[<span class="string">"UltiSnips#Anon"</span>](args.body)</span><br><span class="line"> <span class="keyword">end</span>,</span><br><span class="line"> },</span><br><span class="line"> mapping = cmp.mapping.preset.<span class="built_in">insert</span>({</span><br><span class="line"> <span class="comment">-- 上一个</span></span><br><span class="line"> [<span class="string">'<C-k>'</span>] = cmp.mapping(<span class="function"><span class="keyword">function</span><span class="params">(fallback)</span></span></span><br><span class="line"> <span class="keyword">if</span> cmp.visible() <span class="keyword">then</span></span><br><span class="line"> cmp.select_prev_item()</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> cmp_ultisnips_mappings.jump_backwards(fallback)</span><br><span class="line"> <span class="keyword">end</span></span><br><span class="line"> <span class="keyword">end</span>, { <span class="string">'i'</span>, <span class="string">'s'</span> }),</span><br><span class="line"> [<span class="string">'<Up>'</span>] = cmp.mapping.select_prev_item(),</span><br><span class="line"> <span class="comment">-- 下一个</span></span><br><span class="line"> [<span class="string">'<C-j>'</span>] = cmp.mapping.select_next_item(),</span><br><span class="line"> [<span class="string">'<Down>'</span>] = cmp.mapping.select_next_item(),</span><br><span class="line"> </span><br><span class="line"> [<span class="string">'<C-u>'</span>] = cmp.mapping.scroll_docs(<span class="number">-4</span>), <span class="comment">-- Up</span></span><br><span class="line"> [<span class="string">'<C-d>'</span>] = cmp.mapping.scroll_docs(<span class="number">4</span>), <span class="comment">-- Down</span></span><br><span class="line"></span><br><span class="line"> [<span class="string">'<C-z>'</span>] = cmp.mapping.complete(), <span class="comment">-- 打开补全列表</span></span><br><span class="line"> [<span class="string">'<CR>'</span>] = cmp.mapping.confirm {</span><br><span class="line"> behavior = cmp.ConfirmBehavior.Replace,</span><br><span class="line"> <span class="built_in">select</span> = <span class="literal">false</span>,</span><br><span class="line"> },</span><br><span class="line"> [<span class="string">'<C-a>'</span>] = cmp.mapping(<span class="function"><span class="keyword">function</span><span class="params">(fallback)</span></span></span><br><span class="line"> cmp_ultisnips_mappings.expand_or_jump_forwards(fallback)</span><br><span class="line"> <span class="keyword">end</span>, { <span class="string">'i'</span>, <span class="string">'s'</span> }),</span><br><span class="line"> [<span class="string">'<Tab>'</span>] = cmp.mapping(<span class="function"><span class="keyword">function</span><span class="params">(fallback)</span></span></span><br><span class="line"> <span class="keyword">if</span> cmp.visible() <span class="keyword">then</span></span><br><span class="line"> cmp.select_next_item()</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> cmp_ultisnips_mappings.expand_or_jump_forwards(fallback)</span><br><span class="line"> <span class="keyword">end</span></span><br><span class="line"> <span class="keyword">end</span>, { <span class="string">'i'</span>, <span class="string">'s'</span> }),</span><br><span class="line"> [<span class="string">'<S-Tab>'</span>] = cmp.mapping(<span class="function"><span class="keyword">function</span><span class="params">(fallback)</span></span></span><br><span class="line"> <span class="keyword">if</span> cmp.visible() <span class="keyword">then</span></span><br><span class="line"> cmp.select_prev_item()</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> cmp_ultisnips_mappings.jump_backwards(fallback)</span><br><span class="line"> <span class="keyword">end</span></span><br><span class="line"> <span class="keyword">end</span>, { <span class="string">'i'</span>, <span class="string">'s'</span> }),</span><br><span class="line"> }),</span><br><span class="line"> sources = {</span><br><span class="line"> { name = <span class="string">'nvim_lsp'</span> },</span><br><span class="line"> { name = <span class="string">'nvim_lsp_signature_help'</span> },</span><br><span class="line"> { name = <span class="string">'ultisnips'</span> }, </span><br><span class="line"> { name = <span class="string">'path'</span> },</span><br><span class="line"> { name = <span class="string">'buffer'</span> },</span><br><span class="line"> },</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="nvim-tree"><a href="#nvim-tree" class="headerlink" title="nvim-tree"></a>nvim-tree</h2><p>添加了在打开目录时调用 <code>nvim-tree</code> 的 autocmd</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- disable netrw</span></span><br><span class="line">vim.g.loaded_netrw = <span class="number">1</span></span><br><span class="line">vim.g.loaded_netrwPlugin = <span class="number">1</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">require</span>(<span class="string">"nvim-tree"</span>).setup({</span><br><span class="line"> sort_by = <span class="string">"case_sensitive"</span>,</span><br><span class="line"> </span><br><span class="line"> view = {</span><br><span class="line"> width = <span class="number">30</span>,</span><br><span class="line"> mappings = {</span><br><span class="line"> list = {</span><br><span class="line"> { key = <span class="string">"u"</span>, action = <span class="string">"dir_up"</span> },</span><br><span class="line"> },</span><br><span class="line"> },</span><br><span class="line"> },</span><br><span class="line"> renderer = {</span><br><span class="line"> group_empty = <span class="literal">true</span>,</span><br><span class="line"> },</span><br><span class="line"> filters = {</span><br><span class="line"> dotfiles = <span class="literal">true</span>,</span><br><span class="line"> },</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line"><span class="comment">-- open tree when open or change to directory</span></span><br><span class="line"><span class="keyword">local</span> <span class="function"><span class="keyword">function</span> <span class="title">open_nvim_tree</span><span class="params">(data)</span></span></span><br><span class="line"> <span class="comment">-- buffer is a directory</span></span><br><span class="line"> <span class="keyword">local</span> directory = vim.fn.isdirectory(data.file) == <span class="number">1</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> directory <span class="keyword">then</span></span><br><span class="line"> <span class="keyword">return</span></span><br><span class="line"> <span class="keyword">end</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">-- change to the directory</span></span><br><span class="line"> vim.cmd.cd(data.file)</span><br><span class="line"></span><br><span class="line"> <span class="comment">-- open the tree</span></span><br><span class="line"> <span class="built_in">require</span>(<span class="string">"nvim-tree.api"</span>).tree.<span class="built_in">open</span>()</span><br><span class="line"><span class="keyword">end</span></span><br><span class="line"></span><br><span class="line">vim.api.nvim_create_autocmd({ <span class="string">"VimEnter"</span> }, { callback = open_nvim_tree })</span><br></pre></td></tr></table></figure>
<h2 id="nvim-treesitter"><a href="#nvim-treesitter" class="headerlink" title="nvim-treesitter"></a>nvim-treesitter</h2><p>配置了高亮、折叠和彩虹括号(需要 <code>p00f/nvim-ts-rainbow</code>)</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">require</span><span class="string">'nvim-treesitter.configs'</span>.setup {</span><br><span class="line"> <span class="comment">-- A list of parser names, or "all" (the four listed parsers should always be installed)</span></span><br><span class="line"> ensure_installed = { <span class="string">"c"</span>, <span class="string">"lua"</span>, <span class="string">"vim"</span>, <span class="string">"help"</span>, <span class="string">"cpp"</span>, <span class="string">"python"</span> },</span><br><span class="line"></span><br><span class="line"> <span class="comment">-- Install parsers synchronously (only applied to `ensure_installed`)</span></span><br><span class="line"> sync_install = <span class="literal">false</span>,</span><br><span class="line"></span><br><span class="line"> <span class="comment">-- Automatically install missing parsers when entering buffer</span></span><br><span class="line"> <span class="comment">-- Recommendation: set to false if you don't have `tree-sitter` CLI installed locally</span></span><br><span class="line"> auto_install = <span class="literal">true</span>,</span><br><span class="line"></span><br><span class="line"> highlight = {</span><br><span class="line"> enable = <span class="literal">true</span>,</span><br><span class="line"> </span><br><span class="line"> <span class="comment">-- <span class="doctag">NOTE:</span> these are the names of the parsers and not the filetype. (for example if you want to</span></span><br><span class="line"> <span class="comment">-- disable highlighting for the `tex` filetype, you need to include `latex` in this list as this is</span></span><br><span class="line"> <span class="comment">-- the name of the parser)</span></span><br><span class="line"> <span class="comment">-- list of language that will be disabled</span></span><br><span class="line"> <span class="comment">-- disable = { "c", "rust" },</span></span><br><span class="line"> <span class="comment">-- Or use a function for more flexibility, e.g. to disable slow treesitter highlight for large files</span></span><br><span class="line"> disable = <span class="function"><span class="keyword">function</span><span class="params">(lang, buf)</span></span></span><br><span class="line"> <span class="keyword">local</span> max_filesize = <span class="number">100</span> * <span class="number">1024</span> <span class="comment">-- 100 KB</span></span><br><span class="line"> <span class="keyword">local</span> ok, stats = <span class="built_in">pcall</span>(vim.loop.fs_stat, vim.api.nvim_buf_get_name(buf))</span><br><span class="line"> <span class="keyword">if</span> ok <span class="keyword">and</span> stats <span class="keyword">and</span> stats.size > max_filesize <span class="keyword">then</span></span><br><span class="line"> <span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line"> <span class="keyword">end</span></span><br><span class="line"> <span class="keyword">end</span>,</span><br><span class="line"></span><br><span class="line"> <span class="comment">-- Setting this to true will run `:h syntax` and tree-sitter at the same time.</span></span><br><span class="line"> <span class="comment">-- Set this to `true` if you depend on 'syntax' being enabled (like for indentation).</span></span><br><span class="line"> <span class="comment">-- Using this option may slow down your editor, and you may see some duplicate highlights.</span></span><br><span class="line"> <span class="comment">-- Instead of true it can also be a list of languages</span></span><br><span class="line"> additional_vim_regex_highlighting = <span class="literal">false</span>,</span><br><span class="line"> },</span><br><span class="line"> rainbow = {</span><br><span class="line"> enable = <span class="literal">true</span>,</span><br><span class="line"> <span class="comment">-- disable = { "jsx", "cpp" }, list of languages you want to disable the plugin for</span></span><br><span class="line"> extended_mode = <span class="literal">true</span>, <span class="comment">-- Also highlight non-bracket delimiters like html tags, boolean or table: lang -> boolean</span></span><br><span class="line"> max_file_lines = <span class="literal">nil</span>, <span class="comment">-- Do not enable for files with more than n lines, int</span></span><br><span class="line"> <span class="comment">-- colors = {}, -- table of hex strings</span></span><br><span class="line"> <span class="comment">-- termcolors = {} -- table of colour name strings</span></span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">-- vim.api.nvim_set_hl(0, "@punctuation.bracket", { link = "" })</span></span><br><span class="line">vim.cmd<span class="string">[[</span></span><br><span class="line"><span class="string">set foldmethod=expr</span></span><br><span class="line"><span class="string">set foldexpr=nvim_treesitter#foldexpr()</span></span><br><span class="line"><span class="string">set nofoldenable</span></span><br><span class="line"><span class="string">]]</span></span><br></pre></td></tr></table></figure>
<h2 id="其他"><a href="#其他" class="headerlink" title="其他"></a>其他</h2><p>使用了状态栏 <code>nvim-lualine/lualine.nvim</code> 和配色 <code>rebelot/kanagawa.nvim</code>,我使用了一个 <code>appearance.lua</code> 文件管理这两个插件的配置</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 配色</span></span><br><span class="line">vim.cmd(<span class="string">'colorscheme kanagawa'</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 状态栏</span></span><br><span class="line"><span class="built_in">require</span>(<span class="string">'lualine'</span>).setup()</span><br></pre></td></tr></table></figure>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="https://yukirasawa.github.io/2023/02/06/hello-world/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.jpg">
<meta itemprop="name" content="吉良泽优">
<meta itemprop="description" content="摸鱼的地方">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="吉良泽优的小站">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2023/02/06/hello-world/" class="post-title-link" itemprop="url">Hello World</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2023-02-06 16:18:42" itemprop="dateCreated datePublished" datetime="2023-02-06T16:18:42+08:00">2023-02-06</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar-check"></i>
</span>
<span class="post-meta-item-text">更新于</span>
<time title="修改时间:2023-02-09 09:17:58" itemprop="dateModified" datetime="2023-02-09T09:17:58+08:00">2023-02-09</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>Hello World</p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
</div>
<script>
window.addEventListener('tabs:register', () => {
let { activeClass } = CONFIG.comments;
if (CONFIG.comments.storage) {
activeClass = localStorage.getItem('comments_active') || activeClass;
}
if (activeClass) {
let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
if (activeTab) {
activeTab.click();
}
}
});
if (CONFIG.comments.storage) {
window.addEventListener('tabs:click', event => {
if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
let commentClass = event.target.classList[1];
localStorage.setItem('comments_active', commentClass);
});
}
</script>
</div>
<div class="toggle sidebar-toggle">
<span class="toggle-line toggle-line-first"></span>
<span class="toggle-line toggle-line-middle"></span>
<span class="toggle-line toggle-line-last"></span>
</div>
<aside class="sidebar">
<div class="sidebar-inner">
<ul class="sidebar-nav motion-element">
<li class="sidebar-nav-toc">
文章目录
</li>
<li class="sidebar-nav-overview">
站点概览
</li>
</ul>
<!--noindex-->
<div class="post-toc-wrap sidebar-panel">
</div>
<!--/noindex-->
<div class="site-overview-wrap sidebar-panel">
<div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
<img class="site-author-image" itemprop="image" alt="吉良泽优"
src="/images/avatar.jpg">
<p class="site-author-name" itemprop="name">吉良泽优</p>
<div class="site-description" itemprop="description">摸鱼的地方</div>
</div>
<div class="site-state-wrap motion-element">
<nav class="site-state">
<div class="site-state-item site-state-posts">
<a href="/archives/">
<span class="site-state-item-count">6</span>
<span class="site-state-item-name">日志</span>
</a>
</div>
</nav>
</div>
</div>
</div>
</aside>
<div id="sidebar-dimmer"></div>
</div>
</main>
<footer class="footer">
<div class="footer-inner">
<div class="copyright">
©
<span itemprop="copyrightYear">2023</span>
<span class="with-love">
<i class="fa fa-heart"></i>
</span>
<span class="author" itemprop="copyrightHolder">吉良泽优</span>
</div>
<div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
</div>
</div>
</footer>
</div>
<script src="/lib/anime.min.js"></script>
<script src="/lib/velocity/velocity.min.js"></script>
<script src="/lib/velocity/velocity.ui.min.js"></script>
<script src="/js/utils.js"></script>
<script src="/js/motion.js"></script>
<script src="/js/schemes/pisces.js"></script>
<script src="/js/next-boot.js"></script>
</body>
</html>