简体   繁体   English

"如何在 Vim 中移动到行尾?"

[英]How do I move to end of line in Vim?

我知道通常如何在命令模式下移动,特别是跳转到行等。但是跳转到我当前所在行末尾的命令是什么?

"

Just the $ (dollar sign) key.只是$ (美元符号)键。 You can use A to move to the end of the line and switch to editing mode (Append).您可以使用A移动到行尾并切换到编辑模式 (Append)。 To jump the last non-blank character, you can press g then _ keys.要跳转最后一个非空白字符,可以先按g然后按_键。

The opposite of A is I (Insert mode at beginning of line), as an aside.顺便说一下, A的反面是I (在行首插入模式)。 Pressing just the ^ will place your cursor at the first non-white-space character of the line.仅按^会将光标置于该行的第一个非空白字符处。

As lots of people have said:正如很多人所说:

  • $ gets you to the end of the line $ 带你到行尾

but also:但是也:

  • ^ or _ gets you to the first non-whitespace character in the line, and ^_将您带到该行中的第一个非空白字符,并且
  • 0 (zero) gets you to the beginning of the line incl. 0 (零)让你到行的开头,包括。 whitespace空白
  • $ moves to the last character on the line. $移动到该行的最后一个字符。
  • g _ goes to the last non-whitespace character. g _转到最后一个非空白字符。

  • g $ goes to the end of the screen line (when a buffer line is wrapped across multiple screen lines) g $转到屏幕行的末尾(当缓冲区行跨多个屏幕行时)

The main question - end of line主要问题 - 行尾

$ goes to the end of line, remains in command mode $到行尾,保持命令模式

A goes to the end of line, switches to insert mode A到行尾,切换到插入模式

Conversely - start of line (technically the first non-whitespace character)相反 - 行首(技术上是第一个非空白字符)

^ goes to the start of line, remains in command mode ^到行首,保持命令模式

I (uppercase i) goes to the start of line, switches to insert mode I (大写 i) 到行首,切换到插入模式

Further - start of line (technically the first column irrespective of whitespace )进一步 - 行首(从技术上讲,第一列与空格无关

0 (zero) goes to the start of line, remains in command mode 0 (零)到行首,保持命令模式

0i (zero followed by lowercase i) goes the start of line, switches to insert mode 0i (零后跟小写 i)进入行首,切换到插入模式

For those starting to learn vi, here is a good introduction to vi by listing side by side vi commands to typical Windows GUI Editor cursor movement and shortcut keys.对于那些开始学习 vi 的人,这里有一个很好的 vi 介绍,它并排列出了典型的 Windows GUI 编辑器光标移动和快捷键的 vi 命令。

vi editor for Windows users适用于 Windows 用户的 vi 编辑器

如果当前行环绕可见屏幕到下一行,则可以使用g$到达屏幕行的末尾。

I can't see hotkey for macbook for use vim in standard terminal.我在标准终端中看不到用于 vim 的 macbook 热键。 Hope it will help someone.希望它会帮助某人。 For macOS users (tested on macbook pro 2018):对于 macOS 用户(在 macbook pro 2018 上测试):

fn + - move to beginning line fn + - 移动到起始行

fn + - move to end line fn + - 移动到结束行

fn + - move page up fn + - 向上移动页面

fn + - move page down fn + - 向下移动页面

fn + g - move the cursor to the beginning of the document fn + g - 将光标移动到文档的开头

fn + shift + g - move the cursor to the end of the document fn + shift + g - 将光标移动到文档末尾

For the last two commands sometime needs to tap twice.对于最后两个命令,有时需要点击两次。

美元符号:$

A从行尾开始进入编辑模式。

The advantage of the 'End' key is it works in both normal and insert modes. 'End' 键的优点是它可以在正常和插入模式下工作。

'$' works in normal/command mode only but it also works in the classic vi editor (good to know when vim is not available). '$' 仅适用于普通/命令模式,但它也适用于经典的 vi 编辑器(很高兴知道 vim 不可用时)。

In many cases, when we are inside a string we are enclosed by a double quote, or while writing a statement we don't want to press escape and go to end of that line with arrow key and press the semicolon( ; ) just to end the line.在许多情况下,当我们在一个字符串中时,我们被双引号括起来,或者在编写语句时我们不想按转义键并使用箭头键转到该行的末尾并按分号( ; )只是为了结束行。 Write the following line inside your vimrc file:vimrc文件中写入以下行:

imap <C-l> <Esc>$a

What does the line say?这条线说什么? It maps Ctrl+l to a series of commands.它将 Ctrl+l 映射到一系列命令。 It is equivalent to you pressing Esc (command mode), $ (end of line), a (append) at once.这相当于你一次按下Esc (命令模式)、 $ (行尾)、 a (追加)。

Also note the distinction between line (or perhaps physical line) and screen line .还要注意line (或者可能是物理 line)和screen line之间的区别。 A line is terminated by the End Of Line character ("\\n").一行由行尾字符 ("\\n") 终止。 A screen line is whatever happens to be shown as one row of characters in your terminal or in your screen.屏幕行是在终端或屏幕中显示为一行字符的任何内容。 The two come apart if you have physical lines longer than the screen width, which is very common when writing emails and such.如果您的物理线长于屏幕宽度,这两者就会分开,这在编写电子邮件等时很常见。

The distinction shows up in the end-of-line commands as well.这种区别也体现在行尾命令中。

  • $ and 0 move to the end or beginning of the physical line or paragraph, respectively: $0 分别移动到物理行或段落的末尾或开头:
  • g $ and g 0 move to the end or beginning of the screen line or paragraph, respectively. g $g 0 分别移动到屏幕行或段落的末尾或开头。

If you always prefer the latter behavior, you can remap the keys like this:如果你总是喜欢后一种行为,你可以像这样重新映射键:

:noremap 0 g0
:noremap $ g$

或者有一个显而易见的答案:使用End键转到行尾。

可能不相关,但如果你想在当前行之后开始一个新行,你可以在行中的任何地方使用 o 。

The easiest option would be to key in $ .最简单的选择是键入$ If you are working with blocks of text, you might appreciate the command { and } in order to move a paragraph back and forward, respectively.如果您正在处理文本块,您可能会喜欢使用命令{}来分别前后移动段落。

I was used to Home/End getting me to the start and end of lines in Insert mode (from use in Windows and I think Linux), which Mac doesn't support.我习惯于 Home/End 在插入模式下让我进入行的开始和结束(从在 Windows 中使用,我认为是 Linux),Mac 不支持。 This is particularly annoying because when I'm using vim on a remote system, I also can't easily do it.这特别烦人,因为当我在远程系统上使用 vim 时,我也无法轻松做到。 After some painful trial and error, I came up with these .vimrc lines which do the same thing, but bound to Ctrl-A for the start of the line and Ctrl-D for the end of the line.经过一些痛苦的反复试验,我想出了这些.vimrc行,它们做同样的事情,但绑定到 Ctrl-A 作为行的开头和 Ctrl-D 作为行的结尾。 (For some reason, Ctrl-E I guess is reserved or at least I couldn't figure a way to bind it.) Enjoy. (出于某种原因,我想 Ctrl-E 是保留的,或者至少我无法找到绑定它的方法。)享受吧。

:imap <Char-1> <Char-15>:normal 0<Char-13>
:imap <Char-4> <Char-15>:normal $<Char-13>

There's a good chart here for the ASCII control character codes here for others as well:这里有一个很好的图表,用于其他人的 ASCII 控制字符代码:

http://www.physics.udel.edu/~watson/scen103/ascii.html http://www.physics.udel.edu/~watson/scen103/ascii.html

You can also do Ctrl-V + Ctrl- as well, but that doesn't paste as well to places like this.您也可以执行 Ctrl-V + Ctrl- ,但这并不能粘贴到这样的地方。

如何移动到包括空格在内的行尾?

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM