Linux VIM:如何在 Ubuntu 上精确定位

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6380635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:35:00  来源:igfitidea点击:

VIM: how to go to exact line on Ubuntu

linuxbashvimubuntuterminal

提问by kevin

I'm using vion Ubuntu 12.10. Some files are quite long so when I want to go to the middle of the file, I have to page down or scroll down.

vi在 Ubuntu 12.10 上使用。有些文件很长,所以当我想转到文件中间时,我必须向下翻页或向下滚动。

Is there a VIM shortcut to go to an exact line number?

是否有 VIM 快捷方式可以转到确切的行号?

采纳答案by matchew

:150

will take you to line 150 in vi

将带您到 vi 中的第 150 行

:1500

will take you to line 1500 in vi

将带您到 vi 中的第 1500 行

As per the comments you may want to try

根据您可能想尝试的评论

150G

150G

to get to line 150. which is less key strokes then :150Enterif you aren't sure what line you are on try

到第 150 行。:150Enter如果你不确定你在哪一行,那么击键次数更少

 :set nu!

notice the :

注意:

if you want to always see the line consider editing your vim profile. Most often

如果您想始终看到该行,请考虑编辑您的 vim 配置文件。最经常

vi ~/.vimrc

and add

并添加

:set nu! 

and write and quit

写然后退出

:wq
#or you could use :x

this can be done outside of vi. For example, if I want to delete line 5000 in a text file I could use a scripting language. For example, using sed it would be the following

这可以在 vi 之外完成。例如,如果我想删除文本文件中的第 5000 行,我可以使用脚本语言。例如,使用 sed 将是以下内容

sed -i '5000d;' inputFile.txt

to delete line 10 to 20 it would be

删除第 10 到 20 行,它将是

sed -i '10,20d;' inputFile.txt

notice the -i will edit the file in place. Without the -i it will goto stdout. Try it. you can redirect stdout to a file

注意 -i 将就地编辑文件。没有 -i 它将转到标准输出。尝试一下。您可以将标准输出重定向到文件

sed '5001,$d;' inputFile.txt >> appenedFile.txt

this might have a lot going on here for you. this deletes line 5001 to $. With $ being the end of the file. >> will append to a file. where as > creates a new file.

这对你来说可能有很多事情要做。这会将第 5001 行删除为 $。$ 是文件的结尾。>> 将附加到文件。其中 as > 创建一个新文件。

if you are curious how many lines are in a file you may want to type wc -l inputFile.txt

如果您想知道一个文件中有多少行,您可能想输入 wc -l inputFile.txt

some of this may seem awfully trivial, but if you are trying to edit a file with 50,000 lines it may take vi a sweet minute to open and traverse. where if you know you just want to delete the last line you could use sed and do it in a fraction of the time.

其中一些可能看起来非常微不足道,但是如果您尝试编辑一个包含 50,000 行的文件,则可能需要花费一分钟才能打开和遍历。如果您知道只想删除最后一行,则可以使用 sed 并在很短的时间内完成。

sed can also search and replace inside a file as well. But perhaps awk, perl, or python might also be a viable solution.

sed 也可以在文件内搜索和替换。但也许 awk、perl 或 python 也可能是一个可行的解决方案。

but overall, you may wan to find a good tutorial on vi. thousands exist. I'd consult google. Perhaps find yourself a VIM Cheatsheat.

但总的来说,你可能想找到一个关于 vi 的好教程。数以千计的存在。我会咨询谷歌。也许发现自己是一个 VIM 秘籍。

回答by Jasonw

take a few minutes and start reading thisdocument. It reward you in the long run for efficiency in editing especially config file.

花几分钟时间开始阅读文档。从长远来看,它会奖励您编辑特别是配置文件的效率。

回答by rvs

Another advice: since you are new to vim, running vimtutorcan be very helpful.

另一个建议:由于您是 vim 的新手,因此运行vimtutor会非常有帮助。

回答by glenn Hymanman

Other vim tips: in command mode

其他 vim 提示:在命令模式下

  • Hgoes to the top of the screen
  • Mgoes to the middle of the screen
  • Lgoes to the bottom of the screen
  • gggoes to the first line
  • Ggoes to the last line
  • H转到屏幕顶部
  • M转到屏幕中间
  • L转到屏幕底部
  • gg转到第一行
  • G转到最后一行

回答by F. Hauri

From an opened terminal, in a bash shell, simply edit your file by running:

在打开的终端中,在 bash shell 中,只需通过运行以下命令来编辑您的文件:

$ vi +N yourfile

Where Nis the line number.

N行号在哪里。

For viewing (moreor less;):

用于查看(moreless;):

$ less +N yourfile
$ more +N yourfile

The sign +mean command to run at start. So if commandis only a number, then vi, lessand more, will jumps to this as line number.

符号+表示在 start 时运行的命令。因此,如果command只是一个数字,则vi, lessandmore将作为line number跳转到此。

But you may also use /regexfor finding the first occurence of a specific string or regex:

但您也可以/regex用于查找特定字符串或正则表达式的第一次出现:

$ less +/Error logfile
$ less -i +/error logfile      # -i Causes less's searches to ignore case
$ vi +/open.*myfile myprog...