In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "how to use some cool features in linux to make Vim more powerful". The content is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to use some cool features in linux to make Vim more powerful".
Vim is an integral part of every Linux distribution] and is the most commonly used tool (terminal-based, of course) for Linux users. At least, this statement is true to me. People may argue about what tools are better for programming, and it is true that Vim may not be a good choice, because there are many different IDE or other mature text editors like Sublime Text 3Magee Atom that make programming easier.
My feelings
But I think Vim should work the way we want from the beginning, while other editors let us work the way we have designed, not actually the way we want to work. I don't talk too much about other editors because I haven't used them too much (I have a soft spot for Vim).
Anyway, let's do something with Vim. It's perfectly competent.
Use Vim to design and execute code
Consider a scenario where we use Vim to design C++ code and need to compile and run it.
(a). We return to the terminal via Ctrl + Z, or save and exit using: wq.
(B). But the task is not finished yet, and then you need to type a command similar to Gmail + fileName.cxx on the terminal to compile.
(C). Next you need to type. / a.out to execute it.
There are a lot of things that need to be done to get our C++ code to run in shell. But this doesn't seem to be the way to take advantage of Vim operations (Vim always tends to implement almost all operations with one or two keystrokes). So what exactly is the Vim way to do these things?
Vim mode
Vim is not just a text editor, it is a programming language for editing text. The programming language that helps us extend the functionality of Vim is "VimScript" (LCTT Vim script).
So, with the help of VimScript, we can easily automate the task of compiling and running code with a single button.
Create functions in vim .vimrc
The above is a snippet called the CPP () function created in my .vimrc configuration file.
Using VimScript to create functions
The syntax for creating a function in VimScript is very simple. It starts with the keyword func, followed by the function name (in VimScript, the function name must start with an uppercase letter, otherwise Vim will prompt an error). Use the keyword endfunc at the end of the function.
In the body of the function, you can see that the exec statement, no matter what you write after the exec keyword, is executed in Vim's command mode (remember, the command that starts with: at the bottom of the Vim window). Now, the string passed to exec is (LCTT::! clear & & a.out +% &. / string)-
Vim functions commands & symbols
When this function is called, it first clears the terminal screen so that it can only see the output, then executes Gmail + to process the file you are working on, and then runs the a.out file compiled from the previous step.
Map ctrl+r to running C++ code
I map the statement call CPP () to the key combination Ctrl+r so that I can now press Ctrl+r to execute my C++ code without having to manually type: call CPP (), and then press Enter.
The final result
We finally found a way to operate in the Vim way. Now, all you have to do is press one button, and the C++ code you write will be output on your screen, so you don't have to type all the lengthy commands. It also saves you time.
We can also implement this function for other languages.
Create function in vim for python
For Python: you can press Ctrl+e to interpret and execute your code.
Create function in vim for java
For Java: you can now press Ctrl+j, which will first compile your Java code, then execute your Java class file and display the output.
Further improve
So, this is how to operate in Vim. Now, let's look at how to do all of this in Vim. We can use these code snippets directly in Vim, while another way is to use the automatic command autocmd in Vim. The advantage of autocmd is that these commands do not need to be called by the user, and they are executed automatically under any specific conditions provided by the user.
I want to do this in autocmd instead of using different mappings for each language and executing code compiled by different programming languages.
Autocmd in vimrc
What is done here is to write automatic commands for all functions that define the execution of the corresponding file type code.
What will happen? When I open the buffer for any of the above mentioned file types, Vim automatically maps Ctrl + r to a function call, representing the enter key, so that you don't have to press enter every time you complete a separate task.
To do this, you simply add the function snippet to the .vimrc file, and then add all of these autocmd as well. This way, the next time you open Vim, Vim will have all the appropriate functions to execute all code with the same binding key.
These are all the contents of the article "how to use some cool features in linux to make Vim more powerful". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.