mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-16 22:42:21 +08:00
Merge pull request #7080 from stevenzdg988/master
Translated by stevenzdg988 on 20171231 Why You Should Still Love Telnet.md
This commit is contained in:
commit
36abb27eb9
@ -1,123 +0,0 @@
|
||||
Making Vim Even More Awesome With These Cool Features
|
||||
======
|
||||
|
||||
![](http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/making-vim-even-more-awesome-with-these-cool-features_orig.jpg)
|
||||
|
||||
**Vim** is quite an integral part of Every [Linux Distribution][1] and the most useful tool (of course after the terminal) for Linux Users. At least, this theory holds for me. People might argue that for programming, Vim might not be a good choice as there are different IDEs or other sophisticated text editors like Sublime Text 3, Atom etc. which make the programming job pretty easier.
|
||||
|
||||
#### My Thoughts
|
||||
|
||||
But what I think is that Vim works the way we want it to right from the very start, while other editors make us work the way they have been designed, not the way we actually want them to work. I can’t say much about other editors cause I haven’t used them much ( I’m biased with Vim ).
|
||||
|
||||
Anyway, Let’s make something out of Vim, that really does the Job god damn well.
|
||||
|
||||
### Vim for Programming
|
||||
|
||||
#### Executing the Code
|
||||
|
||||
Consider a scenario, What we do when we are working on a C++ code on Vim and we need to compile and run it.
|
||||
|
||||
(a). We get back to the terminal either through (Ctrl + Z) thing or we just save and quit it (:wq).
|
||||
|
||||
(b). And the trouble’s ain’t over, we now need to type on something on terminal like this { g++ fileName.cxx }.
|
||||
|
||||
|
||||
|
||||
(c). And after that execute it by typing { ./a.out } .
|
||||
|
||||
Certainly a lot of things needed to be done in order to get our C++ code running over the shell. But it doesn’t seem to be a Vim way of doing this (as Vim always tends to keep almost everything under one/two keypresses). So, What is the Vim way of doing this stuff?
|
||||
|
||||
#### The Vim Way
|
||||
|
||||
Vim isn’t just a Text Editor, It is sort of a Programming Language for Editing Text. And that programming language that helps us extending the features of Vim is “VimScript”.
|
||||
|
||||
So, with the help of VimScript, we can easily automate our task of Compiling and Running code with just a KeyPress.
|
||||
|
||||
[![create functions in vim .vimrc](http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/vim_orig.png)][2]
|
||||
|
||||
Above is a snippet out of my .vimrc configuration file where i created a function called CPP().
|
||||
|
||||
#### Creating Functions in VimScript
|
||||
|
||||
The syntax for creating a function in VimScript is pretty easy. It starts with keyword “
|
||||
|
||||
**func**
|
||||
|
||||
” and is followed by the name of Function [Function Name must start with a capital letter in VimScript, otherwise Vim will give an error]. And the end of the function is denoted by keyword “
|
||||
|
||||
**endfunc**
|
||||
|
||||
”.
|
||||
|
||||
In the function’s body, you can see an
|
||||
|
||||
**exec**
|
||||
|
||||
statement, whatever you write after the exec keyword is executed over the Vim’s Command Mode (remember the thing starting with: at the bottom of Vim’s window). Now, the string that I passed to the exec is -
|
||||
|
||||
[![vim functions commands & symbols](http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/vim_1_orig.png)][3]
|
||||
|
||||
What happens is when this function is called, it first clears the Terminal Screen, so that only you will be viewing is your output, then it executes g++ over the filename you are working on and after that executes the a.out file formed due to compilation.
|
||||
|
||||
Mapping Ctrl+r to run C++ code
|
||||
|
||||
-------------------------------------------------------------
|
||||
|
||||
I mapped the statement :call CPP() to the key-combination (Ctrl+r) so that I could now press Ctrl+r to execute my C++ Code without manually typing :call CPP() and then pressing Enter.
|
||||
|
||||
#### End Result
|
||||
|
||||
We finally managed to find the Vim Way of doing that stuff. So now, You just hit a button and the output of your C++ code is on your screen, you don’t need to type all that lengthy thing. It sort of saves your time too.
|
||||
|
||||
We can achieve this sort of functionality for other languages too.
|
||||
|
||||
[![create function in vim for python](http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/vim_2_orig.png)][4]
|
||||
|
||||
So For Python: Now you could press to interpret your code.
|
||||
|
||||
[![create function in vim for java](http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/vim_3_orig.png)][5]
|
||||
|
||||
For Java: You could now press , it will first Compile your Java Code then interpret your java class file and show you the output.
|
||||
|
||||
### Picture ain’t over, Marching a level deep
|
||||
|
||||
So, this was all about how you could manipulate things to work your way in Vim. Now, it comes to how we implement all this in Vim. We can use these Code Snippets directly in Vim and the other way around is by using the AutoCommands in Vim (autocmd’s). The beauty of autocmd is these commands need not be called by users, they execute by themselves at any certain condition which is provided by the user.
|
||||
|
||||
What I want to do with this [autocmd] thing is that, instead of using different mappings to perform execution of codes in different Programming Languages, I would like a single mapping for execution for every language.
|
||||
|
||||
[![autocmd in vimrc](http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/vim_4_orig.png)][6]
|
||||
|
||||
What we did here is that I wrote autocommands for all the File Types for which I had functions for Executing the code.
|
||||
|
||||
What’s going to happen is as soon as I open any buffer of any of the above-mentioned file types, Vim will automatically map my (Ctrl + r) to the function call and represents Enter Key, so that I don’t need to press “Enter key” everytime I press and alone does the job.
|
||||
|
||||
To achieve this Functionality, you just need to add the function snippets to your [dot]vimrc and after that just put all those autocmds . And with that, the next time you open Vim, Vim will have all the Functionalities to execute all the Codes with the very same KeyBindings.
|
||||
|
||||
### Conclusion
|
||||
|
||||
That’s all for now. Hope this thing makes you love your Vim even more. I am currently exploring things in Vim, reading Documentations etc. and doing additions in [.vimrc] file and I will reach to you again when I will have something wonderful to share with you all.
|
||||
|
||||
If you want to have a look at my current [.vimrc] file, here is the link to my Github account: [MyVimrc][7]
|
||||
|
||||
.
|
||||
|
||||
Please do Comment on how you liked the article.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://www.linuxandubuntu.com/home/making-vim-even-more-awesome-with-these-cool-features
|
||||
|
||||
作者:[LINUXANDUBUNTU][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://www.linuxandubuntu.com
|
||||
[1]:http://www.linuxandubuntu.com/home/category/distros
|
||||
[2]:http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/vim_orig.png
|
||||
[3]:http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/vim_1_orig.png
|
||||
[4]:http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/vim_2_orig.png
|
||||
[5]:http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/vim_3_orig.png
|
||||
[6]:http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/vim_4_orig.png
|
||||
[7]:https://github.com/phenomenal-ab/VIm-Configurations/blob/master/.vimrc
|
@ -0,0 +1,109 @@
|
||||
用一些超酷的功能使 Vim 变得更强大
|
||||
======
|
||||
|
||||
![](http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/making-vim-even-more-awesome-with-these-cool-features_orig.jpg)
|
||||
|
||||
**Vim** 是每个 [Linux 发行版][1] 中不可或缺的一部分,也是 Linux 用户最常用的工具(当然是基于终端的)。至少,这个说法对我来说是成立的。人们可能会在利用什么工具进行程序设计更好产生争议,的确 Vim 可能不是一个好的选择,因为有很多不同的 IDE 或其他高性能的类似于 `Sublime Text 3`,`Atom` 等使程序设计变得更加容易的文本编辑器。
|
||||
#### 我的感想
|
||||
|
||||
但我认为,Vim 应该从一开始就以我们想要的方式运作,而其他编辑器让我们按照已经设计好的方式工作,实际上不是我们想要的工作方式。我不能过多地谈论其他编辑器,因为我没有过多地使用它们(我对 Vim 有偏见` Linux 中国注:我对 Vim 情有独钟`)。
|
||||
|
||||
不管怎样,让我们用 Vim 来做一些事情吧,它完全可以胜任。
|
||||
### 利用 Vim 进行程序设计
|
||||
|
||||
#### 执行代码
|
||||
|
||||
|
||||
考虑一个场景,当我们使用 Vim 设计 C++ 代码并需要编译和运行它时,该怎么做呢。
|
||||
|
||||
(a). 我们通过 `(Ctrl + Z)` 返回到终端,或者利用 `(:wq)` 保存并退出。
|
||||
|
||||
(b). 但是任务还没有结束,接下来需要在终端上输入类似于 `g++ fileName.cxx` 的命令进行编译。
|
||||
|
||||
(c). 接下来需要键入 `./a.out` 执行它。
|
||||
|
||||
|
||||
为了让我们的 C++ 代码在 shell 中运行,需要做很多事情。但这似乎并不是利用 Vim 操作的方法( Vim 总是倾向于把几乎所有操作方法利用一个/两个按键实现)。那么,做这些事情的 Vim 的方式究竟是什么?
|
||||
#### Vim 方式
|
||||
|
||||
|
||||
Vim 不仅仅是一个文本编辑器,它是一种编辑文本的编程语言。这种帮助我们扩展 Vim 功能的编程语言是 `“VimScript”(Linux 中国注: Vim 脚本)`。
|
||||
|
||||
因此,在 `VimScript` 的帮助下,我们可以只需一个按键轻松地将编译和运行代码的任务自动化。
|
||||
[![create functions in vim .vimrc](http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/vim_orig.png)][2]
|
||||
|
||||
|
||||
以上是在我的 `.vimrc` 配置文件里创建的一个名为 CPP() 函数的片段。
|
||||
#### 利用 VimScript 创建函数
|
||||
|
||||
|
||||
在VimScript中创建函数的语法非常简单。它以关键字“
|
||||
**func**
|
||||
”开头,然后是函数名[在 VimScript 中函数名必须以大写字母开头,否则 Vim 将提示错误]。在函数的结尾用关键词
|
||||
“**endfunc**
|
||||
”。
|
||||
在函数的主体中,可以看到
|
||||
**exec**
|
||||
声明,无论您在 **exec** 关键字之后写什么,都要在 Vim 的命令模式上执行(记住,在 Vim 窗口的底部以 `:` 开始)。现在,传递给 **exec** 的字符串是(Linux 中国注: ``:!clear && g++ % && ./a.out``) -
|
||||
[![vim functions commands & symbols](http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/vim_1_orig.png)][3]
|
||||
|
||||
|
||||
当这个函数被调用时,它首先清除终端屏幕,因此只能看到输出,接着利用 `g++` 执行正在处理的文件,然后运行由前一步编译而形成的 `a.out` 文件。
|
||||
|
||||
将 `Ctrl+r` 映射为运行 C++ 代码。
|
||||
-------------------------------------------------------------
|
||||
|
||||
|
||||
映射语句: `call CPP()` 到键组合 `Ctrl+r`,以便我现在可以按 `Ctrl+r` 来执行我的 C++ 代码,无需手动输入: `call CPP()`,然后按 `Enter` 键。
|
||||
#### 最终结果
|
||||
|
||||
|
||||
我们终于找到了 Vim Way 操作的方法。现在,你只需点击一个按钮,你编写的 C++ 代码就输出在你的屏幕上,你不需要键入所有冗长的命令了。这也节省了你的时间。
|
||||
|
||||
我们也可以为其他语言实现这类功能。
|
||||
[![create function in vim for python](http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/vim_2_orig.png)][4]
|
||||
|
||||
|
||||
对于Python:您可以按下映射键执行您的代码。
|
||||
[![create function in vim for java](http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/vim_3_orig.png)][5]
|
||||
|
||||
|
||||
对于Java:您现在可以按下映射健,它将首先编译您的 Java 代码,然后执行您的 Java 类文件并显示输出。
|
||||
### 进一步提高
|
||||
|
||||
|
||||
所以,这就是如何在 Vim 中操作的方法。现在,我们来看看如何在 Vim 中实现所有这些。我们可以直接在 Vim 中使用这些代码片段,而另一种方法是使用 Vim 中的自动命令 `autocmd`。`autocmd` 的优点是这些命令无需用户调用,它们在用户所提供的任何特定条件下自动执行。
|
||||
|
||||
我想用 [autocmd] 实现类似于单一的映射来执行每种语言替代使用不同的映射执行不同程序设计语言编译出的代码,。
|
||||
[![autocmd in vimrc](http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/vim_4_orig.png)][6]
|
||||
|
||||
|
||||
在这里做的是,为所有的定义了执行相应文件类型代码的函数编写了自动命令。
|
||||
|
||||
会发生什么当我打开任何上述提到的文件类型的缓冲区, Vim 会自动将 `Ctrl + r` 映射到函数调用和表示 Enter Key (Linux 中国注:回车键),这样就不需要每完成一个独立的任务就按一次 “Enter key” 了。
|
||||
|
||||
为了实现这个功能,您只需将函数片段添加到[dot]vimrc(Linux 中国注: `.vimrc` 文件)文件中,然后将所有这些 `autocmds` 也一并添加进去。这样,当您下一次打开 Vim 时,Vim 将拥有所有相应的功能来执行所有具有相同绑定键的代码。
|
||||
### 总结
|
||||
|
||||
就这些了。希望这些能让你更爱 Vim 。我目前正在探究 Vim 中的一些内容,正阅读文档,补充 [.vimrc] 文件,当我研究出一些成果后我会再次与你分享。
|
||||
如果你想看一下我现在的 [.vimrc] 文件,这是我的 Github 账户的链接: [MyVimrc][7]。
|
||||
|
||||
期待你的好评。
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://www.linuxandubuntu.com/home/making-vim-even-more-awesome-with-these-cool-features
|
||||
|
||||
作者:[LINUXANDUBUNTU][a]
|
||||
译者:[stevenzdg988](https://github.com/stevenzdg988)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://www.linuxandubuntu.com
|
||||
[1]:http://www.linuxandubuntu.com/home/category/distros
|
||||
[2]:http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/vim_orig.png
|
||||
[3]:http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/vim_1_orig.png
|
||||
[4]:http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/vim_2_orig.png
|
||||
[5]:http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/vim_3_orig.png
|
||||
[6]:http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/vim_4_orig.png
|
||||
[7]:https://github.com/phenomenal-ab/VIm-Configurations/blob/master/.vimrc
|
Loading…
Reference in New Issue
Block a user