Sample Header Ad - 728x90

Unix & Linux Stack Exchange

Q&A for users of Linux, FreeBSD and other Unix-like operating systems

Latest Questions

38 votes
5 answers
57253 views
How to indent multiple lines in nano
Selecting lines in nano can be achieved using `Esc+A`. With multiple lines selected, how do I then indent all those lines at once?
Selecting lines in nano can be achieved using Esc+A. With multiple lines selected, how do I then indent all those lines at once?
52d6c6af (491 rep)
Dec 22, 2013, 03:51 PM • Last activity: Aug 6, 2025, 07:19 PM
83 votes
4 answers
95556 views
What's the standard for indentation in shell scripts?
Java community use 4 spaces as the unit of indentation. [1] Ruby community use 2 spaces that is generally agreed-upon. [2] What's the standard for indentation in shell scripts? 2 or 4 spaces or 1 tab? [1]: http://java.sun.com/docs/codeconv/html/CodeConventions.doc3.html [2]: http://www.caliban.org/r...
Java community use 4 spaces as the unit of indentation. [1] Ruby community use 2 spaces that is generally agreed-upon. [2] What's the standard for indentation in shell scripts? 2 or 4 spaces or 1 tab?
Arturo Herrero (2516 rep)
May 23, 2012, 09:27 AM • Last activity: Jul 3, 2025, 03:15 PM
2 votes
1 answers
923 views
Does bash (or zsh) "officially" mandate the use of tabs for indentation in scripts?
I recently came across the following statement ([source][1]; emphasis added): > For shell scripts, using tabs is not a matter of preference or style; *it's how the language is defined*. I am trying to make sense of this claim. Of course, it is somewhat loosely worded 1 , but I would like to know if...
I recently came across the following statement (source ; emphasis added): > For shell scripts, using tabs is not a matter of preference or style; *it's how the language is defined*. I am trying to make sense of this claim. Of course, it is somewhat loosely worded1, but I would like to know if there is any truth to it. In particular, I would like to know if the official documentation for either bash or zsh (the two shells I routinely write scripts for) say anything approaching a mandate or recommendation to use tabs for indentation of source code. (I would appreciate explicit references to the supporting paragraphs in this documentation.) (FWIW, let me point out that I am aware of the fact that, in practice, both bash and zsh readily interpret scripts that are *not* indented exclusively with tabs. Therefore, I don't expect the documentation for either shell to go much further than a strong recommendation, if they mention the matter at all.) --- 1 For one thing, it refers simultaneously to "shell scripts" and "the language", which contradicts the facts that there are multiple shells in current use, each defining its own language.
kjo (16299 rep)
Oct 25, 2022, 02:07 PM • Last activity: Oct 25, 2022, 02:12 PM
10 votes
3 answers
3704 views
In Vim, how can I automatically determine whether to use spaces or tabs for indentation?
Sometimes I edit others source code where the prevailing style is to use tabs. In this case, I want to keep the existing convention of using literal tabs. For files I create myself, and files that use spaces as the prevailing indent style, I wish to use that instead. How can I do this in vim?
Sometimes I edit others source code where the prevailing style is to use tabs. In this case, I want to keep the existing convention of using literal tabs. For files I create myself, and files that use spaces as the prevailing indent style, I wish to use that instead. How can I do this in vim?
Chris Down (129976 rep)
Jan 31, 2013, 05:58 AM • Last activity: Aug 22, 2022, 02:07 AM
3 votes
1 answers
1382 views
Disable vim from auto indenting my comment lines, but continue to autoindent everything else
I want vim to stop auto indenting any time I add a `#` to the beginning of a link after its white space. Here is an example of the problem: Original text: ``` dog cat moose ``` If I add a `#` comment to the "cat" line, this automatically happens (Which I dont want): updated text: ``` dog #cat moose...
I want vim to stop auto indenting any time I add a # to the beginning of a link after its white space. Here is an example of the problem: Original text:
dog
    cat
        moose
If I add a # comment to the "cat" line, this automatically happens (Which I dont want): updated text:
dog
#cat
        moose
If I add a # comment to the "dog" line, this automatically happens (Which I dont want): updated text:
#dog
#cat
        moose
What I do want is the following, force vim to IGNORE any time I add a #, just don't indent, leave the line alone. Original text:
#dog
    #cat
        moose
Here is my vimrc currently.
set number 
set nowrap 
set linebreak 
set tabstop=4 
set softtabstop=4 
set shiftwidth=4 
set shiftround 
set expandtab 
set title
set mouse=a
set hlsearch
set smarttab
set autoindent
set background=dark
filetype indent on
set filetype=html
set smartindent
set nocompatible
syn on
set splitright
What I have tried: I have tried adding these settings with no success:
set nosmartindent 
set cindent 
set cinkeys-=0# 
set indentkeys-=0#
I also tried many of the suggestions in this post with no success. I like vim autoindent, I want vim to use autoindent, but NOT when I use a # at any time. How can I force vim to ingore applying an autoindent when # is used?
Dave (700 rep)
Apr 22, 2022, 12:59 PM • Last activity: Apr 22, 2022, 04:14 PM
5 votes
1 answers
2609 views
How to use awk to indent a source file based on simple rules?
How can I indent source code based on a couple of simple rules? As an example, I've used sed and ask to transform a selenium HTML source table to the following rspec like code. How could I consistently indent lines between `describe ` and `end` ? Ideally I would like to be able to add indenting to d...
How can I indent source code based on a couple of simple rules? As an example, I've used sed and ask to transform a selenium HTML source table to the following rspec like code. How could I consistently indent lines between describe and end ? Ideally I would like to be able to add indenting to describe "Landing" do visit("http://some_url/url_reset ") visit("http://some_url/url_3_step_minimal_foundation ") # comments expect(css_vehicle1_auto_year) to be_visible end describe "Stage1" do wait_for_element_present(css_vehicle1_auto_year option_auto_year) select(auto_year, from: css_vehicle1_auto_year) ... end describe "Stage2" do fill_in(css_driver1_first_name, with: driver1_first_name) fill_in(css_driver1_last_name, with: driver1_last_name) ... submit(css_policy_form) expect(css_vehicle1_coverage_type) to be_visible end describe "Stage3" do wait_for_element_present(css_vehicle1_coverage_type) select(coverage_type, from: css_vehicle1_coverage_type) find(css_has_auto_insurance).click ... submit(css_policy_form) expect(css_quotes) to be_visible end so I have describe "Landing" do visit("http://some_url/url_reset ") visit("http://some_url/url_3_step_minimal_foundation ") # comments expect(css_vehicle1_auto_year) to be_visible end describe "Stage1" do wait_for_element_present(css_vehicle1_auto_year option_auto_year) select(auto_year, from: css_vehicle1_auto_year) ... end describe "Stage2" do fill_in(css_driver1_first_name, with: driver1_first_name) fill_in(css_driver1_last_name, with: driver1_last_name) ... submit(css_policy_form) expect(css_vehicle1_coverage_type) to be_visible end describe "Stage3" do wait_for_element_present(css_vehicle1_coverage_type) select(coverage_type, from: css_vehicle1_coverage_type) find(css_has_auto_insurance).click ... submit(css_policy_form) expect(css_quotes) to be_visible end The source code for the existing sed and awk's is at https://jsfiddle.net/4gbj5mh4/ but it's really messy and not what I am asking about. I've got the hang of simple sed and awk's but not sure where to start with this one. It would be great if it could also handle recursion. Not essential for me but the generalization is probably useful to others using this question, i.e. describe "a" do describe "b" do stuff more stuff end end to describe "a" do describe "b" do stuff more stuff end end btw I am also doing this custom conversion partly because I've used variables as page objects in selenium and they bork the built-in export to rspec.
Michael Durrant (43563 rep)
Aug 29, 2015, 01:35 AM • Last activity: Mar 18, 2022, 09:17 AM
0 votes
1 answers
308 views
Kate replaces text instead of indenting when typing Tab on selected lines
I tried to change all the indentation settings to fix another problem yesterday, but now I have the following problem and I don't know which setting causes it. If I select lines and type Tab, I would expect the selected lines to be indented. Instead, the selection is replaced with a tabulation. For...
I tried to change all the indentation settings to fix another problem yesterday, but now I have the following problem and I don't know which setting causes it. If I select lines and type Tab, I would expect the selected lines to be indented. Instead, the selection is replaced with a tabulation. For example, I start with: aa bb cc Then I select a part (what's between [ and ] is the selected part): aa b[b c]c Then I press Tab (---> represents a tabulation), I expect this: aa --->bb --->cc But I get this instead: aa b--->c How can I revert this behaviour? I'm using Kate 21.12.3.
optical (137 rep)
Mar 17, 2022, 03:55 PM • Last activity: Mar 17, 2022, 06:24 PM
2 votes
1 answers
916 views
Display `grep -lr` results as a tree
I'm searching a large number of text files which are organized in various subdirectories. I can run a command such as `grep -lr foobar ./`, and I get results like the following: ``` ./dirA/dirA.A/abc.txt ./dirA/dirA.A/def.txt ./dirA/dirA.A/dirA.A.A/ghi.txt ./dirA/dirA.B/jkl.txt ./dirB/mno.txt ``` I...
I'm searching a large number of text files which are organized in various subdirectories. I can run a command such as grep -lr foobar ./, and I get results like the following:
./dirA/dirA.A/abc.txt
./dirA/dirA.A/def.txt
./dirA/dirA.A/dirA.A.A/ghi.txt
./dirA/dirA.B/jkl.txt
./dirB/mno.txt
I would like some way to display these in a visual tree, similar to how the tree command works. Something roughly like this:
./
  dirA/
    dirA.A/
      abc.txt
      def.txt
      dirA.A.A/
        ghi.txt
    dirA.B/
      jkl.txt
  dirB/
    mno.txt
It seems like it'd be trivial to do this in some Python script with a stack, but I'd really like some way to do this straight from bash if there's a way to do it. So I guess I'm looking for a way to either (a) format/transform the output of grep, OR (b) some other generic "indent-by-common-prefix" utility that I've so-far been unable to find.
loneboat (241 rep)
Mar 16, 2022, 04:43 PM • Last activity: Mar 17, 2022, 05:32 PM
10 votes
2 answers
6353 views
How do I make Kate indent with spaces on Python files but use tabs for text files and other files?
My goal is to set Kate up to work properly on Python files but to use different settings (tabs not spaces) on other documents. I'm sure others are doing this, but I can't figure out a convenient solution. I appreciate any advice. Kate has settings for indentation here: 1. Click the Settings menu 2....
My goal is to set Kate up to work properly on Python files but to use different settings (tabs not spaces) on other documents. I'm sure others are doing this, but I can't figure out a convenient solution. I appreciate any advice. Kate has settings for indentation here: 1. Click the Settings menu 2. Click "Configure - Kate" 3. On the right expand "Editor" 4. Click "Indentation" One option is "Default indentation mode". One choice for that setting is Python. However, I cannot find where to set (or even display) the options used for the Python choice. Furthermore, it is not clear what is the interaction between "Default indentation mode" and the explicit settings for indentation on that page. Does one override the other?
MountainX (18888 rep)
Jul 25, 2013, 05:48 AM • Last activity: Mar 17, 2022, 03:35 PM
0 votes
2 answers
537 views
How to keep GNU Indent from formatting comments?
I don't want that GNU Indent to touch any comment whatsoever. But it seems that there is no option that allows me to keep the comments untouched. What commands or commands combination should I use to keep the comments in the code unmodified by indent.
I don't want that GNU Indent to touch any comment whatsoever. But it seems that there is no option that allows me to keep the comments untouched. What commands or commands combination should I use to keep the comments in the code unmodified by indent.
m4l490n (195 rep)
Sep 8, 2015, 07:55 PM • Last activity: Feb 25, 2022, 05:36 PM
24 votes
8 answers
18071 views
Change tab size of "cat" command
When I am in `vim` I can change the tab size with the following command: :set ts=4 Is it possible to set tab size for `cat` command output too?
When I am in vim I can change the tab size with the following command: :set ts=4 Is it possible to set tab size for cat command output too?
Meysam (4443 rep)
Feb 27, 2012, 02:28 PM • Last activity: Sep 20, 2021, 02:49 AM
6 votes
4 answers
1383 views
Disable GNU Indent backup files
I am using GNU Indent to format C code in my project. By default backup files are created ending with a `~`. I don't want to have any backup files created, is there a way to disable it?
I am using GNU Indent to format C code in my project. By default backup files are created ending with a ~. I don't want to have any backup files created, is there a way to disable it?
ivokosir (79 rep)
Dec 13, 2013, 10:51 PM • Last activity: Jul 23, 2021, 08:41 PM
12 votes
5 answers
9718 views
Shifting command output to the right
Say I'm writing a `.bashrc` file to give me some useful information in my login terminals and I'm telling it to run the cal command (a nice one). How would I go about shifting the calendar produced to the right to match the formatting of the rest of my `.bashrc` "welcome message"?
Say I'm writing a .bashrc file to give me some useful information in my login terminals and I'm telling it to run the cal command (a nice one). How would I go about shifting the calendar produced to the right to match the formatting of the rest of my .bashrc "welcome message"?
theStandard (343 rep)
Aug 3, 2014, 07:26 AM • Last activity: Mar 27, 2021, 06:09 AM
0 votes
3 answers
584 views
Script for formatting code into columns
Often I have code that I want to align based on similar structure of lines, not just the left-side auto indent. Is there a script out there that can do something like this? Here is an example of what I want to do. Given: ``` self.colorOfBackground =? colorOfBackground self.colorOfLineForTime =? colo...
Often I have code that I want to align based on similar structure of lines, not just the left-side auto indent. Is there a script out there that can do something like this? Here is an example of what I want to do. Given:
self.colorOfBackground =? colorOfBackground
            self.colorOfLineForTime =? colorOfLineForTime
            self.marginOnBottom =? marginOnBottom
            self.marginOnTop =? marginOnTop
            ...
I want to run a script and align each "column" on a tab so that they are aligned and easier to visually parse:
self.colorOfBackground     =?    colorOfBackground
            self.colorOfLineForTime    =?    colorOfLineForTime
            self.marginOnBottom        =?    marginOnBottom
            self.marginOnTop           =?    marginOnTop
            ...
I am thinking that a Perl or Python or AWK or some other scripting language could do this, but alas I know none of these. Till now I have been using Vim and its regex based substitution capabilities but I still spend most of the time manually spacing out the columns.
andrewz (153 rep)
Nov 28, 2020, 04:19 AM • Last activity: Nov 28, 2020, 05:26 PM
1 votes
1 answers
184 views
Vim re-indent file, hardcode some indents
I'm having some difficulty with the vim reindent files (with `gg=G`). When I have a larger file (not that large, maybe less than 400 lines of code) I think Vim is having trouble to indent some lines correctly since the line on which the indention of the line afterwards depends is lots of lines above...
I'm having some difficulty with the vim reindent files (with gg=G). When I have a larger file (not that large, maybe less than 400 lines of code) I think Vim is having trouble to indent some lines correctly since the line on which the indention of the line afterwards depends is lots of lines above (I assume so, because I tried it with smaller blocks and then the indentation is done correctly). Example:
\begin{itemize}
        \begin{minipage} %indent +2 (after \begin{itemize})
        \item %indent +1 (after \begin{minipage}) but -1 because it's \item
            %some lines %indent +1
        \end{minipage} %indent -1
\end{itemize} %indent -2 <--- here is the Problem, because here has to be -double indent
Now if in this case the lines at %some lines are lots of lines, then the \end{itemize} isn't shifted left by two indents (which would be correct) but by only one indent :/ Problem with this is that this messes the whole indention of all lines below. The solution I'd like most, is if there would be something like the %stopzone comment for LaTeX to signal the syntax highlighting to stop the current (math)zone. Maybe something like %indent -1 for move the line by one indent to the left. Does anyone know how you would implement something like this, or even better, it something like this does already exist? Or is there some other tool that can do this indentation better than Vim? It would be enough for me to get an approximate indentation from Vim and to use an external terminal utility to make the indentation really correct?
atticus (129 rep)
Apr 30, 2020, 09:48 PM • Last activity: May 1, 2020, 04:25 AM
11 votes
2 answers
3926 views
emacs - paste text from clipboard without formatting
I am using Emacs 24 for macOS. I'm having some problem with pasting code from clipboard to Emacs. The code that I usually select already is nicely formatted (with tab, indent, ...). However, when I paste it to Emacs, this redoes the formatting, so it is extremely slow for thousands of lines of code....
I am using Emacs 24 for macOS. I'm having some problem with pasting code from clipboard to Emacs. The code that I usually select already is nicely formatted (with tab, indent, ...). However, when I paste it to Emacs, this redoes the formatting, so it is extremely slow for thousands of lines of code. Is there a way to paste from clipboard without formatting?
Think Pl (483 rep)
Aug 2, 2012, 06:15 PM • Last activity: Feb 26, 2020, 05:10 PM
1 votes
1 answers
214 views
Vim autoindent stops indenting multiline array in bash after 20 lines
Given this code: #!/bin/bash _DATABASES=( "secretX" "secretmin" "secretcopyijui" "secretcroma" "secretdemo" "secretdicopy" "secretflashcolo" "secretmdat" "secretneton" "secretprintshar" "secretrealjet" "secretsolumax" "secretunicopia" "secretworddigit" "secretducao" "secrette" "secrette_app" "secret...
Given this code: #!/bin/bash _DATABASES=( "secretX" "secretmin" "secretcopyijui" "secretcroma" "secretdemo" "secretdicopy" "secretflashcolo" "secretmdat" "secretneton" "secretprintshar" "secretrealjet" "secretsolumax" "secretunicopia" "secretworddigit" "secretducao" "secrette" "secrette_app" "secretanopecanh" "secretx_ead" "secretx_site" "secretdroppy" "secret" ) When i do gg=G on vim, then the code be like this: #!/bin/bash _DATABASES=( "secretX" "secretmin" "secretcopyijui" "secretcroma" "secretdemo" "secretdicopy" "secretflashcolo" "secretmdat" "secretneton" "secretprintshar" "secretrealjet" "secretsolumax" "secretunicopia" "secretworddigit" "secretducao" "secrette" "secrette_app" "secretanopecanh" "secretx_ead" "secretx_site" "secretdroppy" "secret" ) Why? With smaller arrays everything works gracefully, but when it's an array with more than 20 elements, this happens... Tested with other language(JS, C++, PHP), no similiar behaviour happened. Info: Vim 7.4.52 No .vimrc
Yuri Santos (13 rep)
Aug 2, 2019, 11:00 PM • Last activity: Aug 3, 2019, 12:32 PM
0 votes
0 answers
293 views
Is there anyway that I can find all files indent with n-space?
I have files indent with 2, 3, and 4 spaces. They all have multiple indentation levels. Is there any existing tool allow me to find all the files indent by n spaces? Moreover, Is there anyway that allow me to convert all the indentation in all the files to a uniformed n-spaces indentation in batch?
I have files indent with 2, 3, and 4 spaces. They all have multiple indentation levels. Is there any existing tool allow me to find all the files indent by n spaces? Moreover, Is there anyway that allow me to convert all the indentation in all the files to a uniformed n-spaces indentation in batch?
Wang (1395 rep)
Apr 23, 2019, 02:41 PM
12 votes
2 answers
5157 views
vim auto indenting even after setting noai option
I am using vim 7.2 from putty terminal. Even if I run `set noai` it seems vim still trying to indent code. I am copying my code from Notepad++ to vim. following is from Notepad++ ![alt text][1] and following what I got in vim: ![alt text][2] I don't have any tab in my file. As a workaround I am open...
I am using vim 7.2 from putty terminal. Even if I run set noai it seems vim still trying to indent code. I am copying my code from Notepad++ to vim. following is from Notepad++ alt text and following what I got in vim: alt text I don't have any tab in my file. As a workaround I am opening old vi run set noai paste save and open in vim again. Any suggestion how to correct this behavior ?
Hemant (6950 rep)
Sep 1, 2010, 11:56 AM • Last activity: Feb 13, 2019, 06:24 PM
2 votes
1 answers
377 views
Why does GNU Indent collapse one level of indentation?
When I try to format my C code using [GNU Indent](https://www.gnu.org/software/indent/), it doesn't seem to deal with multiple levels of nested indentation. Specifically, it seems to collapse the second level of indentation. For example, if this is the code I start with: ```c #include int main(int a...
When I try to format my C code using [GNU Indent](https://www.gnu.org/software/indent/) , it doesn't seem to deal with multiple levels of nested indentation. Specifically, it seems to collapse the second level of indentation. For example, if this is the code I start with:
#include 

int main(int argc, char *argv[])
{
    int n;

    if (argc > 1) {
        printf("# of args: %d\n", argc);
    }

    for (n = 1; n 

int main(int argc, char *argv[])
{
    int n;

    if (argc > 1) {
	printf("# of args: %d\n", argc);
    }

    for (n = 1; n 

int
main (int argc, char *argv[])
{
  int n;

  if (argc > 1)
    {
      printf ("# of args: %d\n", argc);
    }

  for (n = 1; n <= 15; n++)
    {
      if (n % 3 == 0)
	{
	  printf ("fizz %d\n", n);
	}
      else if (n % 5 == 0)
	{
	  printf ("buzz %d\n", n);
	}
      else if (n % 3 == 0 && n % 5 == 0)
	{
	  printf ("fizzbuzz %d\n", n);
	}
      else
	{
	  printf ("%d\n", n);
	}
    }

  return 0;
}
Seems like if it does this out-of-the-box, lots of people would be asking about it, because if it's not a bug, it seems like a really strange way to format your code. Why does it do this? I'm using version 2.2.11 of GNU Indent.
jnrbsn (189 rep)
Jan 18, 2019, 08:26 PM • Last activity: Jan 18, 2019, 09:40 PM
Showing page 1 of 20 total questions