Vim:如何创建键映射到 :edit 并跳转到最后一行?
我将键 F2 映射到刷新(:edit
)当前打开的文件。当文件已在外部更新时(在日志文件末尾添加新行),我在观看日志文件以更新屏幕时使用此功能。
nnoremap <silent> <F2> :edit<CR>
我想在刷新后跳到文件末尾。
如何创建同时执行 :edit
和跳转到文件末尾(快捷键 G
)的键映射?
I mapped key F2 to refresh (:edit
) currently opened file. I'm using this when watching a log file to update the screen when file has been updated outside (new lines added at the end of a log file).
nnoremap <silent> <F2> :edit<CR>
I would like to jump to the end of the file after it has been refreshed.
How do I create key mapping which does :edit
and jump to end of the file (shortcut G
) at the same time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将光标定位在刚刚打开(或重新打开)的文件中的惯用方法
是使用
:edit
命令的+
-参数(请参阅:help +cmd
)。虽然通用语法允许执行任何命令,但有特殊的
通过与该行上的文本匹配的模式导航到该行的情况
(
+/
后跟模式),或行号(+
后跟数字)。如果在后一种形式中省略数字,则假定为
文件的最后一行。
这样,重新加载当前文件,将光标定位在最后一个文件上
行,可以使用命令
或
可以通过使用
:e
而不是:edit
来缩短这些命令,并且在
+
参数之前留下一个可选空格。或
相应的映射将采用以下形式
:
请注意,此
+
参数语法对于从命令行,所以
也可以。
An idiomatic way to position the cursor in the just opened (or reopened) file
is to use the
+
-argument of the:edit
command (see:help +cmd
).Although the general syntax allows to execute any command, there are special
cases for navigating to a certain line by a pattern matching text on that line
(
+/
followed by the pattern), or by a line number (+
followed by thenumber). If the number is omitted in the latter form, it is assumed to be the
last line of the file.
In such a way, to reload the current file positioning the cursor on the last
line, one can use the command
or
It is possible to shorten these commands by using
:e
instead of:edit
andleaving out an optional space before the
+
-argument.or
The corresponding mappings would take the form
and
Note that this
+
-argument syntax is also valid for opening a file from thecommand line, so
works as well.
这就是我要使用的:
This is what I'd use:
您可以使用
在地图中链接命令。此映射可以满足您的要求:在映射/脚本中使用
normal!
而不是normal
非常重要,因为前者不会考虑用户定义的映射。即使在这种情况下存在G
的映射,vim 也会将G
视为根本没有映射。You can chain commands in a map by using
<bar>
. This mapping does what you want:It's important to use
normal!
instead ofnormal
in mappings/scripts because the prior will not take user defined mappings into account. Even if there is a mapping forG
in this case, vim will treatG
as if it were not mapped at all.您可以使用
:normal
来使用正常-mode G 动作:也许更好的是使用
:$
命令转到末尾文件:You can use
:normal
to use the normal-mode G motion:Perhaps better would be to use the
:$
command to go to the end of the file:在 Vim 中,
'|'
可用于分隔命令,就像许多 Linux/Unix 版本一样。有关使用栏的更多信息,请查看:help bar
示例:
:edit |普通的! G
如果您希望在键映射中使用它您可能会发现您的
~/.vimrc
不喜欢使用|
或的映射\|
。为了使这项工作正常进行,请使用等效的
来代替。In Vim
'|'
can be used to separate commands, much like many flavors of Linux/Unix. For more information about the use of the bar check out:help bar
Example:
:edit | normal! G
If you wish to use this in a key mapping You may find that your
~/.vimrc
doesn't like maps utilizing|
, or\|
. In order to make this work use the equivalent<bar>
instead.