GNU less provides two primary options for case-insensitive searching:
-i or --ignore-case -I or --IGNORE-CASE
The key difference lies in how they handle patterns containing uppercase letters. While -i
respects case when the pattern contains uppercase, -I
forces case-insensitive matching regardless.
Many users need to toggle case sensitivity during an active less session, without restarting with command-line flags. Unlike vim's \c
modifier, less doesn't provide an in-session toggle - at least not obviously.
Here are three effective approaches:
1. Using Regex Character Classes
Convert your search pattern to use character classes:
/S[aA]m[pP]le
This matches "Sample" or "sample" while maintaining case sensitivity for other searches.
2. Temporary Case-Insensitive Mode
During an active less session, type:
-I (followed by your search term)
Example:
-Ierror
This temporarily enables case-insensitive search for that specific pattern.
3. Environment Variable Solution
Set the LESS environment variable in your shell:
export LESS="-I"
Now all less sessions will default to case-insensitive searching.
For power users, combine techniques:
/I[sS][yY][sS][tT][eE][mM]
This forces case-insensitive search (I
) while explicitly defining case variations.
When debugging log files or examining code, case variations often occur unexpectedly. These techniques prevent restarting less sessions just to adjust search sensitivity.
When working with GNU less, developers often need to search through files while ignoring case sensitivity. The standard approach is to use the -I
or -i
command-line options, but this requires foreknowledge that case-insensitive searches will be needed.
Unlike Vim which uses the \c
modifier for ad-hoc case-insensitive searches, less doesn't have an identical feature. However, we can leverage less's built-in pattern modifiers:
/search_term\c
Note that this isn't exactly equivalent to Vim's behavior, but it's the closest alternative in less.
1. Using Regular Expression Modifiers
You can use regex pattern modifiers for case-insensitive searches:
/(?i)search_term
This Perl-style modifier tells less to ignore case for this specific search.
2. Toggling Case Sensitivity During Runtime
While viewing a file in less, you can toggle case sensitivity:
-i (then press Enter)
This will toggle case sensitivity for subsequent searches until you exit less.
3. Using Environment Variables
Set default search behavior via environment variables in your shell:
export LESS='-I'
This makes all searches case-insensitive by default.
Combining with Other Search Features
You can combine case-insensitive search with other less features:
/^[Hh]ello.*world\c
This searches for lines starting with "hello" or "Hello" followed by "world" in any case.
Persistent Configuration
For frequent use, add this to your shell configuration:
alias less='less -I'
Or create a function for more control:
function myless() {
case "$1" in
-c*) command less "${@}" ;;
*) command less -I "${@}" ;;
esac
}
The main limitation is that less's case-insensitive search isn't as flexible as Vim's \c
modifier. The workaround is to either:
1. Use regex patterns with (?i)
2. Set default case-insensitive behavior
3. Toggle during operation with -i
Each method has trade-offs between convenience and precision.