我听说过很多关于Vim的事,包括优点和缺点。 (作为一名开发人员)使用Vim确实应该比使用其他任何编辑器都要快。 我用Vim来做一些基本的事情,我用Vim最多只能减少10倍的效率。

当你谈论速度时,只有两件事你应该关心(你可能不够关心,但你应该关心):

左右交替使用的 用手是最快的方法 键盘。 永远不要碰鼠标 第二种方法是尽可能快。 你要花很长时间才能移动你的手, 抓住鼠标,移动它,把它带来 回到键盘上(你经常这样做 看看键盘,确保你 将你的手正确地回到正确的位置)

下面的两个例子说明了为什么我使用Vim效率低得多。

复制/剪切和粘贴。我一直都这么做。在所有当代编辑器中,你用左手按Shift键,用右手移动光标来选择文本。然后按Ctrl+C复制,移动光标,按Ctrl+V粘贴。

Vim的情况很糟糕:

Yy复制一行(你几乎不需要整行!) [number xx]yy复制xx行到缓冲区。但你永远不知道你是否选择了你想要的。我经常要做[number xx]dd然后u来撤销!

另一个例子吗?搜索和替换。

在PSPad中:按Ctrl+f,然后输入你想要搜索的内容,然后按Enter。 在Vim: /中,然后输入你想要搜索的内容,然后如果有一些特殊字符,在每个特殊字符前放\,然后按Enter。

Vim的一切都是这样的:似乎我不知道如何正确处理它。

注:我已经读了Vim小抄:)

我的问题是:

与当代编辑器相比,您使用Vim的哪些方式使您的工作效率更高?


当前回答

Ctrl-w Ctrl-f ............ open file under cursor in new window
Ctrl-6 ................... alternate file
'0 ....................... open last file
:x ....................... close if save

其他回答

多个缓冲区,特别是在它们之间快速跳转,以比较:bp和:bn的两个文件(正确地重新映射到a Shift + p或Shift + n)

Vimdiff模式(在两个垂直缓冲区中分割,用颜色显示差异)

区域复制Ctrl + v

最后,标签完成标识符(搜索“mosh_tab_or_complete”)。这改变了我的生活。

一些提高效率的技巧:

智能运动

*和#在向前/向后光标下搜索单词。 W到下一个单词 W到下一个空格分隔的单词 B /e到当前单词的开头/结尾。(B / E只代表空格) gg / G跳转到文件的开始/结束。 %跳转到匹配{..}或(..)等. . {/}跳转到下一段。 ”。跳转回上次编辑的行。 g;跳回上次编辑的位置。

快速编辑命令

我在开头插入。 end的附加语句。 o / o打开一个新的行后/前电流。 v / v / Ctrl+ v可视模式(选择文本!) Shift+R替换文本 C更改剩余部分行。

结合命令

大多数命令接受数量和方向,例如:

改变直到单词结束 改变3个单词 BcW =开始全字,更改全字 ciW =改变内心的词。 Ci " =改变内部之间的".." Ci(=更改文本之间(..) Ci < = change text between < ..>(需要在vimrc中设置匹配对+=<:>) 4dd =删除4行 3x =删除3个字符。 3s =替换3个字符。

有用的程序员命令

R替换一个字符(例如rd用d替换当前字符)。 ~改变情况。 J连接两条线 Ctrl+A / Ctrl+X增加/减少一个数字。 . 重复最后一个命令(一个简单的宏) ==修复行缩进 >缩进块(可视模式下) < unindent block(在可视模式下)

宏录制

按q[键]开始录音。 然后按q键停止录制。 宏可以使用@[key]播放。

通过使用非常特定的命令和动作,VIM可以为下一行重新播放这些确切的动作。(例如A表示尾尾,b / e表示将光标移动到单词的开头或结尾)

良好设置的示例

# reset to vim-defaults
if &compatible          # only if not set before:
  set nocompatible      # use vim-defaults instead of vi-defaults (easier, more user friendly)
endif

# display settings
set background=dark     # enable for dark terminals
set nowrap              # dont wrap lines
set scrolloff=2         # 2 lines above/below cursor when scrolling
set number              # show line numbers
set showmatch           # show matching bracket (briefly jump)
set showmode            # show mode in status bar (insert/replace/...)
set showcmd             # show typed command in status bar
set ruler               # show cursor position in status bar
set title               # show file in titlebar
set wildmenu            # completion with menu
set wildignore=*.o,*.obj,*.bak,*.exe,*.py[co],*.swp,*~,*.pyc,.svn
set laststatus=2        # use 2 lines for the status bar
set matchtime=2         # show matching bracket for 0.2 seconds
set matchpairs+=<:>     # specially for html

# editor settings
set esckeys             # map missed escape sequences (enables keypad keys)
set ignorecase          # case insensitive searching
set smartcase           # but become case sensitive if you type uppercase characters
set smartindent         # smart auto indenting
set smarttab            # smart tab handling for indenting
set magic               # change the way backslashes are used in search patterns
set bs=indent,eol,start # Allow backspacing over everything in insert mode

set tabstop=4           # number of spaces a tab counts for
set shiftwidth=4        # spaces for autoindents
#set expandtab           # turn a tabs into spaces

set fileformat=unix     # file mode is unix
#set fileformats=unix,dos    # only detect unix file format, displays that ^M with dos files

# system settings
set lazyredraw          # no redraws in macros
set confirm             # get a dialog when :q, :w, or :wq fails
set nobackup            # no backup~ files.
set viminfo='20,\"500   # remember copy registers after quitting in the .viminfo file -- 20 jump links, regs up to 500 lines'
set hidden              # remember undo after quitting
set history=50          # keep 50 lines of command history
set mouse=v             # use mouse in visual mode (not normal,insert,command,help mode


# color settings (if terminal/gui supports it)
if &t_Co > 2 || has("gui_running")
  syntax on          # enable colors
  set hlsearch       # highlight search (very useful!)
  set incsearch      # search incremently (search while typing)
endif

# paste mode toggle (needed when using autoindent/smartindent)
map <F10> :set paste<CR>
map <F11> :set nopaste<CR>
imap <F10> <C-O>:set paste<CR>
imap <F11> <nop>
set pastetoggle=<F11>

# Use of the filetype plugins, auto completion and indentation support
filetype plugin indent on

# file type specific settings
if has("autocmd")
  # For debugging
  #set verbose=9

  # if bash is sh.
  let bash_is_sh=1

  # change to directory of current file automatically
  autocmd BufEnter * lcd %:p:h

  # Put these in an autocmd group, so that we can delete them easily.
  augroup mysettings
    au FileType xslt,xml,css,html,xhtml,javascript,sh,config,c,cpp,docbook set smartindent shiftwidth=2 softtabstop=2 expandtab
    au FileType tex set wrap shiftwidth=2 softtabstop=2 expandtab

    # Confirm to PEP8
    au FileType python set tabstop=4 softtabstop=4 expandtab shiftwidth=4 cinwords=if,elif,else,for,while,try,except,finally,def,class
  augroup END

  augroup perl
    # reset (disable previous 'augroup perl' settings)
    au!  

    au BufReadPre,BufNewFile
    \ *.pl,*.pm
    \ set formatoptions=croq smartindent shiftwidth=2 softtabstop=2 cindent cinkeys='0{,0},!^F,o,O,e' " tags=./tags,tags,~/devel/tags,~/devel/C
    # formatoption:
    #   t - wrap text using textwidth
    #   c - wrap comments using textwidth (and auto insert comment leader)
    #   r - auto insert comment leader when pressing <return> in insert mode
    #   o - auto insert comment leader when pressing 'o' or 'O'.
    #   q - allow formatting of comments with "gq"
    #   a - auto formatting for paragraphs
    #   n - auto wrap numbered lists
    #   
  augroup END


  # Always jump to the last known cursor position. 
  # Don't do it when the position is invalid or when inside
  # an event handler (happens when dropping a file on gvim). 
  autocmd BufReadPost * 
    \ if line("'\"") > 0 && line("'\"") <= line("$") | 
    \   exe "normal g`\"" | 
    \ endif 

endif # has("autocmd")

设置可以存储在~/中。Vimrc,或在/etc/vimrc.系统范围Local,然后从/etc/vimrc文件中读取:

source /etc/vimrc.local

(为了使它在VIM中工作,您必须将# comment字符替换为“”,我想在这里给出正确的语法高亮显示)。

我在这里列出的命令都是非常基本的,也是到目前为止我使用的主要命令。它们已经让我变得更有效率,而不需要知道所有花哨的东西。

gi

转到上次编辑的位置(如果你进行了一些搜索,然后想要返回编辑,这非常有用)

^P和^N

完成上一个(^P)或下一个(^N)文本。

^O和^I

转到前一个(^O -“O”为旧)位置或下一个(^I -“I”靠近“O”)位置。 当你执行搜索、编辑文件等时,你可以通过这些“跳转”向前和向后导航。

我是美国密码协会的成员。这本双月刊杂志包含了超过100种不同类型的密码。其中大约有15个是“密码学”——用字母代替数字的各种类型的算术问题。其中两三个是数独游戏,只不过是字母而不是数字。当网格完成后,九个不同的字母将在网格中的某个地方以某条线、对角线、螺旋线等方式拼出一个或多个单词。

我不用铅笔,也不用手写,而是从他们网站的会员区下载问题。

在处理这些数独游戏时,我使用vi,只是因为我使用了vi所具有的其他编辑器所没有的功能。主要是在把字母网格转换成有编号的网格,因为我觉得这样更容易解决,然后把完成的有编号的网格转换回字母网格中,找到解单词或单词。

这个问题被格式化为9组,每组9个字母,-s代表空格,写在两行中。第一步是将它们格式化成每行9个字符的9行。这没有什么特别的,只是在适当的位置插入8个换行符。

结果如下所示:

T-O-----C
-E-----S-
--AT--N-L
---NASO--
---E-T---
--SPCL---
E-T--OS--
-A-----P-
S-----C-T

So, first step in converting this into numbers is to make a list of the distinct letters. First, I make a copy of the block. I position the cursor at the top of the block, then type :y}}p. : puts me in command mode, y yanks the next movement command. Since } is a move to the end of the next paragraph, y} yanks the paragraph. } then moves the cursor to the end of the paragraph, and p pastes what we had yanked just after the cursor. So y}}p creates a copy of the next paragraph, and ends up with the cursor between the two copies.

接下来,我要把其中一个副本变成一个不同字母的列表。这个命令有点复杂:

:!}tr -cd A-Z | sed 's/\(.\)/\1\n/g' | sort -u | tr -d '\n'

: again puts me in command mode. ! indicates that the content of the next yank should be piped through a command line. } yanks the next paragraph, and the command line then uses the tr command to strip out everything except for upper-case letters, the sed command to print each letter on a single line, and the sort command to sort those lines, removing duplicates, and then tr strips out the newlines, leaving the nine distinct letters in a single line, replacing the nine lines that had made up the paragraph originally. In this case, the letters are: ACELNOPST.

下一步是制作网格的另一个副本。然后用我刚刚确定的字母把这些字母替换成1到9之间的数字。很简单::!}tr ACELNOPST 0-9。结果是:

8-5-----1
-2-----7-
--08--4-3
---4075--
---2-8---
--7613---
2-8--57--
-0-----6-
7-----1-8

这可以用通常的方式解决,或者输入任何你喜欢的数独求解器。然后,完成的解决方案可以转换回字母:!}tr 1-9 ACELNOPST。

vi的强大是很少有其他工具能比拟的。最大的问题是,只有极少数的vi教程书籍、网站、帮助文件等,几乎触及了可能的表面。

这些都不是捷径,但它们是相关的:

将capslock设置为额外的ESC(或Ctrl) 用下面的命令将领导者映射到“,”(逗号):let mapleader=“,”

他们提高了我的工作效率。