AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Change tabs to spaces in aquamacs3/21/2023 ![]() Built-in support for different source code version control systems.Can be customized to do many common tasks.Runs on virtually every OS and CL implementation.Not tying yourself into a single CL vendor’s editor.Emacs has fantastic support for working with Lisp code.Portable and multi-platform CL development environment, a You might think using sed command would have been easier at this point but that’s entirely your choice.This page is meant to provide an introduction to using Emacs as a Lisp IDE. The above command finds all the files ending with extension cpp, pass these files to the expand command and the expand command writes the output to the original files with the help of sponge command. name '*.cpp' -type f -exec bash -c 'expand -t 4 "$0" | sponge "$0"' \ What we have learned so far can be combined with the magnificent find and exec commands. But if you have a project that has several program files and you want to convert the tabs into spaces in all of them, you’ll have to be a bit smarter here. Till now whatever you learned was applicable to a single file. Once installed, you can use it in the following function: expand -t2 -i tab_file | sponge tab_file Convert tabs into spaces in all the matching files in a directory ![]() On Ubuntu/Debian based distributions, you can use this command to install moreutils: sudo apt install moreutils You’ll have to install the moreutils package. Now, the sponge command may not be available on your system. This is extremely useful when you are trying to change and save the same file. Sponge ‘soaks up’ the entire standard input before writing it to output. To do that, you can use the sponge command. But if your aim is to clean your code by convert the tabs into spaces of the existing program files, you would want the output to be saved in the original file itself. In all the above examples, you have saved the converted file into a new file. If we continue the previous example, here is what the command would look like: expand -t2 -i tab_file > space_file Only leading tabs converted into spaces Convert tabs into spaces and save it to the original file If you want to only convert the leading tabs into spaces, use the –i option. ![]() You don’t want to touch the tabs in between the lines that are actually part of the code.Įxpand provides this option as well. Often in the programs, you would only want to convert the leading tabs i.e. expand -t N tab_fileįor example, if you have to change each tab into 2 spaces, you can use the expand command like this: expand -t2 tab_file > space_file All tabs converted into 2 spaces Convert only leading tabs into spaces The good thing is that you can change the default space size with the -t option. That would look super weird if your code has such huge indentation. All tabs converted into 8 spaces (default) Reduce the number of spacesĪs I mentioned in the previous section, by default, a tab is equal to 8 spaces. Now if you see the file using the cat command, you won’t find any tabs anymore. You can see the location of the tabs in the file. ![]() You can use the cat command with option -T and will display all the tabs as ^I on the screen (stdout). The simplest method I find is by using the cat command. You can download this file from here if you want to practice the command while following this tutorial. I am using this sample text file which is a simple C program for checking odd and even number. Using expand command to convert tabs into spaces in Linux command line In this tutorial, I’ll show you how to convert the tabs into spaces in a text file in Linux command line. While you may prefer using tabs all the time, chances are that your coding guideline suggest using spaces.īut if you have already used tabs everywhere into your program and you need to convert those tabs into spaces so that the reviewer allows your code, you are at the right place. The debate over the use of tabs and spaces in the programming is a never ending one.
0 Comments
Read More
Leave a Reply. |