vim ctrlp
by _haochuan
通過_haochuan
使用Ctrlp和Ctag使Vim更智能 (Make Your Vim Smarter Using Ctrlp and Ctags)
I absolutely love Vim, and I use Vim for all my coding and writing from year to year. Although more are more people, especially for those are working with JavaScript, prefer modern code editors such as Sublime Text or VSCode, I’d rather spend a little time trying to make my toy more intelligent.
我絕對喜歡Vim,并且我每年都使用Vim進行所有編碼和寫作。 盡管有更多的人,特別是對于那些使用JavaScript的人,更喜歡諸如Sublime Text或VSCode之類的現代代碼編輯器,但我還是愿意花一點時間來使我的玩具更智能。
CtrlP (CtrlP)
If you are a Sublime Text, Atom, or VSCode guy, you must use ctrl + p
thousands of times to improve productivity. Well, don’t be jealous if you are a Vim guy because this fancy Vim plugin CtrlP will give you all you need.Check this official doc for installation and setup.
如果您是Sublime Text,Atom或VSCode方面的人,則必須使用ctrl + p
數千次才能提高生產力。 好吧,如果您是Vim家伙,請不要嫉妒,因為這個花哨的Vim插件CtrlP會為您提供所需的一切。請查看此官方文檔進行安裝和設置。
標簽 (Ctags)
Ctags is a tool that will sift through your code, indexing methods, classes, variables, and other identifiers, storing the index in a tags file. The tags file contains a single tag per line. Depending on command line arguments and the language ctags is run against, a lot of information can be obtained from this index.
Ctags是一種工具,可用于篩選代碼,索引方法,類,變量和其他標識符,并將索引存儲在標簽文件中。 標簽文件每行包含一個標簽。 根據命令行參數和使用的語言ctags,可以從該索引中獲取很多信息。
Ctags currently supports 41 programming languages, and it’s relatively easy to add definitions for more.
Ctags當前支持41種編程語言 ,并且添加更多的定義相對容易。
Ctags makes it much easier to navigate a larger project, particularly if the code you’re working with is unfamiliar. If you’re unsure of what a method does or how it’s supposed to be called, you can jump straight to its definition. If you’re in the downward spiral of a 500+ line Perl script and want to know where a variable was defined three hours ago, you can jump right back to it. And afterward, you can jump right back to where you were working.
Ctags使導航更大的項目變得容易得多,尤其是在您不熟悉所使用的代碼的情況下。 如果不確定方法的作用或方法的調用方式,可以直接轉到其定義。 如果您處于Perl腳本超過500行的螺旋式下降狀態,并且想知道三個小時前在哪里定義了變量,則可以直接跳回到該變量。 然后,您可以跳回到工作地點。
You can install Ctags using Homebrew in OSX:
您可以在OSX中使用Homebrew安裝Ctags:
brew install ctags
Please note that OS X comes with a Ctags executable, but it’s not exuberant-Ctags and is missing most of the useful features. If you see an error like Invalid Parameter
when you run ctags
, it means that the system is not using the one you installed with Homebrew. To solve this:
請注意,OS X帶有Ctags可執行文件,但它不是旺盛的Ctags,并且缺少大多數有用的功能。 如果在運行ctags
時看到諸如Invalid Parameter
類的錯誤,則表明系統未使用您通過Homebrew安裝的系統。 要解決這個問題:
$ alias ctags="`brew --prefix`/bin/ctags"
When you’re sitting in the directory you want to index, just run:
當您坐在要索引的目錄中時,只需運行:
ctags -R.
Ctags will walk through the directory recursively, tagging all source files it encounters. For very large projects, this might take a while, but normally it’s pretty fast.
Ctags將遞歸遍歷該目錄,標記它遇到的所有源文件。 對于非常大的項目,這可能需要一段時間,但通常速度很快。
You may also need some extra config for Ctags, below is the ~/.ctags
I'm using:
您可能還需要對Ctags進行一些額外的配置,以下是我正在使用的~/.ctags
:
--langmap=javascript:.js.es6.es.jsx--javascript-kinds=-c-f-m-p-v
--regex-javascript=/^[ \t]*(var|let|const)[ \t]+([A-Za-z0-9_$]+)[ \t]*=[ \t]*\[/\2/A,Array,Arrays/
--regex-javascript=/^[ \t]*(var|let|const)[ \t]+([A-Z][A-Za-z0-9_$]+)[ \t]*=[ \t]*function/\2/C,Class,Classes/--regex-javascript=/^[ \t]*class[ \t]+([A-Za-z0-9_$]+)/\1/C,Class,Classes/
--regex-javascript=/^[ \t]*export[ \t]?({[ \t]*)*([A-Za-z0-9_\*]*[ \t]as[ \t])([A-Za-z0-9_]+)/\3/E,Export,Exports/--regex-javascript=/^[ \t]*export[ \t]?({[ \t]*)*([A-Za-z0-9_\*]*[ \t]as[ \t])*([A-Za-z0-9_]+),[ \t]*([A-Za-z0-9_\*]*[ \t]as[ \t])([A-Za-z0-9_]+)/\5/E,export,Exports/--regex-javascript=/^[ \t]*export[ \t]?({[ \t]*)*([A-Za-z0-9_\*]*[ \t]as[ \t])*([A-Za-z0-9_]+),[ \t]*([A-Za-z0-9_\*]*[ \t]as[ \t])*([A-Za-z0-9_]+),[ \t]*([A-Za-z0-9_\*]*[ \t]as[ \t])([A-Za-z0-9_]+)/\7/E,Export,Exports/--regex-javascript=/^[ \t]*export[ \t]?(var|let|const)[ \t]+([A_Za-z0-9_$]+)/\2/E,Export,Exports/--regex-javascript=/^[ \t]*export[ \t]?(var|let|const)[ \t]+([A_Za-z0-9_$]+)[ \t]*[^,]+,[ \t]*([A_Za-z0-9_$]+)/\3/E,Export,Exports/--regex-javascript=/^[ \t]*export[ \t]?(var|let|const)[ \t]+([A_Za-z0-9_$]+)[ \t]*[^,]+,[ \t]*([A_Za-z0-9_$]+)[ \t]*[^,]+,[ \t]*([A_Za-z0-9_$]+)/\4/E,Export,Exports/
--regex-javascript=/^[ \t]*function[ \t]*([A-Za-z0-9_$]+)[ \t\(]/\1/F,Function,Functions/--regex-javascript=/^[ \t]*[\(]function[ \t]*([A-Za-z0-9_$]+)[ \t\(]/\1/F,Function,Functions/--regex-javascript=/^[ \t]*(var|let|const)[ \t]+([a-z][A-Za-z0-9_$]+)[ \t]*=[ \t]*function[^\*][^\*]/\2/F,Function,Functions/--regex-javascript=/^[ \t]*(var|let|const)[ \t]+([a-z][A-Za-z0-9_$]+)[ \t]*=[ \t]*\([^\*]/\2/F,Function,Functions/
--regex-javascript=/^[ \t]*function[ \t]*\*[ \t]*([A-Za-z0-9_$]+)/\1/G,Generator,Generators/--regex-javascript=/^[ \t]*(var|let|const)[ \t]+([a-z][A-Za-z0-9_$]+)[ \t]*=[ \t]*function([ \t]*\*)/\2/G,Generator,Genrators/--regex-javascript=/^[ \t]*(\*[ \t])([A-Za-z0-9_$]+)[ \t]*\(.*\)[ \t]*{/\2/G,Generator,Generators/
--regex-javascript=/^[ \t]*import[ \t]?({[ \t]*)*([A-Za-z0-9_\*]*[ \t]as[ \t])([A-Za-z0-9_]+)/\3/I,Import,Imports/--regex-javascript=/^[ \t]*import[ \t]?({[ \t]*)*([A-Za-z0-9_\*]*[ \t]as[ \t])*([A-Za-z0-9_]+),[ \t]*([A-Za-z0-9_\*]*[ \t]as[ \t])([A-Za-z0-9_]+)/\5/I,Import,Imports/--regex-javascript=/^[ \t]*import[ \t]?({[ \t]*)*([A-Za-z0-9_\*]*[ \t]as[ \t])*([A-Za-z0-9_]+),[ \t]*([A-Za-z0-9_\*]*[ \t]as[ \t])*([A-Za-z0-9_]+),[ \t]*([A-Za-z0-9_\*]*[ \t]as[ \t])([A-Za-z0-9_]+)/\7/I,Import,Imports/
--regex-javascript=/^[ \t]*this\.([A-Za-z0-9_$]+)[ \t]*=.*{$/\1/M,Method,Methods/--regex-javascript=/^[ \t]*([A-Za-z0-9_$]+)[ \t]*[:=][ \t]*[\(]*function[ \t]*\(/\1/M,Method,Methods/--regex-javascript=/^[ \t]*static[ \t]+([A-Za-z0-9_$]+)[ \t]*\(/\1/M,Method,Methods/--regex-javascript=/^[ \t]*([A-Za-z0-9_$]+)\(.*\)[ \t]*{/\1/M,Method,Methods/
--regex-javascript=/^[ \t]*(this\.)*([A-Za-z0-9_$]+)[ \t]*[:=].*[,;]*[^{]$/\2/P,Property,Properties/
--regex-javascript=/^[ \t]*(var|let|const)[ \t]+([A-Za-z0-9_$]+)[ \t]*=[ \t]*{/\2/O,Object,Objects/
--regex-javascript=/\/\/[ \t]*(FIXME|TODO|BUG|NOBUG|\?\?\?|\!\!\!|HACK|XXX)[ \t]*\:*(.*)/\1/T,Tag,Tags/
--regex-javascript=/^[ \t]*(var|let|const)[ \t]+([A-Za-z0-9_$]+)[ \t]*=[ \t]*[^\[{]*;$/\2/V,Variable,Variables/
--exclude=min--exclude=vendor--exclude=\*.min.\*--exclude=\*.map--exclude=\*.swp--exclude=\*.bak--exclude=tags--exclude=node_modules--exclude=bower_components--exclude=test--exclude=__test__--exclude=build--exclude=dist--exclude=*.bundle.*
Here is how it looks like going to function definition:
這是去函數定義的樣子:
Also you can use Ctrlp to search for tags instead of files. To do this, first you need to map a shortcut in your .vimrc
:
您也可以使用Ctrlp搜索標簽而不是文件。 為此,首先需要在.vimrc
映射一個快捷方式:
nnoremap <leader>. :CtrlPTag<cr>
Here is how it works:
下面是它的工作原理:
Hope it helps :)
希望能幫助到你 :)
I write code for audio and web, and play guitar on YouTube. If you want to see more stuff from me or know more about me, you can always find me in:
我編寫音頻和網絡代碼,并在YouTube上彈吉他。 如果您想從我這里了解更多信息或對我有更多了解,可以隨時在以下位置找到我:
Website:https://haochuan.io/
網址: https : //haochuan.io/
GitHub:https://github.com/haochuan
GitHub: https : //github.com/haochuan
Medium:https://medium.com/@haochuan
媒介: https : //medium.com/@haochuan
YouTube: https://www.youtube.com/channel/UCNESazgvF_NtDAOJrJMNw0g
YouTube: https : //www.youtube.com/channel/UCNESazgvF_NtDAOJrJMNw0g
翻譯自: https://www.freecodecamp.org/news/make-your-vim-smarter-using-ctrlp-and-ctags-846fc12178a4/
vim ctrlp