📜 ⬆️ ⬇️

Unix as IDE: Working with Text

A text editor is a basic tool for any programmer, which is why the question of his choice becomes the cause of violent debate. Unix has traditionally been closely associated with its two perennial favorites, Emacs and Vi , and their current versions of GNU Emacs and Vim . These editors have a very different approach to text editing, but they are comparable in strength.

Since I belong to the Vim sect, we will further discuss the inexhaustible features of this editor, as well as the command line tools invoked directly from Vim to extend the built-in functionality. Some of the principles discussed further may be applicable to Emacs , but not for simple editors like Nano .

This will be a very superficial review, since Vim ’s programming possibilities are truly incalculable, and it’s still pretty long. The Vim : help team is known among newbies as a quality and useful source of information, so do not neglect it.

File Type Definition


')
Vim has many settings that affect its behavior. For example, it is easy to customize the code highlighting depending on the type of file being downloaded. Thus, in particular, it is possible to establish an indent style that conforms to the norms of the programming language used. This setting is better to put one of the first in your .vimrc file.

if has("autocmd") filetype on filetype indent on filetype plugin on endif 


Syntax highlighting



Even if you are working with a 16-color terminal, feel free to turn on the backlight in your .vimrc file:

 syntax on 


The color schemes of a standard 16-color terminal are not very good in appearance, but they solve the tasks assigned to them if you pick up the correct syntax files. There are a huge variety of color schemes, so it will not be difficult to customize them for yourself, and a 256-color terminal or gVim will provide even more opportunities. Good syntax files will highlight errors with a bright red background color.

Line numbering



If you are used to numbering lines in traditional IDEs, you can enable it by typing:

 set number 


You can try this trick if you have Vim not older than 7.3, and you want to number the lines not absolutely, but in relation to the current one:

 set relativenumber 


Tag files



Vim works very well with the data generated by the ctags utility. This allows you to quickly find all occurrences of the desired identifier within the project, or go directly to declaring a variable from its place in the code, even if it is in another file. For large C projects with many files, you can save a huge amount of wasted time, and this may be the closest thing that brings Vim closer to mainstream IDEs.

You can run :! Ctags -R in the root directory of the project in one of their supported languages, and as a result you will receive a tag file with definitions and links to the location of identifiers in your project. When the tag file is ready, you can search for the use of the tag in the project as follows:

 :tag someClass 


The : tn and : tp commands allow you to move between tag entries throughout the entire project. The built-in tag mechanism covers most of the most used features, but if you want more clever chips, like a window with a list of tags, you can install the very popular Taglist plugin. The Unimpaired plugin by Tim Pope also contains several important command reassignments.

Calling external programs



There are 2 main methods for invoking external commands from a Vim session:


The third method, which is not discussed in detail here, involves the use of plug-ins like Conque to emulate the shell directly in the Vim buffer. I myself tried it in action, but the plugin seemed to me unfit for use. Perhaps the matter is simply in the wrong author's intention (see : help design-not ).

Vim is not a console or an operating system. You can hardly run the console inside Vim or use it to control the debugger. It has a different usage model: use it as a component of the command line or as part of an IDE.

Lint-like programs and syntax checking



Syntax check or compilation by calling an external program (for example, perl -c , gcc ) can be run from the editor using !: Commands . If you are editing a Perl file, you can run the following command:

 :!perl -c % /home/tom/project/test.pl syntax OK Press Enter or type command to continue 


The character "%" is a wildcard for the file loaded into the current buffer. The result is the text output of the command, if any, under the command line entered. If you need to call an external program all the time, then it is better to wipe it as a command, or even as a key combination in the .vimrc file. Below we define the command : PerlLint , called from normal mode using \ l :

 command PerlLint !perl -c % nnoremap <leader>l :PerlLint<CR> 


For many languages, there is another way to do the same thing by resorting to the quicklist window built into Vim . Set the makeprg setting for the file type by including a call to the module that forms a readable output for Vim :

 :set makeprg=perl\ -c\ -MVi::QuickFix\ % :set errorformat+=%m\ at\ %f\ line\ %l\. :set errorformat+=%m\ at\ %f\ line\ %l 


You must first install the desired module via CPAN. Upon completion, you can enter the command : make and check the syntax of the file. If errors are found, you can open the quicklist window ( : copen ), check their description and move between them with : cn and : cp .


Similar tricks work similarly with gcc output, and indeed with any syntax-checking program that operates in the results of its work with file names, line numbers and error messages. Similarly, you can work with web-based languages ​​like PHP, and with JSLint for Javascript. There is also a great Syntastic plugin designed for the same purpose.

Reading the output of other commands


To call a command and send its output directly to the current buffer, use : r! . For example, to get a quick list of the contents of a directory, you can type:

 :r!ls 


The teams, of course, is not limited to; With the help of : r you can read out any files, for example, public keys or template texts:

 :r ~/.ssh/id_rsa.pub :r ~/dev/perl/boilerplate/copyright.pl 


Filter output via other commands.



If you look at the hedge wider, then it will be all about filtering text in the buffer through external commands. Since the visual mode of Vim is great for working with data that is divided into columns, it often makes sense to use the column , cut , sort or awk commands.

For example, you can sort the entire file by the second column by the following command:

 :%!sort -k2 -r 


You can display only the third column of the selected text, where the line matches the pattern "/ vim /":

 :'<,'>!awk '/vim/ {print $3}' 


You can arrange keywords in rows 1 through 10 by column:

 :1,10!column -t 


Any text filter or command can be applied this way in Vim , and this increases the range of capabilities of the text editor by an order of magnitude. In this case, the Vim buffer is considered as a text stream, and all classical utilities communicate easily in this language.

Built-in alternatives



It should be noted that for the most common operations, such as sorting and searching, Vim has built-in methods : sort and : grep . They are useful if Vim is used under Windows, but they are not even close to the adaptability of console calls.

File comparison



Vim has a vimdiff comparison tool that allows you not only to look at differences in different versions of a file, but also to resolve conflicts through a trilateral merge, to replace the difference between pieces of text with the commands : diffput and : diffget . Vimdiff is called from the command line for at least two files like this:

 $ vimdiff file-v1.c file-v2.c 




Version control



You can run version control methods directly from Vim , and this is often enough. It should be remembered that "%" is always the place to put the file from the current buffer:

 :!svn status :!svn add % :!git commit -a 


The current champion in Git functionality is the Fugitive plugin by Tim Pope, which I highly recommend to anyone using Git with Vim . For more detailed coverage of the history and fundamentals of version control systems in Unix, see Part 7 of this series of articles.

A big difference



Programmers who are accustomed to graphic IDE, often consider Vim a toy or a relic. Part of the reason is the fact that Vim are used to seeing as a means of editing configuration files on the server, and not at all as a convenient command-line text editor. Its built-in tools fit so well with external Unix commands that they are often able to surprise even experienced users.

To be continued...

Unix as IDE: Introduction
Unix as IDE: Files
Unix as IDE: Working with Text
Unix as IDE: Compile

Source: https://habr.com/ru/post/151128/


All Articles