Chinaunix首页 | 论坛 | 博客
  • 博客访问: 47352
  • 博文数量: 5
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 135
  • 用 户 组: 普通用户
  • 注册时间: 2013-01-29 11:58
个人简介

等我想好再说

文章分类

全部博文(5)

文章存档

2015年(1)

2014年(1)

2013年(3)

我的朋友

分类: LINUX

2014-06-08 16:16:11

    相信很多程序员都遇见过这样的场景,有时我们需要在一个文件中插入一个文件名或者路径(比如在程序文件头插入当前文件名,
或者在makefile中写入交叉编译工具链的路径)。如果是文件名很短的情况下,我们当然可以直接输入。但有时我们需要完整的路径
名,尤其当路径很长时,直接输入显然不太现实,除了可能需要一边输入一边对照之外,还很容易输错。这时候更好的做法当然是,
复制-粘贴。但是这样一来,就不得不切换窗口而且还得动鼠标(对一个vimer来说,这太不酷了)。
    博主以前也多次遇见过这个问题,每次都自己说一声要是有个命令来搞这个就太好了,然后就埋头复制-粘贴了。直到前两天,又
遇见同样的问题,博主终于决定要寻找更好的解决办法了,于是怒而搜之。终于被我找到了还不错的办法。
    1:插入模式下(同时按下"Ctrl" 和 "r")光标位置出现引号后输入%,则在光标位置插入当前文件名,若文件在当前目录或
其子目录下,则插入带相对路径的文件名, 否则插入带绝对路径的文件名。
    2:在命令模式下输入 :r !pwd。 在光标位置插入当前绝对路径名。


    第二种方法依赖shell命令pwd,所以在windows下并不可用。那么在windows下要是想插入当前目录下文件的绝对路径名的话怎么办呢?
    3: 先 :cd xxx 到另一个目录,然后目标文件就已不在当前目录下了,于是可以使用 % 插入。
    
    那么如果我想在一个文件(a文件)中插入另一个文件(b文件)的文件名的话怎么办呢,


    4: 先 :vs[plit] xxx 分屏打开b文件(或路径),然后:q 退出,回到文件a,插入模式下, 接着 #,即可。这里虽然在打开b文
 件时仍然需要输入路径,但此时在命令行模式是有自动补全的。
    
    5: 我们也可以在命令模式下, :r !echo 文件名/路径名 直接将echo后的内容插入到当前光标位置,这时同样是有自动补齐的。


    以下是vim 自带的帮助中对于和:r的说明,有助于大家理解上面的命令都是怎么工作的。


 --------------------------------------附录-------------------------------
:
CTRL-R {0-9a-z"%#:-=.} *c_CTRL-R* *c_*
Insert the contents of a numbered or named register.  Between
typing CTRL-R and the second character '"' will be displayed
to indicate that you are expected to enter the name of a
register.
The text is inserted as if you typed it, but mappings and
abbreviations are not used.  Command-line completion through
'wildchar' is not triggered though.  And characters that end
the command line are inserted literally (, , ,
).  A or CTRL-W could still end the command line
though, and remaining characters will then be interpreted in
another mode, which might not be what you intended.
Special registers:
'"' the unnamed register, containing the text of
the last delete or yank
'%' the current file name
'#' the alternate file name
'*' the clipboard contents (X11: primary selection)
'+' the clipboard contents
'/' the last search pattern
':' the last command-line
'-' the last small (less than a line) delete
'.' the last inserted text
*c_CTRL-R_=*
'=' the expression register: you are prompted to
enter an expression (see |expression|)
(doesn't work at the expression prompt; some
things such as changing the buffer or current
window are not allowed to avoid side effects)
When the result is a |List| the items are used
as lines.  They can have line breaks inside
too.
When the result is a Float it's automatically
converted to a String.

:r 
:r[ead] [++opt] [name]
Insert the file [name] (default: current file) below
the cursor.
See |++opt| for the possible values of [++opt].


:{range}r[ead] [++opt] [name]
Insert the file [name] (default: current file) below
the specified line.
See |++opt| for the possible values of [++opt].


*:r!* *:read!*
:[range]r[ead] !{cmd} Execute {cmd} and insert its standard output below
the cursor or the specified line.  A temporary file is
used to store the output of the command which is then
read into the buffer.  'shellredir' is used to save
the output of the command, which can be set to include
stderr or not.  {cmd} is executed like with ":!{cmd}",
any '!' is replaced with the previous command |:!|.


These commands insert the contents of a file, or the output of a command,
into the buffer.  They can be undone.  They cannot be repeated with the "."
command.  They work on a line basis, insertion starts below the line in which
the cursor is, or below the specified line.  To insert text above the first
line use the command ":0r {name}".

----------------------------------------------------------------------
补充,对于附录的说明:
    从上面的帮助里我们可以看到:
    实际上 % 和 # 两个命令是把寄存器 %和寄存器 # 中的内容插入到了当前光标处。
%寄存器中存的是正好是当前文件名,#寄存器中存的是上一个文件的文件名。
    :r !pwd 和 :r !echo xxxx 都是执行了系统命令并将并将标准输出插入了到当前光标处。



最后感叹一句,VIM真是博大精深。








阅读(4477) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~